#!/sbin/runscript

# this file should work to start linesrv-2.1 as well as linesrv-3
# it probably doesn't conform fully to gentoo norms and it's not
# perfect at all... but linesrv likes to be treated this way and
# on my system it seems to work fine.
# Feel free to improve it, send patches to linecontrol@srf.ch.

# created by S. Fuchs, linecontrol@srf.ch for Gentoo GNU/Linux

# /etc/conf.d/linesrv: sample content:
#---- (remove 1 comment character # to get the defaults)
## to start a bit faster but don't check for successful
## start, say 'FASTSTART=yes', else say 'FASTSTART=no'
#FASTSTART=yes
#
## sys config:
## the pid-filename may not contain any whitespaces (space, tab, ...)!
#CFGFILE=/etc/linesrv.conf
#BINNAME=linesrv
#LINESRV=/usr/local/sbin/$BINNAME
#
#----

depend() {
    need net logger
	use mysql dns
}

find_pidfile() {
    PIDFILE=`grep pidfile $CFGFILE | sed 's/pidfile//' | sed 's/[ \t\n]//g'`
    PID=`cat $PIDFILE 2> /dev/null`
}

start() {
    ebegin "Starting linesrv"

    find_pidfile

    if [ -e $PIDFILE ] ; then
	if [ -z "`ps -p $PID | grep $PID`" ] ; then
	    rm $PIDFILE
	else
	    eend 1 "Linesrv seems to be running already"
	fi
    fi

    $LINESRV -c $CFGFILE > /dev/null 2>&1 &
    if [ "$FASTSTART" != "yes" ] ; then
		sleep 1
		if [ ! -e $PIDFILE ] ; then
		    eend 2 "Probably failed starting linesrv: no pidfile found after start"
		else
		    PID=`cat $PIDFILE 2> /dev/null`
		fi
    fi
    eend $? "Failed starting linesrv"
}

stop() {
    ebegin "Terminating linesrv"

    find_pidfile

    if [ ! -z "$PID" ] ; then
		kill -QUIT $PID
		sleep 3
		if [ -e $PIDFILE ] ; then
		    kill -9 $PID > /dev/null 2>&1
	    	if [ -e $PIDFILE ] ; then
				rm $PIDFILE
		    fi
		fi				
    fi
	# uhh... need to improve the following line...
    eend $? "Failed terminating linesrv"
}
