export LANG=zh_CN.UTF-8

export port=8972
[ $# -gt 1 ] && export port=$2

RUN_PATH="/app/financeXsp/"
PRG_KEY="finance.xsp.com.cn"

cd $RUN_PATH

case "$1" in
    start)
        pid=$(pgrep -f $PRG_KEY)
        if [[ $pid -gt 0 ]]; then
            echo ""
            exit 1
        fi

        nohup node --harmony index.js $PRG_KEY >> logs/start.log 2>&1 &
        echo "$PRG_KEY started, please check log."

        ;;

    stop)
        pid=$(pgrep -f $PRG_KEY)
        if [[ $pid -gt 0 ]]; then
            kill -9 $pid
            echo "$PRG_KEY stoped!"
        else
            echo "$PRG_KEY not found, nothing to stop!"
        fi

        ;;

    restart)
        $0 stop $port
        sleep 1
        $0 start $port

        ;;
    *)
        echo "Usage: $0 {start|stop|restart} port"
        exit 1
esac

exit 0
