#!/bin/sh
# $Id: template,v 1.3 2003/05/21 17:47:00 ldv Exp $
### the_Arioch@nm.ru - i wanna /dev/modem symlink be automagic
#
# slmodemd		Smartlink softmodem driver daemon
#
# chkconfig: 345 90 10
# description:	slmodemd is SmartLink softmodem drivers daemon
#		
#		
# processname: slmodemd

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

PIDFILE=/var/run/slmodemd.pid
LOCKFILE=/var/lock/subsys/slmodemd
RETVAL=0

#Arioch - since slmodem now features SysFS interface - lets engage it
SYSFS_DRIVERS=/sys/class/{slusb,slamr}
# i do not know what to do with ALSA modems - and how do they show up;
# also i do not have usb modem and hope it will just follow slamr pattern.
# I assume if someone have built-in modem (slamr) and at the same time external
# one (slusb) - he prefers USB one.


#Now, what env-vars respect ls ? can i unexpectedly get verbose output without -l argument?
#Something like LS_COLORS or like DirCmd in DOS/Windows, i need to clear before invoking ls ?


SYSFS_DEVICES=
#in case it was borrowed from parent process
[[ -d /sys ]] && SYSFS_DEVICES=`for fold in /sys/class/{slusb,slamr}; do ls $fold ; done 2>/dev/null`

#i guess, for 2.4 kernels one could have one more list instead of sysfs
#parsing dmesg, or returned by slmodemd --SomeQueryOption, or from /etc/SomeWhere
## End of Arioch (yet)

start()
{
	action "Starting slmodem daemon: " start-stop-daemon --quiet --start -b -m --pidfile $PIDFILE --exec /usr/sbin/slmodemd -- --country=RUSSIA
	RETVAL=$?

  #     Arioch:
  #	1st existing slmodem device i'd like to see symlinked as /dev/modem - if /dev/modem is not occupied already
  #     But let returning value stay the daemon's return code
        if [ $RETVAL -eq 0 ]; then
           if [[ ! -e /dev/modem  ]]; then 
	      for smth in "$SYSFS_DEVICES"; do
                  [[ -e /dev/$smth ]]  && ( ln -s /dev/$smth /dev/modem ; break )
              done;
	   fi 
	fi
	# A rake, i placed here: imagine someday DEVFS or UDEV would move slamr0 to, say,
	#  /dev/modems/slamr0 or to any other subfolder - "[[ -e /dev/$smth ]]" would fail.
	# To avoid stepping upon the rake someday, we maybe can search throughout /dev.
	# /sys/class/(DRIVER)/(DEVICE)/dev contain major and minor and this can be 
	# used to find driver file wherever under /dev - but i don't know any easy way. 
  # End of Arioch (yet)
       
	return $RETVAL
}

stop()
{
	action "Stopping slmodem daemon: " start-stop-daemon --stop --quiet --pidfile $PIDFILE slmodemd
	RETVAL=$?
        
  # Arioch: let's wipe away /dev/modem, if it is our own link
        if [ $RETVAL -eq 0 ]; then
           local TARGET
 	# i'm afraid local is command, thus can affect value of $?
           TARGET=`readlink -e /dev/modem` && [[ -c $TARGET ]] && TARGET=`basename $TARGET`
	# see rooks in start().  
	
	#Now, how should i test is word is contained in list ?
	   [ $? -eq 0 ] && ( echo $SYSFS_DEVICES | grep -G "\<$TARGET\>" >/dev/null 2>/dev/null ) && rm -f /dev/modem


        #Neither slmodemd nor start-stop-daemon don't care about PID :-(
	   rm -f $PIDFILE
        fi
  # End of Arioch (yet)
	
	return $RETVAL
}

restart()
{
	stop
	sleep 1
	start
}

reload()
{
	msg_reloading slmodemd
	start-stop-daemon --stop --pidfile $PIDFILE -s HUP -- slmodemd
	RETVAL=$?
	return $RETVAL
} 

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user root -- slmodemd
		RETVAL=$?
	# Arioch (next line, list of devices):
	        [[ -n "SYSFS_DEVICES" ]] && echo "  Known devices: $SYSFS_DEVICES"
	# End of Arioch. "finishing, closing and going home" 
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL


