# Eric H. Herrin II
# University of Kentucky Mathematical Sciences
# eric@ms.uky.edu, !cbosgd!ukma!eric
#
# Install script for System V Pty driver
SRCDIR=uipc
MODULE=pty
# put the module in the appropriate place
cp ${MODULE}.o /etc/lddrv/${MODULE}.o
chmod 700 /etc/lddrv/${MODULE}.o
chown root /etc/lddrv/${MODULE}.o
chgrp root /etc/lddrv/${MODULE}.o
# add the entry to the /etc/master file
/etc/masterupd -a char init open release close read write ioctl ${MODULE}
# get the assigned device number
MODULEDEVNO=`/etc/masterupd -c ${MODULE}`
if [  ! ${MODULEDEVNO} ]
then 
	echo "${MODULEDEVNO} cannot be added to the /etc/master file"
	exit 1
fi
# NOTE!!!!!!!!!!!!!!
# To change the number of ptys, change the value in number-ptys.h.
# VVVVVVV
PTYCNT=`grep NUMBER_OF_PTYS $SRCDIR/number-ptys.h | \
sed -e "s/#define//" -e "s/NUMBER_OF_PTYS//" -e "s/ //g"`
echo "PTYCNT=$PTYCNT" > Remove
cat Remove.orig >> Remove
# make the pty device files
echo "Making pty pseudo device files"
cnt=0
for x in p q r s t u v w x y z
do
	for y in 0 1 2 3 4 5 6 7 8 9 a b c d e f
	do
		i=$x$y
		if [ $cnt -ge ${PTYCNT} ] 
		then 
			break
		fi
		/etc/mknod /dev/pty$i c ${MODULEDEVNO}\
 `expr $cnt + $PTYCNT`
		/etc/mknod /dev/tty$i c ${MODULEDEVNO} $cnt
		chown root /dev/tty$i /dev/pty$i
		chgrp root /dev/tty$i /dev/pty$i
		chmod 0666 /dev/tty$i /dev/pty$i
		cnt=`expr $cnt + 1`
	done
done

echo "Putting entry into InstDrv"
# put an entry in /etc/lddrv/InstDrv for ${MODULE}
V=`cat Version`
echo "Name=${MODULE} driver: Version ${V}" 	>> /etc/lddrv/InstDrv
echo "File=${MODULE}"     			>> /etc/lddrv/InstDrv
echo "Comment=Pseudo tty and socket driver" 	>> /etc/lddrv/InstDrv
(
cd /etc/lddrv
# allocate and load the module
echo "Allocating pty module with lddrv"
./lddrv -av -o ${MODULE}.o ${MODULE}
if [ $? -eq 0 ]
then
	echo "Driver ${MODULE} successfully loaded"
else
	echo "Error: Driver ${MODULE} failed loading stage"
	exit 1
fi
)

# load the ${MODULE} at boot time
echo load the ${MODULE} at boot time
echo ${MODULE} >> /etc/lddrv/drivers

echo "Loading support/socket_init to /etc/daemons"
cp support/socket_init /etc/daemons/socket_init
chmod 700 /etc/daemons/socket_init
chown root /etc/daemons/socket_init
/etc/daemons/socket_init

echo "Pseudo tty drivers are now installed"

echo "Loading Libraries and /usr/include/uipc files"

cp lib/libuipc.a /usr/lib/libuipc.a
chown bin /usr/lib/libuipc.a
chgrp bin /usr/lib/libuipc.a
chmod 644 /usr/lib/libuipc.a
echo "/usr/lib/libuipc.a loaded..."

UIPC=/usr/include/uipc
mkdir ${UIPC}
chown bin ${UIPC}
chgrp bin ${UIPC}
chmod 755 ${UIPC}
(cd uipc; find * -print | cpio -pvdum ${UIPC} )
chmod 644 ${UIPC}/*
chown bin ${UIPC}/*
chgrp bin ${UIPC}/*


message -i `cat Fullname|sed 's/\$/\\\\n/'`
exit 0
