#!/bin/sh

##############################################################################
#                                                                            #
# Copyright (c) 2016 by R1Soft All rights reserved                            #
#                                                                            #
##############################################################################
#
# chkconfig: 2345 99 99
# description: Righteous Software Linux Backup Server
# processname: cdpserver

### BEGIN INIT INFO
# Provides:          cdp-server
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts R1Soft CDP Server
# Description:       Starts R1Soft CDP Server
### END INIT INFO

# cdpserver control script 

INSTALLATION_DIR=/usr/sbin/r1soft

# the path to the PID file
PIDFILE=/var/run/cdp-server.pid
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
CDPSERVER_INSTALL=$INSTALLATION_DIR/
CDPSERVER_CONFIG=$INSTALLATION_DIR/conf/server.conf

CDPSERVER_BIN=${CDPSERVER_INSTALL}/bin
LIB_PATH=${CDPSERVER_INSTALL}/jre/lib/i386/server:${CDPSERVER_INSTALL}/jre/lib/amd64/server
RUN_BUAGENT="./cdpserver ${CDPSERVER_CONFIG}"

# DON'T EDIT PAST HERE #######################################################

PATH=$PATH:/sbin:/usr/sbin

ulimit -d unlimited > /dev/null 2>&1 # data segment
ulimit -f unlimited > /dev/null 2>&1 # file size
ulimit -m unlimited > /dev/null 2>&1 # max memory size


ulimit -n 15000 > /dev/null 2>&1 # open files -- try for 15k
ulimit -n 30000 > /dev/null 2>&1 # open files -- try for 30k 
ulimit -n 50000 > /dev/null 2>&1 # open files -- try for 50k 
ulimit -n `cat /proc/sys/fs/nr_open 2> /dev/null` > /dev/null 2>&1 # open files - checking with the system. 
ulimit -n unlimited > /dev/null 2>&1 # open files -- not sure this works ever on any system. 

ulimit -s unlimited > /dev/null 2>&1 # stack size
ulimit -t unlimited > /dev/null 2>&1 # cpu time
ulimit -v unlimited > /dev/null 2>&1 # virtual memory
umask 077

export LD_LIBRARY_PATH=${LIB_PATH}

if [ -d ${CDPSERVER_BIN} ]; then
	cd ${CDPSERVER_BIN}
fi

STATUSCODE=0
ERROR=0
ARGV="$@"
if [ "x$ARGV" = "x" ] ; then 
	ARGS="help"
fi

checkpid() {
# check for pidfile
if [ -f $PIDFILE ] ; then
	PID=`cat $PIDFILE`
	if [ "x$PID" != "x" ] && kill -0 $PID 2>/dev/null ; then
		STATUS="cdpserver (pid $PID) running"
		RUNNING=1
		STATUSCODE=0
	else
		STATUS="cdpserver (pid $PID?) not running"
		RUNNING=0
		STATUSCODE=1
	fi
else
	STATUS="cdpserver (no pid file) not running"
	RUNNING=0
	STATUSCODE=3
fi
}

verify() {
	if [ ! -f "${INSTALLATION_DIR}/conf/.user_set" ]; then
		echo "You must set the username and password for the default user."
		echo "Run '/usr/bin/r1soft-setup --help' for options"
		exit 6;
	fi
	if [ ! -f "${INSTALLATION_DIR}/conf/server.conf" ]; then
		echo "Error: server.conf missing. Did you specify which CDP product to install?"
		echo "Please contact R1Soft Support (support@r1soft.com)"
		exit 5;
	fi
}

start() {
	verify
	checkpid
	if [ ! -d "${INSTALLATION_DIR}/log" ]; then
		mkdir -p ${INSTALLATION_DIR}/log 2>/dev/null;
	fi
	if [ $RUNNING -eq 1 ]; then
	    echo "$0 $ARG: cdpserver (pid $PID) already running"
	else
		if $RUN_BUAGENT > /dev/null ; then
			echo "$0 $ARG: cdpserver started"
			
			if [ -d "/var/lock/subsys" ]; then
				> "/var/lock/subsys/cdpserver"
			fi
	
		else
			echo "$0 $ARG: cdpserver could not be started"
			ERROR=1
		fi
	fi
}

stop() {
	checkpid
	if [ $RUNNING -eq 0 ]; then
		echo "$0 $ARG: $STATUS"
	else

		TIMEOUT=3600
		PROPFILE=${CDPSERVER_INSTALL}/conf/server.properties

		if [ -f $PROPFILE ] ; then

			TIMEOUTPROP=`grep 'sbm-restart-wait-timeout' $PROPFILE | awk -F '=' '{ print $2 }'`

			if [ -z "$TIMEOUTPROP" ] ; then
				TIMEOUT=3600
			else
				TIMEOUT=$(($TIMEOUTPROP * 60))
			fi

		fi

		if kill $PID ; then

			echo "Waiting up to $TIMEOUT seconds for server to terminate"

			count=$TIMEOUT
			while ([ $count != 0 ]) do
				count=`/usr/bin/expr $count - 1`
				if ps -p $PID > /dev/null ; then
					echo -n .
					sleep 1
				else
					count=0
				fi
			done 

			echo ""

			if ps -p $PID > /dev/null ; then
				echo "$0 $ARG: cdpserver could not be stopped"
				ERROR=1
			else
				echo "$0 $ARG: cdpserver stopped"

				if [ -f "/var/lock/subsys/cdpserver" ]; then
					rm -f "/var/lock/subsys/cdpserver" 2>/dev/null
				fi
			fi

		else
			echo "$0 $ARG: cdpserver could not be stopped"
			ERROR=1
		fi
	fi
}

restart() {
	checkpid
	if [ $RUNNING -eq 0 ]; then
		echo "$0 $ARG: cdpserver not running, trying to start"
		if $RUN_BUAGENT > /dev/null ; then
			echo "$0 $ARG: cdpserver started"
		else
			echo "$0 $ARG: cdpserver could not be started"
			ERROR=1
		fi
	else
		stop
		start
	fi
}

status() {
	checkpid
	echo "$0 $ARG: $STATUS"
	exit $STATUSCODE
	continue
}

help() {
	echo "usage: $0 (start|stop|restart|status|help)"
	cat <<EOF

start      - start cdpserver
stop       - stop cdpserver
restart    - stop cdpserver if running then start again, 
             if cdpserver is not running it is started
status     - print status
help       - this screen

EOF
	ERROR=1
}


# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		;;
	restart)
		restart
		;;
	verify)
		verify
		;;
	*)
		help
		;;
esac

exit $ERROR
