#!/bin/bash
#
# This script start and stop t38modem .
# It is driven by a configuration file sourced by this shell
# and called /etc/default/t38modem.

### BEGIN INIT INFO
# Provides:          t38modem
# Required-Start:    $syslog $network
# Required-Stop:     $syslog $network
# Should-Start:      $local_fs 
# Should-Stop:       $local_fs 
# X-Start-Before:    hylafax
# X-Stop-After:	     hylafax
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop the T38 Fax-oder-IP modems
# Description:       Start  t38modem. 
### END INIT INFO




. /etc/default/t38modem


 
case "$1" in
          start)
                    COUNTER=0
                    CURRENT_PORT=${START_PORT}
 
                    while [  $COUNTER -lt $T38_PROCESSES ]; do
                              COMMAND="/usr/bin/t38modem -tt --no-h323 --sip-register $SIPUSER@$SIPSERVER,$SIPPASS --sip-old-asn --ptty +/dev/ttyT38-$COUNTER --route modem:.*=sip:<dn>@${SIPSERVER} --route sip:.*=modem:<dn>" 
 
                              exec $COMMAND > /dev/null 2>&1 &
 
                              PID=$!
 
                              echo "Starting t38modem /dev/ttyT38-$COUNTER with pid $PID (pidfile /var/run/t38modem/$COUNTER)"
 
                              echo $PID > /var/run/t38modem/$COUNTER
 
                              COUNTER=$[ COUNTER + 1 ]
                              CURRENT_PORT=$[ CURRENT_PORT + 1 ]
                    done
          ;;
          stop)
                    echo -n "Stopping t38modem pids: "
 
                    for pidfile in `ls /var/run/t38modem`
                    do
                              _PID=`cat /var/run/t38modem/$pidfile`
                              kill -9 $_PID
                              rm /var/run/t38modem/$pidfile
                              echo -n $_PID" "
                    done
                    echo " Done"
          ;;





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




          *)
                    echo "Usage: $0 {start|stop}" >&2
                    exit 1
          ;;
esac
 
exit 0


