#!/bin/sh
#
# Use the Central Data supplied program to modify the baud rate extension
# factor for the given device and baud rate
#
KillProcs()
{
    Dev=$1
    procs=`ps -t $Dev | tail +2 | awk '{print $1}'`
    if [ -n "$procs" ] ; then
	kill -1 $procs
	sleep 10
    fi
    procs=`ps -t $Dev | tail +2 | awk '{print $1}'`
    if [ -n "$procs" ] ; then
	kill -9 $procs
	sleep 5
    fi
}

SetModem()
{
    InDev=$1
    OutDev=$2
    Baud=$3
    PortMon=$4
    PortService=$5
    echo $InDev $OutDev $Baud $PortMon $PortService
    /usr/sbin/pmadm -d -p $PortMon -s $PortService
    sleep 8
    KillProcs $InDev
    KillProcs $OutDev
    /opt/dp/modem/setmodem -v /opt/dp/modem/telebit.$Baud.com /dev/$OutDev $Baud
    /opt/dp/modem/setmodem -v /opt/dp/modem/telebit.$Baud.com /dev/$OutDev $Baud
    sleep 3
    /usr/sbin/pmadm -e -p $PortMon -s $PortService

}


ProgModem()
{
    Dev=$1
    Baud=$2
    case $Dev in
     /dev/*)	;;
     *)		Dev=/dev/$Dev;;
    esac
    OutDev=`echo $Dev | sed 's,/dev/,,'`
    Out=`echo $OutDev | sed 's,cua/,,'`
    PortMon=stsmon

    if [ "$Out" = "a" -o "$Out" = "b" ] ; then
	In=$Out
	PortMon=zsmon
    elif [ -c /dev/term/17 ] ; then
	In=`expr $Out - 16`
    elif [ -c /dev/term/9 ] ; then
	In=`expr $Out - 9`
    else 
	In=`expr $Out - 3`
    fi
    if [ "$PortMon" = "zsmon" ] ; then
	PortService=tty$In
    else
	PortService=$In
    fi
    InDev=term/$In

    if [ -c $Dev -a $Baud -ne 0 ] ; then
	SetModem $InDev $OutDev $Baud $PortMon $PortService >> /tmp/setmodem.out 2>&1
    fi
}

cat /dev/null > /tmp/setmodem.out
#
# Program all PPP modems
#
if [ -f /etc/dp/conf ] ; then
    grep '^MODEM' /etc/dp/conf | sed 's/^.*DEV=\([^ 	]*\).*BAUD=\([0-9]*\).*$/\1 \2/' | \
    while read Dev Baud ; do
	ProgModem $Dev $Baud
    done
fi
cat /tmp/setmodem.out
rm /tmp/setmodem.out
