:
# xroot - selects from a variety of image format files
#         for use as a mirror tiled root window
#
# Usage: xroot [-e | -j | -l | -s | -r | -u | -n # | -p # | -N # | -P # | 
#               -m | directory or file name | -b]
#
# Takes as an argument the directory in which to find files
# If the argument is not a directory, then it assumes it's an image file
# If the argument is the string "-r", it displays a random GIF from /z/gif.
# If the argument is "-e", it displays a random endo PPM
# If the argument is "-l", it displays a random lyap PPM
# If the argument is "-f", it displays a random Fractal PPM
# If the argument is "-j", it displays a random Julia/Mandelbrot set
# If the argument is "-s", it displays a random Nasa Space GIF
# Additionally, arguments of "-n num" indicate select amongst the 1st num pics
# "-p num" indicates pick the num'th picture
# "-N num" indicates select amongst the 1st num directories
# "-P num" indicates pick the num'th directory
# "-m" indicates use full root window rather than tiling
# "-b" indicates browse and select from available pics
#
# by Ron Record (rr@sco.com) 10 Sep 1992
#

PROG=$0
GROOT=/usr/skunk/pics/gif
FILE=
BROW=
TILE="-rmode 2"
DIR=$GROOT/ocean
EDIR=$GROOT/endo
HDIR=$GROOT/hop
MDIR=$GROOT/spore
JDIR=$GROOT/julia
LDIR=$GROOT/lyap
SDIR=$GROOT/xtopo
CMD="xv -perfect -noresetroot -owncmap -root -quit"
DEF_PIC=$LDIR/monsters.gif
NUM=
FNUM=
DNUM=
PNUM=

Usage() {
	echo "Usage: $PROG [-p num | -n num | -P num | -N num | -m |"
	echo "	-e | -j | -l | -r | -s | -f | -u | directory or file name | -b]"
	echo "	Where :	"
	echo "		-b indicates browse and select from available pics"
	echo "		-e indicates display an Endomorphism diagram"
	echo "		-f indicates display a Fractal picture"
	echo "		-j indicates display a Julia/Mandelbrot set"
	echo "		-l indicates display a Lyapunov diagram"
	echo "		-s indicates display a Nasa image"
	echo "		-r indicates randomly display saved picture"
	echo "		-m indicates use full screen rather than tiling"
	echo "		-u displays this message"
	echo "\n$PROG is a shell script front-end for xv."
	echo "It was written by Ronald Joe Record and can be used to decorate."
	echo "the root window with a GIF, TIFF, PPM or any other image xv reads."
	echo "When invoked with no arguments, it displays the monster lyap GIF."
	echo "To cycle through a set of images, use croot.\n"
	exit 1
}

ReCycle() {
	for j in $DIR/*
	do
		[ $i -gt $FNUM ] && {
			echo "\nPlease wait while i tile your root window ...\c"
	    	$CMD $TILE $j
			echo "\nDo you prefer this as your root window ? (y/n)\c"
			read ans
			[ "$ans" = "y" ] || [ "$ans" = "Y" ] && break
		}
		i=`expr $i + 1`
		[ $i -gt $NUM ] && break
	done
}

Browse() {
	[ -d "$DIR" ] || {
		echo "Warning: $DIR does not exist or is not a directory"
		exit 1
	}
	[ "$NUM" = "" ] && NUM=`ls -l $DIR | wc -l`
	NUM=`expr $NUM - 1`
	[ $NUM -gt 255 ] && NUM=255
	[ "$FNUM" = "" ] && FNUM=`random $NUM`
	i=0
	ReCycle
	[ "$ans" = "y" ] || [ "$ans" = "Y" ] || {
		[ $i -lt $NUM ] && ReCycle	
		[ "$ans" = "y" ] || [ "$ans" = "Y" ] || {
			echo "Well, i'm all out of that variety of root images."
			echo "Please select another category and try again."
			echo "I will leave the root window as it is now."
		}
	}
}

Select() {
	[ -d "$DIR" ] || {
		echo "Warning: $DIR does not exist or is not a directory"
		exit 1
	}
	[ "$NUM" = "" ] && NUM=`ls -l $DIR | wc -l`
	NUM=`expr $NUM - 1`
	[ $NUM -gt 255 ] && NUM=255
	[ "$FNUM" = "" ] && FNUM=`random $NUM`
	i=0

	for j in $DIR/*
	do
		[ "$i" = "" ] && echo "i is empty"
		[ "$FNUM" = "" ] && echo "FNUM is empty with NUM = $NUM and DIR=$DIR"
		[ $i = $FNUM ] && {
		    $CMD $TILE $j
		    break
		}
		i=`expr $i + 1`
	done
}

DirSelect() {
	[ "$DNUM" = "" ] && {
		DNUM=`ls -l $ARGDIR | wc -l`
		DNUM=`expr $DNUM - 1`
	}
	[ "$PNUM" = "" ] && PNUM=`random $DNUM`
	[ $PNUM -gt 255 ] && PNUM=255
	i=0

	for j in $ARGDIR/*
	do
		[ "$j" = "$ARGDIR/small" ] && continue
		[ $i = $PNUM ] && {
		    DIR=$j
		    break
		}
		i=`expr $i + 1`
	done
}

[ $# = 0 ] && {
	$CMD $TILE $DEF_PIC
	exit 0
}

while case "$1" in
	usage)  Usage
		;;
	-b)	BROW=True
		;;
	-m)	TILE="-max"
		;;
	-n)	NUM=$2
		shift
		;;
	-p)	FNUM=$2
		shift
		;;
	-e)     DIR=$EDIR
		;;
	-h)     DIR=$HDIR
		;;
	-j)     DIR=$JDIR
		;;
	-k)     DIR=$MDIR
		;;
	-l)     DIR=$LDIR
		;;
	-f) ;;
	-r)     ARGDIR=$GROOT
			DirSelect
		;;
	-s)     DIR=$SDIR
		;;
	"")     break
		;;
     *) if [ -d $1 ]
		then
			DIR=$1
		else
			if [ -f $1 ]
			then
				FILE=$1
			else
				if [ -f $DIR/$1 ] 
				then
					FILE=$DIR/$1
				else
			    	if [ -d $GROOT/$1 ] 
			    	then
				    	DIR=$GROOT/$1
			    	else
				    	[ -f $EDIR/$1 ] && FILE=$EDIR/$1
				    	[ -f $JDIR/$1 ] && FILE=$JDIR/$1
				    	[ -f $LDIR/$1 ] && FILE=$LDIR/$1
				    	[ "$FILE" = "" ] && Usage
			    	fi
				fi
			fi
		fi
		;;
        esac
do
	shift
done

if [ "$BROW" = "True" ]
then
	Browse
else
	if [ "$FILE" = "" ]
	then
		Select
	else
		$CMD $TILE $FILE
	fi
fi

exit 0
