#!/bin/bash
### BEGIN INIT INFO
# Provides:          {{ application.project }}-{{ application.module }}
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Init script for {{ application.project }}-{{ application.module }}.
# Description:       Init script for {{ application.project }}-{{ application.module }}.
### END INIT INFO

# GENERATED, DO NOT MODIFY
#
# description: Starts and stops {{ application.project }}-{{ application.module }}.
#
# Options:
#   PROJECT={{ application.project }}
#   MODULE={{ application.module }}
#   PID_FILE={{ folders.pidPath }}
#   ETC_FOLDER={{ folders.etc }}
#   LIB_FOLDER={{ folders.lib }}


. /lib/lsb/init-functions

DAEMON="{{ application.project }}-{{ application.module }}"
DAEMON_BASE="{{ application.project }}"
PID_FILE="{{ folders.pidPath }}"
PID_FOLDER="{{ folders.pidFolder }}"
HPROF_FOLDER="{{ folders.hprofFolder }}"
DAEMON_USER="{{ application.project }}"
DAEMON_USER_GROUP="{{ application.project }}:nogroup"

ETC_FOLDER="{{ folders.etc }}"
BIN_FOLDER="{{ folders.bin }}"
# To wait for the balancer to stop sending us requests
PING_DISABLE_SLEEP="{{ application.ping_disable_sleep }}"

{% if http.pingPort is not none %}
# Ping coordinates
PING_URL="http://localhost:{{ http.pingPort }}/ping"
PING_SLEEP=5
PING_RETRY=120
{% endif %}

case "$1" in
  start)
    log_begin_msg "Starting $DAEMON..."

    test -d $PID_FOLDER || mkdir $PID_FOLDER
    chown $DAEMON_USER_GROUP $PID_FOLDER

    test -d $HPROF_FOLDER || mkdir $HPROF_FOLDER
    chown $DAEMON_USER_GROUP $HPROF_FOLDER

    ls -t $HPROF_FOLDER | awk -v r=$HPROF_FOLDER '{print r"/"$1}' | grep '.hprof' | tail -n +4| xargs -i rm -f {}

    if start-stop-daemon --quiet --stop --signal 0 --pidfile $PID_FILE 2>/dev/null 1>/dev/null; then
      log_failure_msg "$DAEMON already running"
    else
      uf=$ETC_FOLDER/ulimit.conf
      if [ -r $uf ]; then
        log_action_msg "Loading ulimits from file" $uf
        . $uf
      fi

      start-stop-daemon --chuid $DAEMON_USER --chdir $BIN_FOLDER --background --make-pidfile --verbose --start --exec $BIN_FOLDER/start.sh --pidfile $PID_FILE

{% if http.pingPort is not none %}
      for i in `seq $PING_RETRY`; do
      	curl --fail --connect-timeout 1 -m 1 -s $PING_URL >/dev/null && log_end_msg $? && exit 0
        sleep $PING_SLEEP
      done

      log_failure_msg "$DAEMON doesn't respond to $PING_URL"
      exit 1
{% else %}
      log_end_msg $?
{% endif %}
    fi
  ;;

  stop)
    if [ -r $PID_FILE ]; then
        if [ -n "$PING_DISABLE_SLEEP" ]; then
            log_begin_msg "Sending SIGTSTP to $DAEMON $locale to disable /ping and waiting for $PING_DISABLE_SLEEP seconds..."
            kill -SIGTSTP `cat $PID_FILE` && sleep $PING_DISABLE_SLEEP
            log_end_msg $?
        fi
    fi

    log_begin_msg "Stopping $DAEMON..."
    start-stop-daemon --quiet --retry 10 --stop --pidfile $PID_FILE 1>/dev/null 2>&1
    log_end_msg $?
    rm -f $PID_FILE 2>/dev/null
  ;;

  restart)
    log_begin_msg "Restarting $DAEMON..."
    $0 stop
    sleep 1
    $0 start
    exit $?
  ;;

*)
  log_success_msg "Usage $0 {start|stop|restart}"
  exit 1

esac