#!/bin/bash
#
# auralis-gcmd  Startup script to start the auralis Google Cloud Messaging
# service that sends push messages to the Google Cloud Messaging service.
#
# processname: auralis-gcmd
#
### BEGIN INIT INFO
# Provides: auralis-gcmd
# Required-Start: $local_fs $remote_fs $network $syslog $named mysqld
# Required-Stop:  $local_fs $remote_fs $network $syslog $named mysqld
# X-Start-Before: httpd
# Default-Start: 3 4 5
# Default-Stop: 0 1 6
# Short-Description: auralis Google Cloud Messaging service
### END INIT INFO

# Source function library.
. /lib/lsb/init-functions

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

if [ -f /etc/profile.d/auralis.sh ]; then
    . /etc/profile.d/auralis.sh
fi

if [ -f /etc/default/auralis-gcmd ]; then
    . /etc/default/auralis-gcmd
fi

gcmd=${GCMD-/usr/bin/auralis-gcmd}
prog=auralis-gcmd
user=${GCMDUSER-www-data}
pidfile=${PIDFILE-/var/run/auralis/gcmd.pid}
rundir=$(dirname $pidfile)
env=${SYMFONY_ENV-prod}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

# Check if any of $pid (could be plural) are running
checkpid() {
    local i

    for i in $* ; do
            [ -d "/proc/$i" ] && return 0
    done
    return 1
}

# Returns 0 if the process with PID $1 is running and is the java service process.
function checkProcess {
    local pid="$1"
    checkpid $pid || return 1
    ps -p $pid --no-headers -o args | grep -q -F "$gcmd" || return 1
    return 0;
}

# Returns 0 when the service is running and sets the variable $pid to the PID.
function getPID {
    [ -f $pidfile ] || return 1
    pid="$(<$pidfile)"
    [ "$pid" != "" ] || return 1
    checkProcess $pid || return 1
    return 0;
}

start() {
    if ! mkdir -p $rundir; then
        log_failure_msg "can't create $rundir"
        exit 1
    fi

    log_daemon_msg "Starting auralis GCMD" $prog

    getPID
    if [ $? -eq 0 ]; then
        log_progress_msg "is already running"
        log_end_msg 0
        return 0;
    fi

    # temporary outputfile to redirect nohup
    tmpout=`su - $user -s $SHELL -c "mktemp"`
    # Writing pid into temporary file is required instead of writing it direct into $pidfile.
    # If $user is not root we (usually) have no right to write in $pidfile.
    tmpfile=`su - $user -s $SHELL -c "mktemp"`
    su - $user -s $SHELL -c "nohup $gcmd -e $env $OPTIONS >$tmpout 2>&1 & echo \$! > $tmpfile"
    pid=`cat $tmpfile`
    rm -f $tmpfile
    # wait and check if process is still running
    sleep 1
    checkProcess $pid
    RETVAL=$?
    cat $tmpout # must be called after sleep, otherwise file is empty
    rm -f $tmpout
    if [ $RETVAL = 0 ]; then
        echo $pid > $pidfile
        log_end_msg 0
    else
        rm -f $pidfile
        log_end_msg 1
    fi
    return $RETVAL
}

stop() {
    log_daemon_msg "Stopping auralis GCMD" $prog
    start-stop-daemon --stop --quiet --oknodo --retry ${STOP_TIMEOUT} --pidfile ${pidfile}
    RETVAL=$?
    if [ $RETVAL = 0 ]; then
        rm -f ${pidfile}
        log_end_msg 0
    else
        log_end_msg 1
    fi
    return $RETVAL
}

# See how we were called.
case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    status)
        status_of_proc -p ${pidfile} $prog $prog
        RETVAL=$?
    ;;
    force-reload)
        stop
        start
    ;;
    restart)
        stop
        start
    ;;
    condrestart|try-restart)
        if status_of_proc -p ${pidfile} $prog $prog >/dev/null; then
            stop
            start
        fi
    ;;
    *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|status|help}"
        RETVAL=2
    ;;
esac

exit $RETVAL
