# System Functions Menu
#
# Created:	 05-Sep-87
# Revision Date: 09-Nov-87
#
# Created By:	 Sandy Marshall
# Modified By:	 Sandy Marshall
#

# Edit DEFAULT as needed:
DEFAULT="q"

# Edit TITLE as needed:
TITLE="			System Functions Menu" 

if [ "`echo -n`" = "-n" ]
then
	NLF1=""
	NLF2="\c"
else
	NLF1="-n"
	NLF2=""
fi

PS1="MENU_sh>> "
export PS1

RET='echo "  Type 'exit' to return to menu."'

while true
do

t=`pwd`
s=`date '+%a %h %d, 19%y'`
clear=`tput clear`

echo $clear

# SHOW ==================================================

cat << TBLOCK 
$TITLE

TBLOCK
echo "Present Directory: $t		Date: $s \n"
cat << SHOWEND
	a. Display User Processes	g. Mount a File System
	b. Terminate User Processes	h. Unmount a File System 
	c. Broadcast a Message		i. Check a File System 
	d. Write a Message to a User	j. Go to Single User Mode 
	e. Format a Floppy		k. Shutdown the System 
	f. Go to the Shell		l. Set System Date and Time

	q. Return to previous menu	? Help
SHOWEND
#
# Add additional lines before 'SHOWEND' and
# add corresponding entries in 'SELECT' below:
# <tab><letter(s)><tab><description"	# format for additional lines
#
# Use blank lines for visual grouping.
#

# ENTER ==================================================

echo $NLF1 "

				Select [$DEFAULT] :  $NLF2"
read SELECT

if [ -z "$SELECT" ]
then
	SELECT=$DEFAULT
fi

DEFAULT=$SELECT
RT="0"

echo $clear

# SELECT ==================================================

case $SELECT in
	a)	echo "			Current Users \n\n"
		echo "Login	Terminal   Date Logged In   Idle \n"
		who -u | while read NAME TTY MON DAY TIME IDLE JUNK
		do
			echo "$NAME	$TTY	   $MON $DAY $TIME	    $IDLE"
		done
		echo "\nEnter name of user for processes to be displayed,"
		echo "RETURN will display all processes: \c"
		read x
		echo "\n\n"
		if [ -n "$x" ]
		then	ps -fu $x | pg
			EXERR=$?
		else	ps -ef | pg
			EXERR=$?
		fi
		RT=1					;;

	b)	echo "			Current Processes \n\n"
		ps -ef | pg
		echo "\nEnter process id, (PID), of process to be killed: \c"
		read x
		if  [ -n "$x" ]
		then	kill -9 $x
			EXERR=$?
		else	EXERR=0
		fi					;;

	c)	echo "			Broadcast a Message\n\n"
		echo "Type message and terminate with a Control d\n"
		wall
		EXERR=$?				;;

	d)	echo "			Current Users \n\n"
		echo "Login	Terminal   Date Logged In   Idle \n"
		who -u | while read NAME TTY MON DAY TIME IDLE JUNK
		do
			echo "$NAME	$TTY	   $MON $DAY $TIME	    $IDLE"
		done
		echo "\nEnter the name of the person to write to: \c"
		read x
		if [ -n "$x" ]
		then	echo "\nIndicate which terminal to write to if"
			echo "the user is logged in more than once: \c"
			read y
			echo "\nType message and terminate with a Control d\n"
			if [ -n "$y" ]
			then	write $x $y
				EXERR=$?
			else	write $x
				EXERR=$?
			fi
		else	EXERR=0
		fi					;;

	e)	echo "				Floppy Types\n"
		echo "			Type	Description"
		echo "	 		1	1.2MB High Density"
		echo "	 		2	CUBIX Standard - non-bootable\n"
		echo "Enter device \"Type\" to be formatted: \c"
		read x
		if [ -n "$x" ]
		then	if [ "$x" -eq 1 -o "$x" -eq 2 ]
			then	echo "Is floppy in drive 1 or 2: \c"
				read y
				if [ -n "$y" ]
				then	if [ "$y" -eq 1 -o "$y" -eq 2 ]
					then echo "\n\nInsert floppy into drive $y"
					     echo "To start format type \"go\", anthing else to abort: \c"
					     read z
					     if [ "$z" != go ]
					     then echo "\nFormat aborted"
						  EXERR=0
						  RT=1
					     else echo "\n Formating disk, please wait...\n"
					          if [ "$x" -eq 1 ]
						  then format /dev/rdsk/"$y"s33
						       EXERR=$?
						  else format /dev/rdsk/"$y"s18
						       EXERR=$?
						  fi
					     fi
					else echo "Drive does not exist"
					     EXERR=0
					     RT=1
					fi
				else	echo "No drive selected, format aborted."
					EXERR=0
					RT=1
				fi
			else	echo "Device does not exist"
				EXERR=0
				RT=1
			fi
		else	EXERR=0
		fi						;;

	f)	eval $RET; sh; EXERR=$?				;;

	g)	echo "			File Systems Currently Mounted\n\n"
		mount
		echo "\n\nFile system to be mounted: \c"
		read x
		if [ -n "$x" ]
		then
			echo "Name to be mounted on: \c"
			read y
			if [ -n "$y" ]
			then	mount /dev/dsk/$x /$y
				EXERR=$?
			else	echo "/nMount aborted"
				EXERR=0
				RT=1
			fi
		else	EXERR=0
		fi						;;

	h)	echo "			File Systems Currently Mounted\n\n"
		mount
		echo "\n\nFile system to be unmounted: \c"
		read x
		if [ -n "$x" ]
		then	umount /dev/dsk/$x
			EXERR=$?
		else	EXERR=0
		fi						;;

	i)	echo "Check which file system? \c"
		read x
		if [ -n "$x" ]
		then	fsck /dev/dsk/$x
			EXERR=$?
		else	EXERR=0
		fi						;;

	j)	echo "Grace period (seconds) before shutdown"
		echo "to single user mode: \c"
		read x
		if [ -n "$x" ]
		then	sleep $x
		fi
		echo "If you want to continue type \"yes\", anything else to abort: \c"
		read y
		if [ "$y" != yes ]
		then	echo "Shutdown to single user mode aborted"
			EXERR=0
			RT=1
		else	init s
			EXERR=0
			exit 1
		fi					;;

	l)	t=`date`
		echo "Current date and time: $t \n"
		/etc/setdate
		EXERR=$?				;;

	k)	echo "Enter the grace period in seconds before"
		echo "shutdown, 0 to 300: \c"
		read GRACE
		if [ "$GRACE" -lt 0 -o "$GRACE" -gt 300 ]
		then	/etc/shutdown
		else	/etc/shutdown -g$GRACE
		fi
		EXERR=0					;;

	\?)	sh /usr/bin/function.help
		EXERR=0
		RT=1					;;

	q)	exit $EXERR			 	;;

#
# Add additional lines here and
# add corresponding entries before 'SHOWEND' above:
# <tab><letter(s)>)<tab><cmd pathname> ; EXERR=$? ;;	# format
#

esac

if [ "$EXERR" != "0" ]
then
	echo $NLF1 " Exit error: $EXERR	$NLF2	"
	echo $NLF1 "  Press <RETURN> to continue:  $NLF2"
	read OK
else
	if [ "$RT" = "1" ]
	then
		echo $NLF1 " \n Press <RETURN> to continue:  $NLF2"
		read OK
	fi
fi

done
