#! /bin/sh
### BEGIN INIT INFO
# Provides:          sems
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Should-Start:      mysql sshd
# Should-Stop:       mysql
# Short-Description: Start/stop SEMS Application Server
### END INIT INFO

umask 0022

SEMS_CACHEDIR="/var/cache/ngcp-sems"

# read configuration from /etc/default/ngcp-sems file
if test -f /etc/default/ngcp-sems ; then
  . /etc/default/ngcp-sems
fi


if test "$SEMS_RUN" = "no" ; then
    echo -ne "Sems start disabled by default file"
    exit 0
fi

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/ngcp-sems
NAME=ngcp-sems
DESC=ngcp-sems

PARAMS=""

if ! test -d $SEMS_CACHEDIR ; then
	mkdir -m 0755 -p $SEMS_CACHEDIR
fi

for dir in $SEMS_CACHEDIR_EXTRADIRS ; do
	if ! test -d ${SEMS_CACHEDIR}/${dir} ; then
		mkdir -m 0755 -p ${SEMS_CACHEDIR}/${dir}
	fi
done

if ! test -d $SEMS_RUNDIR ; then
   mkdir -m 0755 -p $SEMS_RUNDIR
fi
chown -R $SEMS_USER:$SEMS_GROUP $SEMS_RUNDIR $SEMS_CACHEDIR

if test "$SEMS_PIDFILE" ; then
  PARAMS="$PARAMS -P $SEMS_PIDFILE"
fi

if test "$SEMS_USER" ; then
  PARAMS="$PARAMS -u $SEMS_USER"
fi

if test "$SEMS_GROUP" ; then
  PARAMS="$PARAMS -g $SEMS_GROUP"
fi

if test "$SEMS_CFG_FILE" ; then
  PARAMS="$PARAMS -f $SEMS_CFG_FILE"
  CFGPARAMS="-f $SEMS_CFG_FILE"
fi

if [ -x /usr/sbin/ngcp-virt-identify ]; then
  if /usr/sbin/ngcp-virt-identify --type container; then
    VIRT="yes"
  fi
fi

if test "$SEMS_CREATE_CORE" = "yes" ; then
  if [ "$VIRT" = "yes" ]; then
    echo "Container environment detected. Skipping core dump configuration."
  else
    # directory for the core dump files
    [ -d $SEMS_COREDIR ] || mkdir $SEMS_COREDIR
    chmod 777 $SEMS_COREDIR
    echo "$SEMS_COREDIR/core.%e.sig%s.%p" > /proc/sys/kernel/core_pattern
    echo 2 > /proc/sys/fs/suid_dumpable
    ulimit -c unlimited
  fi
fi

if [ "$VIRT" = "yes" ]; then
  echo "Container environment detected. Skipping file descriptors limit configuration."
else
  # raise file descriptors limit - call hold consumes two fds for RTP ports and one for moh file
  ulimit -n 100000
fi

if ! test -f $DAEMON ; then
  echo "Error: cannot find $DAEMON"
  exit 0
fi

LD_LIBRARY_PATH=/usr/lib/ngcp-sems
export LD_LIBRARY_PATH

set -e

. /lib/lsb/init-functions

case "$1" in
  start)
	if [ -x /usr/sbin/ngcp-check_active ]; then
		if ! /usr/sbin/ngcp-check_active -q; then
			echo "Ignored start action in inactive node"
			exit 0
		fi
	fi
	echo "Starting $DESC: $NAME"
	start-stop-daemon --start --quiet --oknodo --pidfile $SEMS_PIDFILE \
		--exec $DAEMON -- $PARAMS
	log_end_msg $?
	;;
  stop)
	echo -n "Stopping $DESC: $NAME"
	start-stop-daemon --oknodo --stop --quiet --pidfile $SEMS_PIDFILE \
		--exec $DAEMON
	log_end_msg $?
	;;
  restart|force-reload)
	echo "Restarting $DESC: $NAME"
	start-stop-daemon --oknodo --stop --quiet --pidfile \
		$SEMS_PIDFILE --exec $DAEMON
	if [ -x /usr/sbin/ngcp-check_active ]; then
		if ! /usr/sbin/ngcp-check_active -q; then
		  echo "Ignored start action in inactive node"
		  exit 0
		fi
	fi
	sleep 5
	start-stop-daemon --oknodo --start --quiet --pidfile \
		$SEMS_PIDFILE --exec $DAEMON  -- $PARAMS
	echo "."
	;;
  status)
       status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
       ;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0

