# @(#) Explore 1.0	Magpie Download Directory Explorer
#
#	Author: John Cowan
#	Note:	This program MUST reside in the Magpie home directory.
#	Requires ArcWindow 2.1 or greater.
#	Requires 'lizzy', the parent killer
:
# set these up for your system environment
ACCEPT=/u/bin/accept
ARCW=ArcWindow
AWK=/bin/awk
CUT=/usr/bin/cut
EXPR=/bin/expr
KILL=/bin/kill
MORE="/usr/bin/more -d"
SLEEP="/bin/sleep"
PATHS="files/Paths"
TEMP="/tmp/Expl$$"
trap 'rm -f $TEMP' 0 1 2 3

# check for time out (and other future arguments) from Magpie
if [ "$#" -gt 0 ]
   then	for ARG in $@
	do
		case "$ARG" in
		-t*)	TIMELEFT=`echo $ARG | $CUT -c3-`
			if [ "`$EXPR $TIMELEFT / 60`" = "0" ]
			   then	echo "\nSorry... you don't have enough time"\
				     "to run the Explorer.\n"
				exit 1
			fi
			echo "\nYou have `$EXPR $TIMELEFT / 60` minute(s) left"
			lizzy $TIMELEFT &
			;;
		esac
	done
fi


$AWK '/^\/[^/]/ { printf "%d. %s\n", ++i, $0 }' $PATHS >$TEMP
while :
do
	echo "
----------------------------------
Magpie Download Directory Explorer
----------------------------------
(L)ist     directories
(O)pen     a directory

Command [RETURN to exit] > \c"

	ACTION=`$ACCEPT loqLOQ`

	if [ "$ACTION" = "" -o "$ACTION" = "q" -o "$ACTION" = "Q" ]
	   then
		$KILL -15 $!
		exit 0
	fi
	echo ""
#
# List available directories
#
	if [ "$ACTION" = "L" -o "$ACTION" = "l" ]
	   then
		echo "\nDirectories available for exploration:\n"
		$MORE $TEMP
	elif [ "$ACTION" = "o" -o "$ACTION" = "O" ]
	    then
		echo "\nSpecify directory number: \c"
		read NAME
		if [ "$NAME" = "" ]
		    then continue
		fi
		ARG=\$
		TRUENAME=`$AWK "/^$NAME\./ { print ${ARG}2 }" $TEMP`
		if [ "$TRUENAME" = "" ]
		then continue
		fi
		echo "\nOpening directory $TRUENAME"
		$ARCW $TRUENAME
		echo ""
	fi
done
