#!/bin/sh
#
# I am /usr/sbin/mwavemd
#
# I start the Mwave manager process and exit when the latter exits.
# If and when the M.m. reports that its start-up sequence is complete,
# I test the modem serial port and create a symbolic link to it.
# This serves as the signal to others that the modem is ready to use.
#
# I create a process i.d. file as I start.  When the M.m. exits or if
# I am terminated then I delete my process-i.d. file and the symbolic link
# and exit.
#
# Usually I am only invoked by the mwavem init script.
#
# Written by Thomas Hood <jdthood_AT_yahoo.co.uk>

MYNAME=mwavemd

NICE=/usr/bin/nice
if [ -x $NICE ]; then
	NICECMD="$NICE --5"
else
	NICECMD=""
fi

if [ "$6" = "" ]; then
	echo "$MYNAME: error: Six args are required -- the pathnames (1) of the serial device file, (2) of the symbolic link to the latter to be created, (3) of my process i.d. file to be created, (4) of the Mwave manager executable, (5) of the Mwave manager configuration file and (6) of the mwave device file.  Exiting."
	exit 1
fi

# The serial device used to access the Mwave modem
DEV=$1
if [ ! -c $DEV ]; then
	echo "$MYNAME: error: $DEV is not a character special file.  Exiting."
	exit 1
fi

# The name of the symbolic link to $DEV
LINK=$2

# The name of my process i.d. file
PIDFILE=$3
if [ -e $PIDFILE ]; then
	echo "$MYNAME: error: Process i.d. file $PIDFILE already exists.  If you are sure that no other instance of mwavemd is running, then delete $PIDFILE and try again.  Exiting."
	exit 1
fi

# The Mwave manager executable
MWAVEMPATHNAME=$4
if [ ! -x $MWAVEMPATHNAME ]; then
	echo "$MYNAME: error: $MWAVEMPATHNAME is not an executable file.  Exiting."
	exit 1
fi

# The Mwave manager configuration file
CONFPATHNAME=$5

# The Mwave device file
# At present, mwavem can't use this argument!!!
MWDEV=$6

# To begin with, delete any old link
rm -f $LINK

# Create new pidfile
echo $$ >| $PIDFILE

# Before exiting for any reason, delete link and pidfile.
# Also kill $MWAVEMPATHNAME in case I am exiting because of a SIGTERM.
trap 'rm -f $LINK ; killall -q -v $MWAVEMPATHNAME ; logger -s -t $MYNAME "Cleaning up, unlinking $LINK and exiting. " 2>&1 ; wait ; rm -f $PIDFILE' EXIT

# Here we use awk to do our dirty work.
# Awk will exit when the $MWAVEMPATHNAME process exits and closes its stdout.
# (I assume that $MWAVEMPATHNAME exits soon after reporting an "ERROR" but
# that it continues to run (without forking) after reporting that
# the modem has been "STARTED".)
$NICECMD $MWAVEMPATHNAME $CONFPATHNAME | awk -W interactive 'BEGIN { print "Started the Mwave manager ..." ; exitstatus=101 } { print } /STARTED/ { print "... successful.  Now checking for modem device file ..." ; if ( system("test -c '"$DEV"'") == 0 ) { system("ln -sf '"$DEV"' '"$LINK"' ") ; print "... successful.  Modem is available at '"$LINK"'." ; exitstatus=100 } else { print "... failed.  Modem device file not found." ; system("killall -q '"$MWAVEMPATHNAME"' ") ; exitstatus=103 ; exit } } /ERROR/ { exitstatus=102 } END { exit exitstatus }' - | logger -s -t $MYNAME 2>&1

# Get the exit status of awk
exitstatus=${PIPESTATUS[1]}

case "$exitstatus" in
	101)
		echo "$MYNAME: Error: The Mwave manager exited without starting the modem."
		exit 101
		;;
	102)
		echo "$MYNAME: Error: The Mwave manager exited reporting an ERROR."
		exit 102
		;;
	103)
		echo "$MYNAME: Error: The Mwave manager has been killed because the modem did not respond."
		exit 103
		;;
	100)
		# Normal exit
		exit 0
		;;
	*)
		echo "$MYNAME: Error: awk exited with status $exitstatus."
		exit 1
esac

# (Note that the trap command removes the link and the pidfile.)

