#!/bin/sh
##
##  pm2.sh -- Node PM2 shell execution wrapper
##  Copyright (c) 2014-2016 Ralf S. Engelschall <rse@engelschall.com>
##

#   emulate the standard pm2 wrapper script (shell part)
if [ -f $HOME/.pm2/custom_options.sh ]; then
    . $HOME/.pm2/custom_options.sh
fi

#   execute the standard pm2 wrapper script
pm2_cmd () {
    NODE_PATH=@l_prefix@/lib/node/pkg/pm2
    export NODE_PATH
    @l_prefix@/bin/node \
        $PM2_NODE_OPTIONS \
        @l_prefix@/lib/node/pkg/pm2/pm2/bin/pm2 \
        ${1+"$@"}
}

#   simple deployment/undeployment commands
if [ $# -ge 1 ]; then
    cmd="$1"
    case "$cmd" in
        local-deploy | local-undeploy )
            if [ ".`id -u`" != .@l_nuid@ ]; then
                echo "$0: ERROR: command \"$cmd\" can only be used under UID @l_nuid@" 1>&2
                exit 1
            fi
            if [ $# -lt 2 ]; then
                echo "$0: ERROR: command \"$cmd\" requires a deployment unit name" 1>&2
                exit 1
            fi
            name="$2"
            if [ ".$name" = . ]; then
                echo "$0: ERROR: invalid (empty) deployment unit name" 1>&2
                exit 1
            fi
            appdir="@l_prefix@/var/pm2/app/"
            case "$cmd" in
                deploy )
                    if [ $# -ne 3 ]; then
                        echo "$0: ERROR: invalid number of arguments for command \"undeploy\"" 1>&2
                        echo "$0: USAGE: $0 local-deploy <name> <artifact>" 1>&2
                        exit 1
                    fi
                    if [ -d "$appdir/$name" ]; then
                        echo "$0: ERROR: deployment unit \"$name\" already existing -- undeploy first" 1>&2
                        exit 1
                    fi
                    artifact="$3"
                    if [ ! -f "$artifact" ]; then
                        echo "$0: ERROR: deployment artifact \"$artifact\" not found" 1>&2
                        exit 1
                    fi
                    mkdir "$appdir/$name"
                    unzip -q -o -d "$appdir/$name" "$artifact"
                    js=""
                    if [ -f "$appdir/$name/app.js" ]; then
                        js="$appdir/$name/app.js"
                    elif [ -f "$appdir/$name/app.json" ]; then
                        js="$appdir/$name/pm2.json"
                    else
                        echo "$0: ERROR: deployment unit \"$name\" has neither \"app.js\" nor \"pm2.json\" at the top-level" 1>&2
                        rm -rf "$appdir/$name"
                        exit 1
                    fi
                    pm2_cmd -s start "$js" --name "$name"
                    exit 0
                    ;;
                undeploy )
                    if [ $# -ne 2 ]; then
                        echo "$0: ERROR: invalid number of arguments for command \"undeploy\"" 1>&2
                        echo "$0: USAGE: $0 local-undeploy <name>" 1>&2
                        exit 1
                    fi
                    if [ ! -d "$appdir/$name" ]; then
                        echo "$0: ERROR: deployment unit \"$name\" not already existing -- deploy first" 1>&2
                        exit 1
                    fi
                    pm2_cmd -s stop "$name"
                    pm2_cmd -s delete "$name"
                    rm -rf "$appdir/$name"
                    exit 0
                    ;;
            esac
            ;;
    esac
fi

#   pass-through execution to pm2 wrapper script
pm2_cmd ${1+"$@"}

