#!/bin/sh
### BEGIN INIT INFO
# Provides:          ngcp-www-csc
# Required-Start:    $syslog $network $local_fs $remote_fs $time
# Required-Stop:     $syslog $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Should-Start:      sshd mysql
# Short-Description: Start the ngcp-www-csc webapp
# Description:       Start the ngcp-www-csc webapp
### END INIT INFO

umask 0022

. /lib/lsb/init-functions

DAEMON=/usr/share/ngcp-www-csc/script/csc_fastcgi.pl
HOMEDIR=/usr/share/ngcp-www-csc
HOMERUN=/var/run/fastcgi
PIDFILE=$HOMERUN/ngcp-www-csc.pid
USER=www-data
GROUP=www-data
NAME="ngcp-www-csc"
DESC="NGCP-CSC Webapp"
DEFAULTS=/etc/default/$NAME
USOCKET=$HOMERUN/ngcp-www-csc.sock
LOGERR=/var/log/ngcp/ngcp-www-csc.log
NPROC=1

if ! [ -x "$DAEMON" ] ; then
  log_warning_msg "File $DAEMON not available/executable."
  exit 0
fi

check_running() {
  if [ -s $PIDFILE ]; then
    /bin/kill -0 $(cat $PIDFILE) >/dev/null 2>&1
  else
    PID=`pidofproc $DAEMON`
    if [ -z "$PID" ]; then
        return 1
    fi
  fi
}

# this is a workaround for slow startup of ngcp-www_csc and generation of its pidfile
check_for_ongoing_startup() {
  # if no other www_csc init script is running store our PID
  if ! [ -r "${PIDFILE}.startup" ] ; then
    echo "$$" > "${PIDFILE}.startup"
    return 0
  fi

  # another init script started up but didn't log a PID?
  startup_pid="$(cat ${PIDFILE}.startup)"
  if [ -z "$startup_pid" ] ; then
    log_progress_msg "starting up with invalid PID, forcing start"
    echo "$$" > "${PIDFILE}.startup"
    return 0
  fi

  # another init script started but was interrupted?
  if ! ps -o pid "$startup_pid" | grep -q -- "$startup_pid" ; then
    log_progress_msg "interrupted during startup, forcing start"
    echo "$$" > "${PIDFILE}.startup"
    return 0
  fi

  log_progress_msg "is already starting up, ignored"
  return 1
}

_start() {
  if [ "$RUN_DAEMON" != "yes" ]; then
    log_warning_msg "$NAME not yet configured. Edit /etc/default/$NAME first."
    log_end_msg 0
    exit 0
  fi
  rm -f $PIDFILE 2>/dev/null
  check_for_ongoing_startup || return 0
  start-stop-daemon --start --quiet \
        --pidfile $PIDFILE \
        --exec $DAEMON --chdir $HOMEDIR \
        --user $USER --group $GROUP --chuid $USER:$GROUP \
        -- $OPTIONS >/dev/null || log_failure_msg "error"
  rm -f "${PIDFILE}.startup"
  sleep 1
  if check_running ; then
  	return 0
  else
    return 1
  fi
}

_stop() {
  if ! check_running ; then
     log_progress_msg " not running"
     return 0
  fi
  if [ -e $PIDFILE ]; then
      /bin/kill `cat $PIDFILE`;
  else
      PID=`pidofproc $DAEMON`
      if [ -n "$PID" ]; then
          /bin/kill $PID
      fi
  fi
  for i in 1 2 3 4 5 6 7; do
      log_progress_msg "."
      sleep 1;
      if ! check_running ; then
          return 0
      fi
  done
  if check_running ; then
      return 1
  fi
}

# Load startup options if available
if [ -f $DEFAULTS ]; then
   . $DEFAULTS || true
fi

OPTIONS="--listen $USOCKET --daemon --pidfile $PIDFILE --nproc $NPROC"

# $HOMERUN perms
mkdir -p $HOMERUN && chown -R $USER:$GROUP $HOMERUN && chmod 0755 $HOMERUN

case "$1" in
  start)
        log_daemon_msg "Starting $DESC"
        if check_running; then
            log_progress_msg "already running"
            log_end_msg 0
            exit 0
        fi
        _start; status="$?"
        log_end_msg $status
        exit $status
        ;;
  stop)
        log_daemon_msg "Stopping $DESC: $NAME"
        _stop; status="$?"
        log_end_msg $status
        exit $status
        ;;
  restart|force-reload)
        log_daemon_msg "Restarting web application" $NAME
        _stop; status="$?"
        if [ "$status" = 0 ]; then
          _start; status="$?"
        fi
        log_end_msg $status
        exit $status
        ;;
  status)
        log_action_msg "Status of $DESC: "
        if [ -s $PIDFILE ]; then
          status_of_proc -p$PIDFILE $DAEMON $NAME ; exit $?
        else
          status_of_proc $DAEMON $NAME; exit $?
        fi
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0
