#!/bin/sh
#!/bin/bash
# firebird    This is the init script for starting up the Firebird
#               server
#
# chkconfig: - 64 36
# description: Starts and stops the default instance of Firebird
# processname: fbguard
# pidfile: /var/run/firebird/default.pid
# description: Start/Stop firebird database server

# To run more instances of firebird:
#   Copy /usr/lib/firebird somewhere
#   Copy this script under a new name
#   Change INSTANCE and FIREBIRD below (all instance names should be unique)
#   Edit the copied firebird.conf to change at least RemoteServicePort

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

INSTANCE=default

# No changes needed below for multiple instances
USER=firebird
PIDFILE=/var/run/firebird/$INSTANCE.pid
FULLNAME="Firebird server [$INSTANCE]"
EXE=/usr/sbin/fbguard
RETVAL=0
LOCKFILE=/var/lock/subsys/firebird

start()
{
    if start-stop-daemon --start --test --quiet --pidfile "$PIDFILE" \
        --user $USER --exec "$EXE" >/dev/null; then
        action "Starting Firebird:" \
            start-stop-daemon --start --pidfile "$PIDFILE" --chuid $USER --exec "$EXE" \
            -- "-pidfile $PIDFILE -start -daemon -forever"
    else
        msg_already_running Firebird
        passed "Firebird startup"
        echo
    fi
    RETVAL=$?
    [ "$RETVAL" -ne 0 ] || touch "$LOCKFILE"
    return $RETVAL
}

stop()
{
    action "Stopping Firebird:" \
        start-stop-daemon --stop --pidfile "$PIDFILE" --user $USER
    RETVAL=$?
    [ "$RETVAL" -ne 0 ] || rm -f -- "$LOCKFILE"
    return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	if [ -f $PIDFILE ]
	then
		pid=`cat $PIDFILE`
    	ps -p $pid >/dev/null 2>&1
		RETVAL=$?
	else
		RETVAL=1
	fi
	if [ $RETVAL -eq 0 ]
	then
		echo "$FULLNAME is running, pid $pid"
	else
		echo "$FULLNAME is stopped."
	fi
	;;
  restart|reload)
	$0 stop
	sleep 1
	$0 start
	RETVAL=$?
	;;
	condrestart|condreload)
	if [ -e "$LOCKFILE" ]; then
		$0 restart
	fi
	;;
	condstop)
	if [ -e "$LOCKFILE" ]; then
		$0 stop
	fi
	;;
  *)
	echo "Usage: firebird {start|stop|status|restart|reload|condrestart|condreload|condstop}"
	exit 1
esac

exit $RETVAL

