
#ident	"%Z%sadmin/admin/menu/usermgmt:%M%	%I%"

#menu# add a sxt device to the system for shl layer manager
#help# 
#help#	Addsxt adds a group of sxt devices to the system. These devices
#help#	are to be used through the shl layer manager. Each shl layer user
#help#	requires eight sxt devices. You can enter more than one user at a time.

checker(){
count=-1
cat /tmp/sxt.part | while read i
do
count=`expr $count + 1`

# Get the first and last entry for the group in case there is an error.
if [ $count -eq 0 ]
then echo $i >/tmp/first
     tail -1 /tmp/sxt.part > /tmp/last
fi

# Check that the device is of type character special.
	if [ ! -c $i ]
	then echo ERROR: $i is not a character special device
             echo 1 > /tmp/errflag
	     echo 1 > /tmp/grperr
	fi

# Check that the major device number is 7.
	set `ls -l $i`
	maj=$5
	mindev=$6
	if [ "$5" != "7," ]
	then set `ls $i`
             echo ERROR: $1 has the wrong major device. Should be 7, is $maj
	     echo 1 > /tmp/errflag
	     echo 1 > /tmp/grperr
	fi

# Check that the device has the correct minor device number.
	set `ls $i` 
	x=`basename $1`
	if [ $x -lt 10 ]
	then min=$count
	else min=`expr \( \( $x / 10 \) \* 8 \) + $count`
	fi
	if [ $mindev -ne $min ]
	then echo ERROR: $1 has the wrong minor device number. Should be $count, is $mindev.
	     echo 1 > /tmp/errflag
	     echo 1 > /tmp/grperr
	fi

done
}

checkamount(){
for i in 0 1 2 3 4 5 6 7
do
	case $1 in
		[1-9]|10) x=/dev/sxt/0`expr $1 - 1`$i
			  echo /dev/sxt/0`expr $1 - 1`0 > /tmp/first 
		    	  echo /dev/sxt/0`expr $1 - 1`7 > /tmp/last ;;
		*) x=/dev/sxt/`expr $1 - 1`$i 
			  echo /dev/sxt/`expr $1 - 1`0 > /tmp/first 
		    	  echo /dev/sxt/`expr $1 - 1`7 > /tmp/last ;;
	esac
	y=`/bin/ls $x 2> /dev/null`
	if [ "$y" = "" ]
	then echo "ERROR: $x cannot be found in /dev/sxt directory."
	     wrongamt=1
	else `/bin/ls $x >> /tmp/sxt.part`
	fi
done
}
	
# If there was an error in any of the existing devices, remove the group
# that is in error and rerun script.
removeit(){
if [ -s /tmp/first ]
then echo "\n\nPlease remove devices `cat /tmp/first` through `cat /tmp/last`.\n\n"
     rm -f /tmp/first /tmp/last
fi
}

# Remove all of the temporary files
cleanup(){
rm -f /tmp/sxt.part /tmp/errflag /tmp/group /tmp/first /tmp/last /tmp/sxt.list /tmp/grperr
}

trap 'rm -f /tmp/sxt.list /tmp/sxt.part /tmp/group /tmp/grperr /tmp/errflag /tmp/first /tmp/last; exit 1' 2
# Beginning of main program
tput clear
echo
echo "Determining and verifying the number of users currently supported"
echo "on your system."
echo "\nPlease wait . . .\n"

# Initialize variables
wrongamt=0
any=1

# Check to see if /dev/sxt directory has been made
if [ ! -d /dev/sxt ]
then
	mkdir /dev/sxt
	chmod 775 /dev/sxt
	chown bin /dev/sxt
	chgrp bin /dev/sxt
fi

cd /dev/sxt
/bin/ls | sort -n > /tmp/sxt.list

# If the there are no sxt devices, we don't need to do any of the computation.
if [ ! -s /tmp/sxt.list ]
then any=0
     end=0
     num=0
else
	end=`tail -1l /tmp/sxt.list`
	rem=`expr \( $end - 7 \) % 10`
	num=`expr \( \( $end - 7 \) / 10 \) + 1`
	if [ $rem -ne 0 ]
	then num=`expr $num + 1`
	fi
fi

# If there aren't any sxt devices on the system, tell the user.
echo
if [ $any -eq "0" ]
then 
	echo
	echo "Your system does not have any sxt named devices."
else
	group=1
	while [ $group -le $num ]
	do
		checkamount $group
		if [ $wrongamt -eq 0 ]
		then checker
   		     rm -f /tmp/sxt.part
		     if [ -s /tmp/grperr ]
		     then removeit
			  rm -f /tmp/grperr
                     fi
		else echo 1 > /tmp/errflag
		     wrongamt=0
		     removeit
		fi
	group=`expr $group + 1`
	done
	if [ -s /tmp/errflag ]
	then echo "\n\nAborting due to reported errors. Please rerun script after"
	     echo "removing the necessary files."
	     cleanup
 	     exit
	fi
	echo
	echo "Your system has the sxt named devices to support" $num "user(s)."
fi
echo "\n\n"
echo "Enter the number of shl users you wish to add to or create on your"
echo "system:  \c"
read add

# Make sure we get a good response.
while :
do
	case $add in
		0)	exit;;
		[1-9]|[1-9][0-9])
			tot=`expr $add + $num`
			if [ $tot -lt 0 -o $tot -gt 88 ]
			then echo "Please enter a valid number: \c"
			else break
			fi;;
		*)	echo "Please enter a valid entry: \c";;
		esac
read add
done

# Make the necessary nodes.
while [ $add -gt "0" ]
do
	if [ $end -eq "0" ]
	then start=0
	     minor=0
	else start=`expr $end + 3`
	     minor=`expr \( $start / 10 \) \* 8`
	fi

# Determine the position to start removing from if someone hits delete.
	part=`expr $start / 10`
	case $part in 
		[0-9]) remove="0"$part;;
		[1-9][0-9]) remove=$part;;
	esac
	trap 'rm -f /dev/sxt/$remove* /dev/sxt$remove* /tmp/sxt.list /tmp/sxt.part /tmp/group /tmp/grperr /tmp/errflag; exit 1' 2
	for i in 0 1 2 3 4 5 6 7
	do
		name=`expr $start + $i`
		end=$name
		curmin=`expr $minor + $i`
		case $name in 
			[0-9]) name="00"$name;;
			[1-9][0-9]) name="0"$name;;
		esac
		echo "making device /dev/sxt/"$name
		/etc/mknod /dev/sxt/$name c 7 $curmin
		echo "linking device /dev/sxt"$name
		/bin/ln /dev/sxt/$name /dev/sxt$name
	done
add=`expr $add - 1`
done

# Clean up
cleanup

echo "\n\n\n"
echo "sxt named devices have been created."
