#!/bin/sh
#
# cdv, a shell script for use with cdview for those of us who cannot yet
# mount CD-ROMS right into the filesystem.
#
# Copyright (c) 1994, Christopher Sean Hilton - copy freely.
#
# Last Modified: Wed Mar 23 00:39:27 1994 by [chris]
#
# Rename this as you wish and put it into a convienient place. make sure
# that cdview is located in the path so that this file can find it.
#
#

if [ "$1" ]
then
	cd_wd=$1
else
	cd_wd=/
fi
if [ "$cd_wd" != "/" ]
then
	tocdir=/
fi
all_done=n
scratchdir=/tmp/$$
toc=$scratchdir/cdsh.toc
dirs=$scratchdir/cdsh.dirs
files=$scratchdir/cdsh.files
getdtoc=$scratchdir/getd.toc
getddirs=$scratchdir/getd.dirs
getdfiles=$scratchdir/cdsh.dirs
cdview=/usr/local/bin/cdview
mkdir $scratchdir

while [ $all_done = "n" ]
do

#	When the directory is out of sync with the table of contents
#	rebuild the table of contents from the cd.

	if [ "$tocdir" != "$cd_wd" ]
	then
		echo -n "Getting table of contents for: $cd_wd..."
		rm -f ${toc}*
		$cdview $cd_wd >$toc
		if [ ! -s $toc ]
		then
			echo "Bad directory: $cd_wd..."
			cd_wd=$tocdir
			$cdview $cd_wd >$toc
		fi
		/bin/awk '$1~/^d/ { print $10 }' $toc >$dirs
		/bin/awk '$1~/^-/ { printf $10 }' $toc >$files

		split -18 $toc ${toc}.
		tocpage=${toc}.aa
		page=1
		pagecount=$((`ls ${toc}.* | wc -l`))
		tocdir=$cd_wd
	fi

	echo "CD-ROM Directory: $tocdir Page: $page of $pagecount\n"
	/bin/awk '{ printf("%10s %9d %s\n", $1, $5, $10) }' $tocpage 

	echo -n "\nCommand: "
	read cmd args
	case $cmd in
	cd)	if [ "$args" = "" ]
		then
			echo -n "Change directory (new directory): "
			read args
		fi
		case $args in
		.)
			;;
		..)
			cd_wd=`echo -n foobar$cd_wd | awk -F/ '{ for (i = 2; i < NF - 1; i++) printf("/%s", $i) }' -`/
			;;
		/*)	
			cd_wd=$args
			;;
		*/*)
			cd_wd=$cd_wd$args/
			;;
		*)
			if ! grep $args $dirs >/dev/null
			then
				echo -n "Bad directory: $args\nHit Enter to continue:"
				read tmp
			else
				cd_wd=$cd_wd$args/
			fi
			;;
		esac
		case $cd_wd in
		*/)
			;;
		*)
			cd_wd=$cd_wd/
			;;
		esac
		;;
	G*)	if [ "$args" = "" ]
		then
			echo -n "Get a directory (directory): "
			read args
		fi
		if ! grep $args $dirs
		then
			echo "$args is not a directory."
			echo -n "Hit enter to continue: "
			read tmp
		else
			echo "Get directory not done yet"
		fi
		;;
	g*)	if [ "$args" = "" ]
		then
			echo -n "Get a file (filename): "
			read args
		fi
		$cdview $cd_wd$args >$args
		;;

	[Pp]*)	echo -n "Previous page..."
		if [ $page -gt 1 ]
		then
			target=${toc}.aa
			for tmp in ${toc}.*
			do
				if [ "$tocpage" = "$tmp" ]
				then
					tocpage=$target
					break
				else
					target=$tmp
				fi
			done
			page=$(($page - 1))
		fi
		;;
	[Nn]*)	echo -n "Next page..."
		if [ $page -lt $pagecount ]
		then
			target=foobar
			for tmp in ${toc}.*
			do
				if [ "$tocpage" = "$target" ]
				then
					tocpage=$tmp
					target=done
					break
				else
					target=$tmp
				fi
			done
			if [ "$target" != "done" ]
			then
				tocpage=$tmp
			fi
			page=$(($page + 1))
		fi
		;;
	[Qq]*)	echo "quit"
		all_done=y
		;;
	!)	if [ "$args" = "" ]
		then
			if [ "$SHELL" = "" ]
			then
				/bin/sh
			else
				$SHELL
			fi
		else
			$args
			echo -n "Press Enter to continue:"
			read tmp
		fi
		;;
	*)	cat <<EOF

Help: (c)hange directory, (d)escend, (g)et file, (p)revious page, 
(n)ext page, (q)uit (!) Shell.

Press Enter to continue:
EOF
		read tmp
		;;
	esac
done

rm -r $scratchdir

