# Basic Utilities Menu
#
# Created:	 05-Sep-87
# Modified Date: 09-Nov-87
#
# Created By:	 Sandy Marshall
# Modified By:	 Sandy Marshall

# Edit DEFAULT as needed:
DEFAULT="q"

# Edit TITLE as needed:
TITLE="			Basic Utilities Menu"

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

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
	Directory Functions		File Functions

	a. List Directory	        f. Display a File
	b. Display Long Directory	g. Move a File
	c. Change Directory		h. Rename a File
	d. Make a Directory		i. Make a Copy of a File
	e. Remove a Directory		j. Copy a File to a Directory
					k. Remove a File

			Miscellaneous Functions

			l. Change Password
			m. Who is using the system?
			n. Current Time

		q. Return to Main 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 "Directory of \c"
		pwd
		echo ""
		ls -FC | pg
		EXERR=$?					;;

	b)	echo "Directory of \c"
		pwd
		echo ""
		ls -l | pg
		EXERR=$?					;;

	c)	echo "Current directory: \c"
		pwd
		echo "Enter new directory name: \c"
		read x
		if [ -n "$x" ]
		then 	if [ -d "$x" ]
			then	cd $x
				EXERR=$?
			else	echo "\nDirectory $x does not exist"
				EXERR=0
				RT=1
			fi
		else	EXERR=0
		fi						;;

	d)	echo "Current directory: \c"
		pwd
		echo "Directories to be created: \c"
		read x
		if [ -n "$x" ]
		then	if [ -d "$x" ]
			then	echo "\nDirectory $x already exists"
				EXERR=0
				RT=1
			else	mkdir $x
				EXERR=$?
			fi
		else	EXERR=0
		fi						;;

	e)	echo "Directories to be removed: \c"
		read x
		if [ -n "$x" ]
		then	for i in $x
			do
				if [ ! -d "$i" ]
				then	echo "\nDirectory $i does not exist"
					EXERR=0
					RT=1
				else	t=`pwd`
					tdir="$t"
					cd "$i"
					if [ -s * .* *.* ]
					then  echo "\nDirectory $i contains files"
						cd $tdir
						EXERR=0
						RT=1
					else	cd $tdir
						rmdir $i
						EXERR=$?
					fi
				fi
			done
		else	EXERR=0
		fi						;;

	k)	echo "Directory of \c"
		pwd
		echo ""
		ls -FC | pg
		echo "\nFile to be removed: \c"
		read x
		if [ -n "$x" ]
		then	for i in $x
			do
				if [ ! -f "$i" ]
				then	echo "File $i does not exist"
					EXERR=0
					RT=1
				else	echo "\nType \"yes\" to continue, anything else to abort: \c"
					read ans
					if [ "$ans" = "yes" ]
					then	rm $i
						EXERR=$?
					else	echo "\nRemove aborted"
						EXERR=0
						RT=1
					fi
				fi
			done
		else	EXERR=0
		fi						;;

	i)	echo "Name of file to be copied: \c"
		read x
		if [ -n "$x" ]
		then	if [ ! -f "$x" ]
			then	echo "File $x does not exist"
				EXERR=0
				RT=1
			else	echo "Copy that file to: \c"
				read y
				if [ -n "$y" ]
				then	if [ -f "$y" ]
					then	echo "\nFile already exists, type \"yes\" to continue, anything else to abort: \c"
						read ans
						if [ "$ans" = "yes" ]
						then	cp $x $y
							EXERR=$?
						else	echo "\nCopy aborted"
							EXERR=0
							RT=1
						fi
					else	cp $x $y
						EXERR=$?
					fi
				fi
			fi
		else	EXERR=0
		fi						;;

	j)	echo "Name of file to be copied: \c"
		read x
		if [ -n "$x" ]
		then	if [ ! -f "$x" ]
			then	echo "File $x does not exist"
				EXERR=0
				RT=1
			else	echo "Copy the file to this directory: \c"
				read y
				if [ -n "$y" ]
				then	if [ -f "$y"/"$x" ]
					then	echo "\nFile already exists, type \"yes\" to continue, anything else to abort: \c"
						read ans
						if [ "$ans" = "yes" ]
						then	cp $x $y
							EXERR=$?
						else	echo "\nCopy aborted"
							EXERR=0
							RT=1
						fi
					else	cp $x $y
						EXERR=$?
					fi
				fi
			fi
		else	EXERR=0
		fi						;;

	f)	echo "File to be displayed: \c"
		read x
		echo ""
		if [ -n "$x" ]
		then	for i in $x
			do
				if [ ! -f "$i" ]
				then	echo "File $i does not exist"
					EXERR=0
					RT=1
				else	pg $i
					EXERR=$?
				fi
			done
		else	EXERR=0
		fi						;;

	g)	echo "Name of file to be moved: \c"
		read x
		if [ -n "$x" ]
		then	if [ ! -f "$x" ]
			then	echo "File $x does not exist"
				EXERR=0
				RT=1
			else	echo "Move file to this directory: \c"
				read y
				if [ -n "$y" ]
				then	if [ -f "$y"/"$x" ]
					then	echo "\nFile already exists, type \"yes\" to continue, anything else to abort: \c"
						read ans
						if [ "$ans" = "yes" ]
						then	mv $x $y
							EXERR=$?
						else	echo "\nMove aborted"
							EXERR=0
							RT=1
						fi
					else	mv $x $y
						EXERR=$?
					fi
				fi
			fi
		else	EXERR=0
		fi						;;

	h)	echo "Current file name: \c"
		read x
		if [ -n "$x" ]
		then	if [ ! -f "$x" ]
			then	echo "File $x does not exist"
				EXERR=0
				RT=1
			else	echo "New file name: \c"
				read y
				if [ -n "$y" ]
				then	if [ -f "$y" ]
					then	echo "\nFile already exists, type \"yes\" to continue, anything else to abort: \c"
						read ans
						if [ "$ans" = "yes" ]
						then	mv $x $y
							EXERR=$?
						else	echo "\nRename aborted"
							EXERR=0
							RT=1
						fi
					else	mv $x $y
						EXERR=$?
					fi
				fi
			fi
		else	EXERR=0
		fi						;;

	m)	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
		EXERR=$?
		RT=1						;;

	l)	user=`geteuser`
		if [ "$user" -eq 0 ]
		then	echo "Enter name of user: \c"
			read x
			if [ -n "$x" ]
			then 	passwd $x
				EXERR=$?
				continue
			fi
		fi
		passwd
		EXERR=$?					;;

	n)	TIME=`date '+%r'`
		echo "Current Time: $TIME"
		EXERR=0
		RT=1						;;

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

	q)	exit $EXERR					;;

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
