#! /bin/sh
#
# Manage Firebird Super Server (without too much care)
#
# PLEASE READ FOR YOUR OWN GOOD
#
# There is NO WAY for install/uninstall/upgrade debian package scripts
# to check if there are any clients connected to the server.
# Therefore this is YOUR JOB to CLEANLY SHUTDOWN all the CLIENTS.
# Without that You take the RISK that Your DATABASES can be CORRUPTED
# while this package is upgraded/removed/reinstalled.
#
# The /etc/init.d/firebird script ALSO can NOT be treated as CLEAN way
# of managing the server IF any CLIENTS are CONNECTED.
# for Super Server managment You should use /usr/lib/firebird/bin/ibmgr
# utility (unfortunatelly it requries admin password so cannot be used
# by the scripts).
# For Classic Server it is enough to disconnect all the clients.
#
# If You know the WAY OF SOLVING this problem please write to me:
# greg@sente.pl (remember that it must work w/o user/password pairs,
# user interaction, find / and other ugly hacks).

# TECHNICAL NOTE: this file exists in both - super and classic flavours.
#     It is intended (not a bug). If there's no super flavour binaries
#     available - it just exits. No harm is done.		

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/lib/firebird/bin
DAEMON=/usr/lib/firebird/bin/ibmgr
NAME=ibmgr
DESC=FireBird
USER=firebird
GROUP=firebird

test -f $DAEMON || exit 0

INTERBASE=/usr/lib/firebird
if [ -r /etc/firebird/firebird.env ]; then
    . /etc/firebird/firebird.env
fi
export INTERBASE

set -e

case "$1" in
  start)
	# have some checks first
	# in case someone accidently removed FB's log file
	if [ ! -f /var/log/firebird.log ]; then
	    touch /var/log/firebird.log
	    chmod 750 /var/log/firebird.log
	    chown firebird.firebird /var/log/firebird.log
	fi
	# now really get to the job
	echo -n "Starting $DESC: "
	start-stop-daemon --start --quiet \
		--chuid firebird:firebird --exec $DAEMON -- -start -forever \
		>/dev/null 2>&1
	echo "$NAME."
	;;
  stop)
	# it is not clear whether we need to know USER:PASS here to stop the server
	# and what happens to client's processes
	echo -n "Stopping Firebird: "
#	start-stop-daemon --oknodo --stop --quiet --pidfile /var/run/$NAME.pid \
#		--chuid firebird:firebird --exec $DAEMON -- -shut
#		>/dev/null 2>&1

	# stop the guardian (else he'll fork new servers when 
	#   we kill the running ones)
	start-stop-daemon --oknodo --stop --quiet --signal HUP \
	    --chuid firebird:firebird --name ibguard
	# stop the server (not sure if this is fully secure!)
	start-stop-daemon --oknodo --stop --quiet --signal HUP \
	    --chuid firebird:firebird --name ibserver
	echo "done"
	;;
  #reload)
	#
	#	If the daemon can reload its config files on the fly
	#	for example by sending it SIGHUP, do it here.
	#
	#	If the daemon responds to changes in its config file
	#	directly anyway, make this a do-nothing entry.
	#
	# echo "Reloading $DESC configuration files."
	# start-stop-daemon --stop --signal 1 --quiet --pidfile \
	#	/var/run/$NAME.pid --exec $DAEMON
  #;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo "FireBird Super Server will be restarted..."
	$0 stop	
	$0 start
	#start-stop-daemon --stop --quiet --pidfile \
	#	/var/run/$NAME.pid --exec $DAEMON
	#sleep 1
	#start-stop-daemon --start --quiet --pidfile \
	#	/var/run/$NAME.pid --exec $DAEMON	
	#echo "$NAME."
	
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
