#!/bin/sh
#
#
# Lotus Domino rc-script /etc/init.d/domino
# For Debian GNU/Linux, but also usable on other distributions.
#
# Usage:
# /etc/init.d/domino {start|stop|restart|kill|help}
#
# Written by Jens Vogel in 2004 - 2007
# Inspired by written IBM documentation
#
# Modified by Danilo Dellaquila in 2007
# K-Gigas Computers S.L. <ddellaquila@gmail.com>
# Thanks Jens for your script.
# I've just put some chkconfig stuff,
# and added the Spanish language support.
#
#############################################################################
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#############################################################################
#
# chkconfig: 345 95 5
# description: This script is used to start the domino
# server as a background process.
#
# http://www.desktux.nl/
# desktux@desktux.nl


# Variables
###########

# Remember to adapt everyting to your installation!
# The values below work only on default installations!
domino_user="notes"
domino_group="notes"
data_dir="/local/notesdata"
if [ -d "/opt/lotus/bin" ]; then
	bin_dir="/opt/lotus/bin"	# For Domino 6.5.x or older
elif [ -d "/opt/ibm/lotus/bin" ]; then
	bin_dir="/opt/ibm/lotus/bin"	# For Domino 7
else
	bin_dir="/usr/local/bin"	# If your bin_dir is somwhere else
fi

# Uncomment just one of the following
# NOTE: if you use a logfile and wish to rotate the logfile you *must* use the
#       logrotate-option "copytruncate" or stop the server before rotating!
output="/dev/tty12"
#output="/var/log/domino.log"

# The password file must exist and have the permissions
# 0400 $domino_user:$domino_group!
# If you have no server password just touch an empty file.
passwd_file="${data_dir}/.domino.pwd"

# Thanks to Giorgio Fedon for pointing this out:
# In case you didn't set up your language environment correctly and have
# problems with characters specific to your language, uncomment one of the
# following or enter your own value.
#LANG="de_DE@euro"
#LANG="fr_FR@euro"
#LANG="it_IT@euro"
#LANG="nl_NL@euro"
#LANG="es_ES@euro"
export LANG

# See how we are called and take action
#######################################

function start() {
	# Check for the password file
	if [ ! -f $passwd_file ]; then
		echo -e "\aFatal error: no password file ${passwd_file} found!"
		exit 1
	fi
	# Just preventive: correct the rights
	chmod 0400 $passwd_file
	chown $domino_user:$domino_group $passwd_file
	# Start the server
	echo -n "Starting Domino server: "
	su - $domino_user -c "cd ${data_dir}; cat ${passwd_file}|${bin_dir}/server" >> $output 2>&1 &
	echo "done. Output is redirected to ${output}."
}

function stop() {
	# Stop the server
	echo "Stopping Domino server:"
	su - $domino_user -c "cd ${data_dir}; ${bin_dir}/server -q"
	echo "Done."
}

case $1 in
	start)
		start;;
	stop)
		stop;;
	restart)
		stop
		start
		;;
	kill)
		# Kill the server if stopping soesn't work.
		# Also write an NSD file.
		echo -n "Killing Domino server: "
		su - $domino_user -c "cd ${data_dir}; ${bin_dir}/nsd -kill"
		echo "Done."
		;;
	help)
		# Give help for those in need...
		echo -e "\nLotus Domino rc-script"
		echo -e "/etc/init.d/domino {start|stop|restart|kill|help}\n"
		echo "Options:"
		echo "--------"
		echo -e "start\tStarts the Domino Server."
		echo -e "stop\tStops the Domino Server."
		echo -e "restart\tRestarts the Domino Server."
		echo -e "kill\tKills the Domino Server if 'stop' doesn't work.\n\tAlso writes a nsd logfile in ${data_dir}."
		echo -e "help\tShow this message.\n"
		echo -e "Remember to adapt the variables to your installation if you didn't run a default\nDomino installation!\n"
		;;
	*)
		# Small usage instructions
		echo "Usage: /etc/init.d/domino {start|stop|restart|kill|help}"
		;;
esac

exit 0
