#!/bin/sh
#
# zaptel        This shell script takes care of loading and unloading \
#               Zapata Telephony interfaces
# chkconfig: 2345 11 70
# description: The zapata telephony drivers allow you to use your linux \
# computer to accept incoming data and voice interfaces
#
# config: /etc/sysconfig/zaptel

initdir=/etc/init.d

#
# Determine which kind of configuration we're using
#
system=redhat  # assume redhat
if [ -f /etc/debian_version ]; then
    system=debian
fi

# Source function library.
if [ $system = redhat ]; then
    . $initdir/functions || exit 0
fi

# Source zaptel configuration.
if [ $system = debian ]; then
    [ -f /etc/default/zaptel ] && . /etc/default/zaptel
	LOCKFILE=/var/lock/zaptel
elif [ $system = redhat ]; then
    [ -f /etc/sysconfig/zaptel ] && . /etc/sysconfig/zaptel
	LOCKFILE=/var/lock/subsys/zaptel
fi

if [ -z "${MODULES}" ]; then 
	# Populate defaults if not present
	MODULES="ztdynamic ztd_eth" 
fi

# Reverse list for un-loading; don't change
for x in $MODULES; do 
    RMODULES="$x $RMODULES"
done

# Check that telephony is up.
#[ "${TELEPHONY}" = "yes" ] || exit 0

[ -f /sbin/ztcfg ] || exit 0

[ -f /etc/zaptel.conf ] || exit 0

if [ "${DEBUG}" = "yes" ]; then
	ARGS="debug=1"
fi

# check if install lines exist in modprobe.conf
grep -q "ztcfg" /etc/modprobe.conf
if [ $? -eq 0 ]; then
	echo
	echo install lines exist in /etc/modprobe.conf for zaptel devices!
	echo Remove them before attempting to use zaptel!
	echo
	exit 0
fi

RETVAL=0

# See how we were called.
case "$1" in
  start)
	# Load drivers
	rmmod wcusb >& /dev/null
	rmmod wcfxsusb >& /dev/null
	rmmod audio >& /dev/null
	if [ $system = debian ]; then
	    echo -n "Loading zaptel framework: " 
	    modprobe zaptel >& /dev/null && echo -n "done"
	    echo "."
	elif [ $system = redhat ]; then
	    action "Loading zaptel framework: " modprobe zaptel
	fi
	echo -n "Waiting for zap to come online..."
	TMOUT=10 # max secs to wait
	while [ ! -d /dev/zap ] ; do
 		sleep 1
		TMOUT=`expr $TMOUT - 1`
		if [ $TMOUT -eq 0 ] ; then
			echo "Error: missing /dev/zap!"
			exit 1
		fi
	done
	echo "OK"
	echo -n "Loading zaptel hardware modules:"
	for x in $MODULES; do 
		if modprobe ${x} ${ARGS} >& /dev/null; then
			echo -n " $x"
		fi
	done
	#sleep 3
	#if [ ! -e /proc/zaptel/1 ]; then
	#	echo "No functioning zap hardware found in /proc/zaptel."
	#fi
	if [ $system = debian ]; then
	    echo -n "Running ztcfg: " 
	    /sbin/ztcfg >& /dev/null && echo -n "done"
	    echo "."
	elif [ $system = redhat ]; then
	    action "Running ztcfg: " /sbin/ztcfg
        #    action /sbin/ztcfg		
	fi
	#if [ $system = redhat ]; then
        #    action "Running ztcfg: " /sbin/ztcfg
        #    action /sbin/ztcfg
	#fi
	RETVAL=$?

	[ $RETVAL -eq 0 ] && touch $LOCKFILE

	if [ $FONULATOR = YES ]; then
                for i in `ls /etc/redfone.d`; do
                        site=`basename $i .conf`
                        echo -n $"Starting fonulator for $site: "
                        /usr/local/bin/fonulator -s /etc/redfone.d/$i &
                        RETVAL=$?
                        [ $RETVAL -eq 0 ] && {
                           touch /var/lock/subsys/fonulator
                           success $"fonulator $site"
                        }
                        echo
                done
	fi
	;;
  stop)
	# Unload drivers
	echo "Unloading zaptel hardware drivers:"
	echo "RMODULES=$RMODULES"
	if [ $FONULATOR = YES ]; then
	        echo "Stopping spans..."
	        /sbin/ztcfg -s
	        echo "Sleeping for 3 seconds..."
	        sleep 3
	fi
        echo -n "Unloading modules: "
	rmmod zttranscode
 	for x in $RMODULES; do 
		if rmmod ${x} >& /dev/null; then
			echo -n " $x"
		fi
	done
	echo "."

	if [ $system = debian ]; then
	    echo -n "Removing zaptel module: " 
	    rmmod zaptel >& /dev/null && echo -n "done"
	    echo "."
	elif [ $system = redhat ]; then
	    action "Removing zaptel module: " rmmod zaptel
	fi
	RETVAL=$?

	[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
	;;
  restart)
	$0 stop
	$0 start
	;;
  reload)
	if [ $system = debian ]; then
	    echo -n "Reloading ztcfg: "
	    /sbin/ztcfg >& /dev/null && echo -n "done"
	    echo "."
	elif [ $system = redhat ]; then
	    action "Reloading ztcfg: " /sbin/ztcfg
	fi
	RETVAL=$?
	;;
  *)
	echo "Usage: zaptel {start|stop|restart|reload}"
	exit 1
esac

exit $RETVAL

