#!/bin/sh
#
#       /etc/rc.d/init.d/dominoSrv2
#
# Starts the domino server 2-d instance
#
# chkconfig: 345 85 15
# description: Domino Server Instance 2
# processname: dominoSrv2
# Source function library.
. /etc/init.d/functions

###################################################################
# some improvements
# from http://www.xminc.com/linux/dominoscale.php

ulimit -n 20000
#echo "100 1200 128 512 15 5000 500 1884 2">/proc/sys/vm/bdflush
#echo "300 400 500"> /proc/sys/vm/freepages

# For a Web Server:
#echo "10240 61000" > /proc/sys/net/ipv4/ip_local_port_range
#echo "134217728" > /proc/sys/kernel/shmmax
#echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time

# On a LAN
# echo 0 > /proc/sys/net/ipv4/tcp_timestamps
# disable TCP timestamps

#echo 0 > /proc/sys/net/ipv4/tcp_window_scaling
# disable window scaling

#echo 0 > /proc/sys/net/ipv4/tcp_stack
# disable selective acks
####################################################################
# DOM_HOME is the variable that tells the script where the Domino Data resides
DOM_HOME=/local/notesdata2

# DOM_USER is the Linux account used to run the Domino server
DOM_USER=notes2

# DOM_PROG is the location of the Domino executables
DOM_INST=/opt/ibm/inst2
DOM_PROG=$DOM_INST/lotus/bin
DOM_PROG_OTHER=$DOM_INST/lotus/notes/latest/linux
TEMP=$DOM_HOME/tmp
TMP=$TEMP
export TEMP TMP


# Set the prog variable for the status line of the code
prog=$DOM_PROG/server
PATH=$PATH:$DOM_HOME
export PATH
# Does the lock file exist?
config () {
	if [ -f $DOM_HOME/.jsc_lock ]; then
		rm -f $DOM_HOME/.jsc_lock
	fi
# Domino tune kernel options
	$DOM_PROG_OTHER/tunekrnl

}

# Let's start the server
start() {
	echo -n "Starting domino: "
	config
	cd $DOM_HOME 
echo " ====== Instance 2 ======= ";
echo ; echo " ====== before starting server process ======= ";
#echo -e "  \n\n" | su $DOM_USER -c "$DOM_PROG/server -jc -c > /dev/null 2>&1 &"
su $DOM_USER -c "$DOM_PROG/server -jc -c > /dev/null 2>&1 &"
echo ; echo " ====== after starting server process ======== ";
#	su $DOM_USER -c "$DOM_PROG/server -jc -c > /dev/null 2>&1 &"
	return 0
}

# Let's stop the server
stop() {
	echo -n "Stopping domino server: "
	su $DOM_USER -c "$DOM_PROG/server -q"
	sleep 5
	echo -n "Is the controller running?  If so, stopping it: "
	check_controller='netstat -an | grep " LISTEN" | grep 2050'
	if [ "$check_controller" ]
	then
		echo -n "Domino controller appears to be running - stopping..."
		echo Y | su $DOM_USER -c "$DOM_PROG/server -jc -q";echo ''
		sleep 5
	fi
	return 0
}

# Let's stop and restart the server.
restart() {
	echo -n "Beginning restart script..."
	check_controller='netstat -an | grep " LISTEN" | grep 2050'
	if [ "$check_controller" ]
	then
		echo -n "Domino controller appears to be running - stopping..."
		echo Y | su $DOM_USER -c "$DOM_PROG/server -jc -q";echo ''
		sleep 5
	else
		check_server='ps -A | grep replica'
		if [ "$check_server" ]
		then
			echo -n "Domino server appears to be running - stopping..."
			su - $DOM_USER -c "$DOM_PROG/server -q"
			sleep 5
		fi
	fi
	echo -n "Starting Domino Controller and Server..."
	config
	cd $DOM_HOME 
	su - $DOM_USER -c "$DOM_PROG/server -jc -c > /dev/null 2>&1 &"
	sleep 5
	echo -n "Domino Server restart complete..."
} 

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
restart)
	restart
	;;
status)
	status $prog 
	;;
*)
	echo "Usage: domino {start|stop|status|restart}"
	exit 1
esac
