#!/bin/sh
#
# Author (isdnlog 1.0): 2000/07/26 Jrgen Hammelmann
#
# 2000/07/26 Jrgen Hammelmann<juergen.hammelmann@gmx.de>:
# isdn4linux modified because of isdnlog hanging at boot before ippp devices are set!
# isdnlog is started in a separate script after ippp devices are up!
#
# This script is starting the needed isdnlog daemon
# This script is designed to work in BSD as well as SysV init setups.
#
# You may use the script '/usr/local/bin/isdn' to configure this script
# Look in /usr/doc/isdn4net for samples.
# See http:/www.terminator.net/isdn4net/ for more info.
#
# Version >= 1.1 is only tested on RedHat 6.x
# 
# SysV settings
# chkconfig: 2345 33 98
# description: ISDN for Linux subsystem - isdnlog

usage()
{
    	echo "Usage: $0 {start|stop|restart}"
	echo "(Try 'isdn' for configuration)"
}

# Source ISDN configuration, if available
if [ -f /etc/sysconfig/isdn ] ; then
    . /etc/sysconfig/isdn
else
    I4L_START=no
fi

EXITCODE=1

for x in "1" ; do

    # Check that ISDN is up
    [ ${I4L_START} = "no" ] && break

    if [ $# -lt 1 ] ; then usage ; break ; fi

    action=$1

    case "$action" in

    'start')
	
	# start isdnlog ?
	if [ "$I4L_ISDNLOG" = "yes" ] ; then		
		logpid=""
		if [ -f /var/run/isdnlog.isdnctrl0.pid ] ; then
			logpid=`cat /var/run/isdnlog.isdnctrl0.pid`		
			ps axww | grep -v grep | grep $logpid >& /dev/null
    			if [ $? -ne 0 ] ; then
        			rm -f /var/run/isdnlog.isdnctrl0.pid
        			logpid=""
    			fi
		fi
		if [ -z "$logpid" ] ; then
			echo "Start isdnlog-daemon"
			isdnlog -D -f /etc/isdn/my_isdnlog.conf /dev/isdnctrl0
		        touch /var/lock/subsys/isdnlog
		fi
	fi
	
    ;;

    'stop')

	echo "Shutting down isdnlog"
	
	# Shutdown isdnlog
	if [ -f /var/run/isdnlog.isdnctrl0.pid ] ; then
		logpid=`cat /var/run/isdnlog.isdnctrl0.pid`		
		kill $logpid > /dev/null 2>&1
		rm -f /var/run/isdnlog.isdnctrl0.pid
		rm /var/lock/subsys/isdnlog
	fi

	pid=`ps axww|grep -v grep|grep isdnlog |awk '{print $1}'` 
	if [ -n "$pid" ] ; then 
		echo "Stopping isdnlog ..."
		kill $pid > /dev/null 2>&1
	fi

    ;;

    'status')

    	if [ -f /var/run/isdnlog.isdnctrl0.pid ] ; then
		logpid=`cat /var/run/isdnlog.isdnctrl0.pid`
		ps axww | grep -v grep | grep $logpid >& /dev/null
		if [ $? -ne 0 ] ; then
			echo "isdnlog is running"
		else
			echo "isdnlog is stopped, but /var/run/isdnlog.isdnctrl0.pid still exists"
		fi
	else
		echo "isdnlog is stopped"
	fi

    ;;

    'restart')
	$0 stop
	$0 start
	;;

    'reload')
        ;;

    *)
	usage
	break
	;;

    esac
    EXITCODE=0

done

exit $EXITCODE

