#!/bin/sh
#
# zaptel        This shell script takes care of loading and unloading \
#               Zapata Telephony interfaces
# chkconfig: 35 92 9
# 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/rc.d/init.d

# Source function library.
. $initdir/functions || exit 0

# Source zaptel configuration.
[ -f /etc/sysconfig/zaptel ] && . /etc/sysconfig/zaptel

if [ -z "${MODULES}" ]; then 
	# Populate defaults if not present
	# MODULES="tor2 wct4xxp wct1xxp wcte11xp wcfxo wctdm ztdummypll" 
	MODULES="ztdummypll"
fi

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

# check if /etc/sysconfig/zaptel exists. If not exit
if [ ! -f /etc/sysconfig/zaptel ]; then
        echo "Cannot start: /etc/sysconfig/zaptel is missing."
        exit 0
fi

# Check that telephony is "yes". If not exit
if [ ${TELEPHONY} = "no" ]; then
        echo "Cannot start: TELEPHONY=no in /etc/sysconfig/zaptel."
        exit 0
fi

# check of /usr/sbin/ztcfg exists. If not exit
if [ ! -f /usr/sbin/ztcfg ]; then
        echo "Cannot start: /usr/sbin/ztcfg is missing."
        exit 0
fi

# check of /etc/zaptel.conf exits. If not exit
if [ ! -f /etc/zaptel.conf ]; then
        echo "Cannot start: /etc/zaptel.conf is missing."
        exit 0
fi

if [ "${DEBUG}" = "yes" ]; then
    ARGS="debug=1"
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
	action "Loading zaptel framework: " modprobe zaptel
	rc=$?
	if [ "$rc" = "1" ]; then
	    exit 1
	fi
	STRING="Waiting for zap to come online:"
	echo -n "$STRING "
	if [ "${RHGB_STARTED}" != "" -a -w /etc/rhgb/temp/rhgb-console ]; then
	    echo -n "$STRING " > /etc/rhgb/temp/rhgb-console
	fi
	
	TMOUT=10 # max secs to wait
	while [ ! -d /dev/zap ] ; do
	    sleep 1
	    TMOUT=`expr $TMOUT - 1`
	    if [ $TMOUT -eq 0 ] ; then
		failure $"$STRING"
		if [ "${RHGB_STARTED}" != "" -a -w /etc/rhgb/tmp/rhgb-console ]; then
		    echo_failure > /etc/rhgb/temp/rhgb-console
		    [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --details=yes
		fi
		echo
		exit 1
	    fi
	done
	success $"$STRING"
	if [ "${RHGB_STARTED}" != "" -a -w /etc/rhgb/tmp/rhgb-console ]; then
	    echo_success > /etc/rhgb/temp/rhgb-console
	fi
	echo

	echo "Loading zaptel hardware modules:"
	for x in $MODULES; do 
		eval localARGS="\$${x}_ARGS"
		action "Loading ${x}:" modprobe ${x} ${ARGS} ${localARGS}
	done
	sleep 3
	if [ ! -e /proc/zaptel/1 ]; then
		echo "No functioning zap hardware found in /proc/zaptel, loading ztdummypll"
		action "Loading ztdummypll:" modprobe ztdummypll
	fi
	action "Running ztcfg: " /usr/sbin/ztcfg
	RETVAL=$?

        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/zaptel
        ;;
  stop)
        # Unload drivers
        echo "Unloading zaptel hardware drivers:"
	for x in $RMODULES; do 
	    action "Unloading ${x}:" rmmod ${x}
	done
	action "Removing zaptel module:" rmmod zaptel
	RETVAL=$?

        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/zaptel
        ;;
 restart)
	$0 stop
	$0 start
	;;
  reload)
	action "Reloading ztcfg: " /usr/sbin/ztcfg
	RETVAL=$?
	;;
  *)
        echo "Usage: zaptel {start|stop|restart|reload}"
        exit 1
esac

exit $RETVAL

