#!/bin/sh
# script to make specialix port nodes-(post-installation)
# This version prevents duplicate entries in inittab.
#set -x

#if [ `whoami` != "root" ] 
#then
#   $echo "Must be super-user to run this script."
#   exit 1
#fi
major=123
gotvals=0

while [ $gotvals -eq 0 ]
do
    /bsd43/bin/echo -n "Enter first port#-default is 32 for /dev/tty{aA}32: "
    read firstport
    if [ -z "$firstport" ] 
    then
       firstport=32
    fi

    /bsd43/bin/echo -n "Enter last port#-default is 127 for /dev/tty{aA}127: "
    read lastport
    if [ -z "$lastport" ] 
    then
       lastport=127
    fi

    if [ $firstport -gt $lastport ] 
    then
       /bsd43/bin/echo "Bad values-try again"
    else
       gotvals=1
    fi
    port=$firstport
done

# remove digiboard and SIO entries from inittab to prevent dup id fields

#
#          Remember to put the /etc back in front of these two files
#

sed -e '/^d/d' inittab|sed -e '/^h/d'|sed -e '/^i/d' > inittab.2

if [ -f inittab.old ]
then
   /bsd43/bin/echo -n "File 'inittab.old' exists.  May I overwrite it? [y/N] "
   read overwrite
fi

if [ $overwrite = "Y" -o $overwrite = "y" ]
then
   rm inittab.old
else
   echo "Please save the file 'inittab.old' by hand, then re-run this script."
   exit 1
fi

mv inittab inittab.old
mv inittab.2 inittab

while [ $port -le $lastport ]       # Big Loop
do
   #  First make the ttya port-minor is same as port#
   if [ -c /dev/ttya$port  ] 
	then
     /bsd43/bin/echo /dev/ttya$port exists
   else
     /etc/mknod /dev/ttya$port c $major $port
   fi

   #  Now make the ttyA port-this is for modem control
   minor=`expr $port + 128`
   if [ -c /dev/ttyA$port  ] 
	then
     /bsd43/bin/echo /dev/ttyA$port exists
   else
     /etc/mknod /dev/ttyA$port c $major $minor
   fi

   #  math for first (ID) field of /etc/inittab- $l
   k=`expr $port / 26`
   k=`expr $k + 97`
   k3=`expr $k / 64`
   k2=`expr 64 \* $k3`
   k2=`expr $k - $k2`
   k2=`expr $k2 / 8`
   k1=`expr $k % 8`
   k=`expr $k - 97`
   j=`expr $port - $k \* 26`
   j=`expr $j + 97`
   j3=`expr $j / 64`
   j2=`expr 64 \* $j3`
   j2=`expr $j - $j2`
   j2=`expr $j2 / 8`
   j1=`expr $j % 8`
#  convert to ASCII
   l="\\0$k3$k2$k1\\0$j3$j2$j1"

#  $l is the ID field. This statement changes "co" to "zz"-don't want 2 co's
   if [ "$l" = "\\0143\\0157" ] 
   then
      l="\\0172\\0172"
   fi

#  this if-statement prevents duplicate ID fields in /etc/inittab

x="^"`echo $l`

   if [ `egrep -c $x /etc/inittab` -lt 1  ] 
   then
      echo "$l:234:off:/etc/getty ttya$port dx_9600 none LDISC0" >> /etc/inittab
   else
      /bsd43/bin/echo "/etc/inittab entry for ttya$port already exists."
   fi

   port=`expr $port + 1`
done       # of the loop
exit 1

 
/bsd43/bin/echo "Done making extra ports."
