#!/bin/sh
# Univel
# mkETHdsk
# Shell script to create an IHV LAN driver diskette from a downloaded
# bulletin board compressed cpio archive.
#set -x

trap "exit 1" 1 2 15

Restore ()
{
	cd $SPATH
	cp ${archive}.ORIG $archive
	rm -f /tmp/flist.$PPID
}


makeIhvFlop ()
{
	hit=1
	HERE=`pwd`
	until [ $hit -eq 0 ]
	do
	>/tmp/flist.$PPID
	clear
	echo "______________________________________________________________"
	echo "--------------------------------------------------------------"
	echo "             IHV LAN FLOPPY DEVICE SELECTION SUBMENU       "
	echo "______________________________________________________________"
	echo "--------------------------------------------------------------\n"
        
	j=1
	for floppy in diskette1 diskette2
	do
		/bin/grep $floppy /etc/device.tab >/dev/null 2>&1
		if [ $? -eq 0 ]
		then
			echo "	$j)	 $floppy"
			rdev=`/bin/grep $floppy /etc/device.tab | cut -d: -f2`
			bdev=`/bin/grep $floppy /etc/device.tab | cut -d: -f3`
			formcmd=`/bin/grep $floppy /etc/device.tab | cut -d: -f5 | cut -d\" -f16`
			echo "$j:$floppy:$rdev:$bdev:$formcmd" >>/tmp/flist.$PPID
			j=`expr $j + 1`
		fi
	done
	echo "\nEnter Selection for floppy device [default=1,q=quit]? \c"
	read sel
	if test $sel
	then
		if [ $sel -eq "q" ] 
		then
			Restore
			exit 0
		fi
		
		/bin/grep diskette$sel /tmp/flist.$PPID >/dev/null
		if [ $? -ne 0 ]
		then
			echo "\n"
			echo "		Invalid Selection..\n"
			sleep 2
		else
			RDEV=`/bin/grep diskette$sel /tmp/flist.$PPID | cut -d: -f3`
			BDEV=`/bin/grep diskette$sel /tmp/flist.$PPID | cut -d: -f4`
			DRVNAME=`/bin/grep $RDEV /tmp/flist.$PPID | cut -d: -f2`
			FORMCMD=`/bin/grep $DRVNAME /tmp/flist.$PPID | cut -d: -f5`
			if [ $DRVNAME = "diskette1" ]
			then
				MNTPT2=/Disk_A
			else
				MNTPT2=/Disk_B
			fi
			hit=0
		fi
	else
		RDEV=`/bin/grep diskette1 /tmp/flist.$PPID | cut -d: -f3`
		BDEV=`/bin/grep diskette1 /tmp/flist.$PPID | cut -d: -f4`
		FORMCMD=`/bin/grep diskette1 /tmp/flist.$PPID | cut -d: -f5`
		MKFSCMD=`/bin/grep diskette1 /etc/device.tab | awk '{print $18,$19,$20,$21,$22,$23,$24}'`
		echo $MKFSCMD
		MNTPT2=/Disk_A
		hit=0
	fi
    done
    clear
    echo "\n\n"
    echo "Please insert a floppy diskette\n"
    echo "Press the ENTER key to continue \c"
    read return
    echo "\n\n"
    echo "Making s5 filesystem ...Please wait\n"
    /usr/sbin/mkfs -Fs5 $RDEV 2370:200 
    if [ $? -ne 0 ]
    then
	echo "mkfs ERROR: Formatting ${DRVNAME}...please wait\n"
	$FORMCMD
	/usr/sbin/mkfs -Fs5 $RDEV 2370:200 
    fi
    /sbin/mount -Fs5 $BDEV $MNTPT2
    if [ $? -ne 0 ]
    then
	echo "mount FAILED!\n"
	echo "Press the RETURN key to exit \c"
	read dummy
	Restore
        exit 1

   fi
   cd $SPOOL/$SEL
   echo "\nTransferring package contents to floppy filesystem...Please wait\n"
   cd $CPIOD
   /bin/find . -print | /bin/cpio -pmud $MNTPT2
   if [ $? -ne 0 ]
   then
	echo "		An error has occurred with copy\n"
	echo "		Press the RETURN key to exit \c"
	read dummy
	Restore
	exit 1
   fi
   cd $HERE
   sync;sync
   /sbin/umount $MNTPT2
   ret=$?
   if [ $ret -ne 0 ]
   then
	echo "umount ERROR: $rc \n"
        echo "		Press the RETURN key to continue \c"
        read dummy
   fi
   clear
   echo "\n\n\n\n\n\n\n"
   echo "		----------------------------------------------------------\n"
   echo "			The IHV LAN floppy diskette"
   echo "			has successfully completed.\n"
   echo "			Please LABEL and date this diskette\n"
   echo "		----------------------------------------------------------\n"
   echo "\n"
   echo "		Press the RETURN key to continue \c"
   read ans
   /bin/rm -f /tmp/*$$
   cd $SPATH
}

#main starts here

PPID=$$

unpack ()
{
	#mv the file to file.Z and uncompress the cpio archive
	#
	here=`pwd`
	clear
	echo "\n\n\n\n"
	echo "		Enter directory path of IHV LAN archive \n"
	echo "		$here is the default: \c"
	read path
	if test $path
	then
	
		if [ "$path" = "$here" ]
		then
			SPATH=$here
		else
       		        SPATH=$path
		fi
	else
		SPATH=$here
	fi
	cd $SPATH
        ls ihv* >/dev/null
	if [ $? -ne 0 ]
        then
		echo "\nCannot locate IHV files \n"
	        echo "Press the ENTERY key to exit \c"
		read return
		exit 1
	fi
	echo "\n\n"
	echo "Unpacking files....please wait \n"
	for i in `ls ihv*`
	do
		type=`file $i | awk '{print $2}'` >/dev/null 2>&1
		if [ $type = "compressed" ]
		then
       			 archive=$i
			 /bin/cp $archive $archive.ORIG
			 /bin/mv $archive $archive.Z
			 break;
      		fi
	done
      
	/bin/uncompress $archive
	/bin/mkdir net
	/bin/mv $archive net
	cd net
	CPIOD=`pwd`
	/bin/cpio -icvudB < $archive
	/bin/rm $archive
	cd $SPATH
}

unpack
imdone=1
while [ $imdone -ne 0 ]
do
	makeIhvFlop
        cd $SPATH
done
