#compdef jonas
# ------------------------------------------------------------------------------
# Copyright (c) 2011 Github zsh-users - http://github.com/zsh-users
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the zsh-users nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
#  Completion script for JOnAS 5.2 (http://jonas.ow2.org).
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Julien Nicoulaud <julien.nicoulaud@gmail.com>
#
# ------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# ------------------------------------------------------------------------------


typeset -A opt_args
local context state line curcontext="$curcontext"

_jonas() {
  local ret=1

  _arguments -C \
    '1:cmd:->cmds' \
    '*::arg:->args' \
  && ret=0

  case "$state" in
    (cmds)
      local commands; commands=(
        'version:show version information'
        'check:check that the JOnAS environment is correctly set'
        'start:start a server instance'
        'stop:stop a server instance'
        'admin:administrate a server instance'
      )
      _describe -t commands 'command' commands && ret=0
    ;;
    (args)
      curcontext="${curcontext%:*:*}:jonas-cmd-$words[1]:"
      case $words[1] in
        (version|check)
          _message 'no more arguments' && ret=0
        ;;
        (start)
          _arguments \
            '-standby[start a minimal JOnAS server with only mandatory services]' \
            '(-bg)-fg[start the server in foreground mode]' \
            '(-fg)-bg[start the server in background mode]' \
            '-win[start the server in a new window]' \
            '(-bg)-tui[start the Apache Felix TUI (force foreground mode)]' \
            '-gui[start the Apache Felix GUI]' \
            '-dev[start a JOnAS server by using bundles present in the default maven repository instead of bundles under $JONAS_ROOT/lib/bundles]' \
            '-clean[clean the Apache Felix cache before starting a JOnAS server]' \
            '-n[set the server name, must be unique in the domain (default: jonas)]:name' \
            '-target[start another server or cluster (group of servers) in the domain]:server' \
            '-Ddomain.name=[set the name of the management domain to which the server belongs]:domain' \
          && ret=0
        ;;
        (stop)
          _arguments \
            '-standby[stop all services except the mandatory ones]' \
            '-n[set the name of the server to stop (default: jonas)]:name' \
            '-target[stop another server or cluster (group of servers) in the domain]:server' \
            '-Ddomain.name=[set the name of the management domain to which the server belongs]:domain' \
          && ret=0
        ;;
        (admin)
          _arguments \
            '(- : *)-?[print the help message]' \
            '-win[administer the server in a new window]' \
            '-n[set the name of the server to administer (default: jonas)]:name' \
            '-username[set the username when authentication is required]: :_users' \
            '-password[set the password when authentication is required]:password' \
            '-registry[set the registry URL]: :_urls' \
            '-protocol[set the protocol name]:protocol:((jrmp\:JRE\ implementation\ of\ RMI\ on\ the\ JRMP\ protocol\ \(default\) iiop\:JacORB\ implementation\ of\ RMI\ over\ the\ IIOP\ protocol irmi\:Oracle\ JRE\ independant\ implementation\ of\ RMI))' \
            '-a[deploy an application from a given filepath on the current server, or on another target in the domain if the current server is a master]:Java application archive:_files -g "*.(j|w|r|e)ar"' \
            '-r[undeploy a previously deployed application from the current server or from the specified target if the current server is a master]:Java application archive:_files -g "*.(j|w|r|e)ar"' \
            '-gc[run the garbage collector on the current JOnAS server]' \
            '-passivate[passivate all entity bean instances]' \
            '-e[list the properties of the current JOnAS server]' \
            '-j[list the registered JNDI names, as seen by the current JOnAS server]' \
            '-l[list the beans currently loaded by the current JOnAS server]' \
            '-synch[synchronize the entity bean instances on the current JOnAS server]' \
            '-debug[set the logging level for the given topic to DEBUG]:topic' \
            '-tt[change the default timeout for transactions]:timeout (seconds)' \
            '-ping[wait until the JOnAS server is available]' \
            '-timeout[maximum time to wait when -ping is used]:timeout (seconds)' \
          && ret=0
        ;;
      esac
    ;;
  esac

  return ret
}

_jonas "$@"
