#!/bin/sh

# drpadmin - adminstrative tool for adding and removing portservers,
#    tty devices, and configuration files.

# $Id: drpadmin,v 1.22 2001/08/08 20:42:08 jamesp Exp $

CONFIGDIR="/etc/conf/digi"
CONFIGFILE="/etc/conf/digi/drp.conf"
RPDAEMON="/sbin/drpd"
TTY="drp"

ttyname=tty
prname=pr
cuname=cu

# driver prefix
NAME="drp"

#
# Verify super-user privilege.
#

if [ '!' -w /etc/passwd ]
then
    echo "Must be root to use $0"
    exit 2
fi

#
# Define return values
#
: ${OK=0} ${FAIL=1}

#
# Initialize variables
#
PATH=/bin:/usr/bin:/etc:$PATH
list=
tmp=/tmp/${NAME}.$$

exitval=OK

#
# Clean up and exit after signals
#
trap 'rm -f tmp* ; exit 1' 1 2 3 15

#
# Remove temp files and exit with the status passed as argument
#
# Usage: cleanup status
# References: $tmp indicates prefix of files to remove
# Notes: cleanup exits with whatever value it is passed.
#
cleanup()
{
    trap '' 1 2 3 15
    [ -n "$tmp" ] && rm -f $tmp*
    exit $1
}

#
# add a PortServer entry
#
add_ps()
{
# grab the last ps number
i=0
psn=0
while read n rest
do
    if [ $n -gt $i ]
    then
	break
    fi
    i=`expr $i + 1`
    psn=$i
done < $CONFIGFILE

while :
do
    printf "\nEnter the node name or IP address: "
    read node

    while :
    do
	printf "Enter the number of ports: "
	read ndev
	if [ "$ndev" -ge 1 -a "$ndev" -le 64 ] 2>/dev/null
	then break
	fi
	printf "\n**** Please enter a number from 1 to 64\n\n"
    done

    while :
    do
	printf "Enter the tty device ID (only 2 chars allowed) : "
	read devid
	case $devid in
	[0-9A-Za-z]|[0-9A-Za-z][0-9A-Za-z])
	    ;;
	*)  printf "\n**** Please enter 1 or 2 alphanumeric characters\n\n"
	    continue
	    ;;
	esac
	if [ \! -c /dev/${ttyname}${devid}01 ]
	then
	    break
	fi
	printf "\n**** That ID is already used, please try another ID\n\n"
    done

    while :
    do
	printf "\nIf the network connection between your HP-UX system\n"
	printf "and the remote unit includes a WAN link slower than 1 MBit,\n"
	printf "see the drpd.1m manual page and enter the WAN speed here : "
	read wan
	if [ -z "$wan" ] || s=`$RPDAEMON -s "$wan" 2>&1` 
	then 
	    break
	fi
	printf "\n**** "
	expr "$s" : '.*:\(.*\)'
    done

    printf "\nThe following unit will be configured,\n"
    printf "$psn\t$node\t$ndev\t$devid\t$wan\n"
    printf "\nIs this correct (y to add or x to abort) ? "
    read ans
    if [ $ans = 'x' ]
    then
	return
    fi
    if [ $ans = 'y' ]
    then
	printf "$psn\t$node\t$ndev\t$devid\t$wan\n" >> $CONFIGFILE
	sort +n $CONFIGFILE > $tmp
	mv $tmp $CONFIGFILE
	mkdevices $psn $ndev $devid
	$RPDAEMON ${psn} $node $wan > /dev/null 2>&1 &
	return
    fi
done
}

#
# mkdevices - make the realport devices
#
# $1 == portserver number
# $2 == number tty ports
# $3 == device id
#
mkdevices()
{
printf "Creating Digi RealPort Devices, Please wait... "
# make the daemon device
psn=$1
nports=$2
devid=$3

major=`/etc/lsdev -d drp | tail -1 | awk '{ print $1 }'`
if [ $? != 0 ]
then
    printf "RealPort Driver is not installed "
    return
fi

mknod /dev/drp${psn} c $major ${psn}
mknod /dev/drpmon${psn} c $major `expr ${psn} + 2048`

chmod 0600 /dev/drp${psn} /dev/drpmon${psn}
chgrp root /dev/drp${psn} /dev/drpmon${psn}

>/tmp/mktty.${TTY}
>/tmp/init.${TTY}
>/tmp/ttytype.${TTY}

i=0
pstart=1
base=`expr ${psn} \* 4096 + 4096`
while [ $i -lt $nports ]
do
    case $pstart in
    ?) p=0$pstart;;
    ??) p=$pstart;;
    esac

    ttyminor=`expr $base + $i`
    prminor=`expr $base + $i + 512`
    cuminor=`expr $base + $i + 1024`

    (
    echo "mknod /dev/${ttyname}${devid}${p} c $major $ttyminor"
    echo "mknod /dev/${prname}${devid}${p} c $major $prminor"
    echo "mknod /dev/${cuname}${devid}${p} c $major $cuminor"
    ) >>/tmp/mktty.${TTY}

    echo "${devid}${p}:234:off:/etc/getty -h" \
	 "${ttyname}${devid}${p} H" >>/tmp/init.${TTY}
    
    echo "vt100 ${ttyname}${devid}${p}" >>/tmp/ttytype.${TTY}

    ttyminor=`expr $ttyminor + 1`
    i=`expr $i + 1`
    pstart=`expr $pstart + 1`
done

(
echo "chmod 0666 /dev/${ttyname}${devid}[0-9][0-9]"
echo "chmod 0666 /dev/${prname}${devid}[0-9][0-9]"
echo "chmod 0666 /dev/${cuname}${devid}[0-9][0-9]"
echo "chgrp tty /dev/${ttyname}${devid}[0-9][0-9]"
echo "chgrp tty /dev/${prname}${devid}[0-9][0-9]"
echo "chgrp tty /dev/${cuname}${devid}[0-9][0-9]"
) >>/tmp/mktty.${TTY}

sh -x /tmp/mktty.${TTY} > /dev/null 2>&1
cat /tmp/init.${TTY} >> /etc/inittab
cat /tmp/ttytype.${TTY} >> /etc/ttytype

rm -f /tmp/mktty.${TTY} /tmp/init.${TTY} /tmp/ttytype.${TTY}

printf "Done\n"
}

#
# delete a PortServer entry
#
delete_ps()
{
while :
do
    printf "\nEnter the number of the unit to delete: "
    read del_psn
    grep "^$del_psn[^0-9]" $CONFIGFILE
    if [ $? = 1 ]
    then
	printf "Unit not found\n"
	return
    fi
    printf "\nIs this correct (y to delete or x to abort) ? "
    read ans
    if [ $ans = 'x' ]
    then
	return
    fi
    if [ $ans = 'y' ]
    then
	del_ps_entry $del_psn
	break
    fi
done
}

#
# del_ps_entry - delete entry from configs file.
#
# $1 == portserver number to delete
#
del_ps_entry()
{
del_psn=$1

# try to kill the daemon if it is running
fuser -k /dev/drp${del_psn} >/dev/null 2>&1

#if [ $? != 0 ]
#then
#    printf "warning: device /dev/drp${del_psn} does not exist\n"
#fi

del_devid=`grep "^${del_psn}[^0-9]" $CONFIGFILE | awk '{ print $4 }'`

# remove line from config file
grep -v "^$1[^0-9]" $CONFIGFILE > $tmp
mv $tmp $CONFIGFILE

# remove the tty devices and drp device
rm -f /dev/drp${del_psn}
rm -f /dev/drpmon${del_psn}
rm -f /dev/${ttyname}${del_devid}[0-9][0-9]
rm -f /dev/${cuname}${del_devid}[0-9][0-9]
rm -f /dev/${prname}${del_devid}[0-9][0-9]

# remove the tty entries from the inittab file
grep -v "${ttyname}${del_devid}[0-9][0-9]" /etc/inittab >$tmp &&
    mv /etc/inittab /etc/inittab.prev &&
    mv $tmp /etc/inittab

# remove the tty entries from the ttytype file
grep -v "${ttyname}${del_devid}[0-9][0-9]" /etc/ttytype >$tmp &&
    mv /etc/ttytype /etc/ttytype.prev &&
    mv $tmp /etc/ttytype
}

#
# main body
#

if [ ! -f $CONFIGFILE ]
then
    > $CONFIGFILE
fi

while :
do
    printf "\nPlease select an option (a)dd (d)elete (s)how (q)uit : "
    read opt

    case $opt in
    's')
	more $CONFIGFILE
	;;
    'a')
	add_ps
	;;
    'd')
	delete_ps
	;;
    'q')
	exit 0
	;;
    esac
done
