#!/bin/zsh -f
#compdef pm2
#autoload

local -a _1st_arguments

_1st_arguments=(
    "start:start and daemonize an app"
    "trigger:trigger process action"
    "deploy:deploy your json"
    "startOrRestart:start or restart JSON file"
    "startOrReload:start or gracefully reload JSON file"
    "pid:return pid of [app_name] or all"
    "stop:stop a process"
    "restart:restart a process"
    "scale:scale up/down a process in cluster mode depending on total_number param"
    "profile\:mem:Sample PM2 heap memory"
    "profile\:cpu:Profile PM2 cpu"
    "reload:reload processes (note that its for app using HTTP/HTTPS)"
    "id:get process id by name"
    "inspect:inspect a process"
    "delete:stop and delete a process from pm2 process list"
    "sendSignal:send a system signal to the target process"
    "ping:ping pm2 daemon - if not up it will launch it"
    "updatePM2:update in-memory PM2 with local PM2"
    "install:install or update a module and run it forever"
    "module\:update:update a module and run it forever"
    "module\:generate:Generate a sample module in current folder"
    "uninstall:stop and uninstall a module"
    "package:Check & Package TAR type module"
    "publish:Publish the module you are currently on"
    "set:sets the specified config <key> <value>"
    "multiset:multiset eg \"key1 val1 key2 val2\""
    "get:get value for <key>"
    "config:get / set module config values"
    "unset:clears the specified config <key>"
    "report:give a full pm2 report for https\://github.com/Unitech/pm2/issues"
    "link:link with the pm2 monitoring dashboard"
    "unlink:unlink with the pm2 monitoring dashboard"
    "monitor:monitor target process"
    "unmonitor:unmonitor target process"
    "open:open the pm2 monitoring dashboard"
    "plus:enable pm2 plus"
    "login:Login to pm2 plus"
    "logout:Logout from pm2 plus"
    "web:launch a health API on 0.0.0.0\:9615"
    "dump:dump all processes for resurrecting them later"
    "cleardump:Create empty dump file"
    "send:send stdin to <pm_id>"
    "attach:attach stdin/stdout to application identified by <pm_id>"
    "resurrect:resurrect previously dumped processes"
    "unstartup:disable the pm2 startup hook"
    "startup:enable the pm2 startup hook"
    "logrotate:copy default logrotate configuration"
    "ecosystem:generate a process conf file. (mode = null or simple)"
    "reset:reset counters for process"
    "describe:describe all parameters of a process id"
    "list:list all processes"
    "jlist:list all processes in JSON format"
    "prettylist:print json in a prettified JSON"
    "monit:launch termcaps monitoring"
    "imonit:launch legacy termcaps monitoring"
    "dashboard:launch dashboard with monitoring and logs"
    "flush:flush logs"
    "reloadLogs:reload all logs"
    "logs:stream logs file. Default stream all logs"
    "kill:kill daemon"
    "pull:updates repository for a given app"
    "forward:updates repository to the next commit for a given app"
    "backward:downgrades repository to the previous commit for a given app"
    "deepUpdate:performs a deep update of PM2"
    "serve:serve a directory over http via port"
    "examples:display pm2 usage examples"
)

local -a id_names

_id_names() {
    local app_list
    app_list=`pm2 list -m`

    local -a names ids 
    names=(`echo $app_list | grep '+---' | awk '{print $2}'`)
    ids=(`echo $app_list | grep 'pm2 id' | awk '{print $4}'`)

    if (( ${#ids} > 0 )); then
        for i in {1..${#ids}}; do
            id_names+=( "${ids[i]}:${names[i]}" )
        done
    fi
}

_arguments \
    '(-v --version)'{-v,--version}'[output version]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    '*:: :->subcmds' && return 0

if (( CURRENT == 1 )); then
    _describe "command" _1st_arguments
    return
fi

local -a id_comp id_all_comp id_all_files_comp start_options logs_options
id_comp=('1: :->id_comp')
id_all_comp=('1: :->id_all_comp')
id_all_files_comp=('1: :->id_all_files_comp')
start_options=(
    '--watch[Watch folder for changes]'
    '--fresh[Rebuild Dockerfile]'
    '--daemon[Run container in Daemon mode (debug purposes)]'
    '--container[Start application in container mode]'
    '--dist[with --container; change local Dockerfile to containerize all files in current directory]'
    '--image-name[with --dist; set the exported image name]'
    '--node-version[with --container, set a specific major Node.js version]'
    '--dockerdaemon[for debugging purpose]'
    '(-h --help)'{-h,--help}'[output usage information]'
    $id_all_files_comp
)
logs_options=(
    '--json[json log output]'
    '--format[formated log output]'
    '--raw[raw output]'
    '--err[only shows error output]'
    '--out[only shows standard output]'
    '--lines[output the last N lines, instead of the last 15 by default]'
    '--timestamp[add timestamps (default format YYYY-MM-DD-HH:mm:ss)]'
    '--nostream[print logs without launching the log stream]'
    '(-h --help)'{-h,--help}'[output usage information]'
    $id_all_comp
)

case "$words[1]" in
    start)
        _arguments $start_options && return 0
        ;;
    logs)
        _arguments $logs_options && return 0
        ;;
    stop|restart|delete|reload|reset)
        _arguments $id_all_comp && return 0
        ;;
    env|inspect|monitor|unmonitor|describe)
        _arguments $id_comp && return 0
        ;;
    deploy|startOrRestart|startOrReload)
        _files ;;
esac

case "$state" in
    id_comp)
        _id_names
        _alternative \
            'args:app args:(($id_names))'
        ;;
    id_all_comp)
        _id_names
        id_names+=(all)
        _alternative \
            'args:app args:(($id_names))'
        ;;
    id_all_files_comp)
        _id_names
        id_names+=(all)
        _alternative \
            'args:app args:(($id_names))' \
            'files:filename:_files'
        ;;
esac
