#!/usr/bin/bash
#
# refdbctl: RefDB control script for starting, stopping, reconfiguring, and
# restarting of refdbd
# usage: refdbctl (start|stop|reload|restart)

# markus@mhoenicka.de 2001-7-22
# $Id: refdbctl.in,v 1.7.2.1 2004/12/10 22:07:20 mhoenicka Exp $

# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# NB we must use bash for "cd -" to work

# full path to the daemon binary
REFDBD='/usr/bin/refdbd'

# full path of the file the daemon writes its PID to
PIDFILE='/var/run/refdbd.pid'

# default return value is 0 (all fine). Other values are: 1 (error),
# 2 (nothing to do), 3 (unknown or missing command)
RETVAL=0

# we need at least one argument (only the first one is used actually)
ARGV="$@"
if [ "x$ARGV" = "x" ] ; then 
    ARGS="help"
fi

# find the saved process ID (strip leading and trailing junk)
if [ -f $PIDFILE ]; then
    SAVEDPID=`cat $PIDFILE | sed 's/^ *\([0-9]*\).*/\1/'`
else
    SAVEDPID=#
fi

# make sure the process with this ID is an instance of refdbd
# NB run grep case-insensitive as some Windozes return uppercase process names
PID=`ps ax | grep -i "^ *$SAVEDPID.*[r]efdbd" | sed 's/^ *\([0-9]*\).*/\1/'`

# see whether the daemon is running
if [ "x$PID" != "x" ] ; then
    RUNNING=1
else
    RUNNING=0
fi


# in order to work around a sqlite bug on Windows/Cygwin, we have to
# start the daemon from the root directory (this does not hurt in any way).
# On Cygwin, the refdbdrc config file has to use the relative path starting
# from / to the database directory. On other systems you can use either
# the absolute or the relative path
case $1 in
    start)
	if [ $RUNNING -eq 1 ]; then
	    echo "$0 $1: $REFDBD already running (PID=$PID)"
	    RETVAL=2
	else
	    cd /
	    if $REFDBD ; then
		echo "$0 $1: bibliography tool application server started"
	    else
		echo "$0 $1: bibliography tool application server could not be started"
		RETVAL=1
	    fi
	    cd -
	fi;;
    stop)
	if [ $RUNNING -eq 0 ]; then
	    echo "$0 $1: $REFDBD is not running"
	    RETVAL=2
	else
	    if kill $PID ; then
		echo "$0 $1: bibliography tool application server stopped"
	    else
		echo "$0 $1: bibliography tool application server could not be stopped"
		RETVAL=1
	    fi
	fi;;
    reload)
	if [ $RUNNING -eq 0 ]; then
	    echo "$0 $1: $REFDBD is not running"
	    RETVAL=2
	else
	    if kill -HUP $PID ; then
		echo "$0 $1: bibliography tool application server reconfigured"
	    else
		echo "$0 $1: bibliography tool application server could not be reconfigured"
		RETVAL=1
	    fi
	fi;;
    restart)
	if [ $RUNNING -eq 0 ]; then
	    echo "$0 $1: $REFDBD is not running"
	    RETVAL=2
	else
	    if kill -TERM $PID ; then
		cd /
		if $REFDBD ; then
		    echo "$0 $1: bibliography tool application server restarted"
		else
		    echo "$0 $1: bibliography tool application server could not be restarted"
		    RETVAL=1
		fi
		cd -
	    else
		echo "$0 $1: bibliography tool application server could not be stopped"
		RETVAL=1
	    fi
	fi;;
    *)
	echo "usage: $0 (start|stop|reload|restart)"
	RETVAL=3;;
esac

exit $RETVAL