#!/bin/sh
#
# Initscript for mwavem
#
# Written by Thomas Hood <jdthood_AT_yahoo.co.uk>
# This file is part of the Debian mwavem package

MYNAME="/etc/init.d/mwavem"
MODULENAME=mwave
DAEMONNAME=mwavemd
DAEMONPATHNAME="/usr/sbin/mwavemd"
PIDFILE=/var/run/mwavemd.pid
MWAVELOADERPATHNAME="/usr/sbin/mwavem"
MWAVELOADERCONFPATHNAME="/etc/mwavem/mwavem.conf"
MWDEV="/dev/mwave"
SERDEV="/dev/ttyS1"
LINK="/dev/Mwave-modem"

case "$1" in

start)
	# Do not remove $LINK here because daemon may already be running.

	if [ ! -f $DAEMONPATHNAME ]; then
		echo "$MYNAME: $DAEMONPATHNAME not found.  Exiting."
		exit 0
	fi

	if [ ! -f $MWAVELOADERPATHNAME ]; then
		echo "$MYNAME: $MWAVELOADERPATHNAME not found.  Exiting."
		exit 0
	fi

	# The following prints a message on the console ending with a newline
	modprobeoutput=`modprobe -v $MODULENAME`
	modprobestatus=$?

	echo -n "Starting Mwave modem: "

	if [ $modprobestatus -ne 0 ]; then
		echo -n "(module $MODULENAME did not load) "
	else  # modprobe returned 0
		echo $modprobeoutput | grep Using | grep $MODULENAME >> /dev/null 2>&1
		if [ $? -eq 0 ]; then
			echo -n "(module:) $MODULENAME "
		else
			echo -n "(module $MODULENAME already loaded) "
		fi
	fi

	# Check for the mwave device
	if [ ! -c $MWDEV ]; then
		echo "$MYNAME: error: $MWDEV does not exist.  Exiting."
		exit 1
	fi

	# Check for the serial device
	if [ ! -c $SERDEV ]; then
		echo "$MYNAME: error: $SERDEV does not exist.  Exiting."
		exit 1
	fi

	# Start $DAEMONPATHNAME which creates $PIDFILE containing its own process i.d.
	# and symbolically links $LINK to $SERDEV if and when the modem is ready.
	start-stop-daemon --start --pidfile $PIDFILE --startas $DAEMONPATHNAME --background -- $SERDEV $LINK $PIDFILE $MWAVELOADERPATHNAME $MWAVELOADERCONFPATHNAME $MWDEV
	if [ $? -ne 0 ]; then
		# start-stop-daemon has already printed an error message
		exit 1
	fi
	# start-stop-daemon succeeded in starting $DAEMONPATHNAME

	# Wait for either:
	# thirty seconds to elapse, or
	# $DAEMONPATHNAME to exit without creating $LINK (due to some error), or
	# $LINK to be created, indicating that the modem is ready to use.
	daemon_started=0
	for w in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 timeout; do
		if [ "$w" = "timeout" ]; then
			echo "(timed out waiting for daemon to start)."
			exit 1
		fi
		if [ -h $LINK ]; then
			echo "(daemon:) mwavemd."
			# We're done starting
			exit 0
		fi
		sleep 1
		pidof_mwavemd=`pidof -x $DAEMONPATHNAME`
		if [ -z "$pidof_mwavemd" ]; then
			if [ $daemon_started -ne 0 ]; then
				echo "(daemon aborted)."
			else
				echo "(daemon failed to start)."
			fi
			exit 1
		else
			daemon_started=1
		fi
	done
	exit 2   # We shouldn't reach here
	;;

stop)
	echo -n "Stopping Mwave modem: "
	start-stop-daemon --stop --quiet --pidfile $PIDFILE || kill `pidof -x $DAEMONPATHNAME` >> /dev/null 2>&1
	if [ $? -ne 0 ]; then
		# start-stop-daemon hasn't printed a message because of "--quiet"
		echo -n "(no daemon running) "
	else
		# daemon was sent signal 15.  Wait for it to die.
		for w in 0 1 2 3 giveup; do
			if [ "$w" = "giveup" ]; then
				echo -n "(timed out waiting for daemon to exit; KILLing daemon:) mwavemd "
				kill -9 `pidof $DAEMONPATHNAME` >> /dev/null 2>&1
				break
			fi
			if [ ! -f $PIDFILE ]; then
				echo -n "(daemon:) mwavemd "
				break
			fi
			sleep 1
		done
	fi
	kill -9 `pidof $MWAVELOADERPATHNAME` >> /dev/null 2>&1  # Just to make sure
	rm -f $PIDFILE   # Just to make sure
	rm -f $LINK   # Just to make sure

	lsmod | grep -q -s "^$MODULENAME" >> /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "(and module not loaded)."
		exit 0
	fi
	# Module is present
	for w in 0 1 2 3 giveup; do
		if [ "$w" = "giveup" ]; then
			echo "(could not remove module $MODULENAME)."
			exit 1
		fi
		rmmod $MODULENAME >> /dev/null 2>&1
		if [ $? -eq 0 ]; then
			echo "(module:) $MODULENAME."
			exit 0
		fi
		sleep 1
	done
	exit 2   # We shouldn't reach here
	;;

restart|reload|force-reload)
	$0 stop && $0 start
	;;

check)
	echo -n "Checking Mwave modem: "
	exitstatus=0
	(pidof -x $DAEMONPATHNAME >> /dev/null 2>&1 && test -f $PIDFILE ) && echo -n "mwavemd OK; " || ( echo -n "mwavemd NOT OK; " ; exitstatus=1 )
	(pidof $MWAVELOADERPATHNAME >> /dev/null 2>&1 && test -e /proc/mwave ) && echo "mwavem OK." || ( echo "mwavem NOT OK." ; exitstatus=0 )
	exit $exitstatus
	;;

*)
	echo "Usage: $MYNAME {start|stop|restart|reload|force-reload|check}"
	exit 1

esac
