#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          ethercalc
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ethercalc sysv init script
# Description:       collaborative calc
### END INIT INFO
#

#repertoire d'installation
INSTALLPATH=/usr/share/eole/nodejs/ethercalc

NAME="Ethercalc"

NODE="/usr/bin/node"

LOGFILE=/cat/log/ethercalc.log

LAUNCH="$NODE $NODEOPTS"
PIDFILE=/var/run/ethercalc.pid

PM2=/usr/bin/pm2
CALC_CONF=/etc/pm2/ethercalc.json


cd $INSTALLPATH
test -x $NODE || exit 0

. /lib/lsb/init-functions

ec_start() {
    log_daemon_msg "Starting $NAME"
    HOME=/root ${PM2} start ${CALC_CONF}
    log_end_msg $?
    return $?
}

ec_stop() {
    log_daemon_msg "Stopping $NAME"
    ret=$?
    HOME=/root ${PM2} delete ${CALC_CONF} &>1 /dev/null
    if [ $ret -eq 0 ]
    then
        log_end_msg $ret
    fi
    return $ret
}

ec_status() {
    HOME=/root ${PM2} show ${CALC_CONF} &> /dev/null
}

case $1 in
    start)
        ec_start || exit 0
        ;;
    stop)
        ec_stop || exit 0
        ;;
    force-reload|restart)
        ec_stop && sleep 3
        ec_start || exit
        ;;
    status)
        ec_status || exit 0
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|force-reload|status}"
        exit 1
esac

exit 0

