daleken/
daleken/data/notes/
daleken/data/player/
daleken/data/system/poses/
daleken/doc/Homepage/images/
daleken/log/
#!/bin/bash

#set -x

if [ -x `which dialog` ]; then
    echo Starting the Daleken Documentation Reader...
else
    echo This menu program requires the dialog program.
    echo Sorry.
    exit 0
fi

THISFILE=`which $0`
# The tmpfile is named .html so lynx can be nice about it.
TMPFILE=/tmp/docmenu.html
DIALOG=(dialog --backtitle 'Daleken Documentation Reader')
#DIALOG=dialog

# List all files and put them in a manner useful to dialog
function menudir() {
    if [ -d $1 ]; then
	unset CURRENTFILE
	cd $1
	LIST=`ls -bBd1 * 2>/dev/null`
	TITLE="Listing of directory $PWD"
    else
	CURRENTFILE=$1
	TITLE="Listing of archive $PWD/$1"
	case $1 in
	*.zip)
	    LIST=`zipinfo -1 $1`
	    ;;
	*.tar)
	    LIST=`tar -tf $1`
	    ;;
	*.tar.gz|*.tgz|*.tar.Z)
	    LIST=`gzip -cdfq $1 | tar -tf -`
	    ;;
	*.tar.bz2|*.tbz)
	    LIST=`bzip2 -cdf $1 | tar -tf -`
	    ;;
	*)
	    LIST=`ls -bBd1 * 2>/dev/null`
	    CURRENTFILE=
	    TITLE="Listing of directory $PWD"
	    ;;
	esac
    fi
    
    MENU=("${DIALOG[@]}" --clear --menu "$TITLE" 20 70 14 .. 'Up Directory')

    for i in $LIST; do
	add=1
	case $i in
	README*|*.txt)
	    desc="Text File"
	    ;;
	*.zip|*.tar|*.tar.gz|*.tgz|*.tar.Z|*.tar.bz2|*.tbz)
	    desc="Archive"
	    ;;
	*.gz|*.bz2|*.Z)
	    desc="Compressed File"
	    ;;
	*.html)
	    desc="HTML Document"
	    ;;
	*~|*.bak)
	    add=0
	    ;;
	*)
	    if [ -d $i ]; then
		desc="Directory (select to view)"
	    elif [ ! -r $i ]; then
		add=0
	    else
		desc="Unknown (use default viewer)"
	    fi
	    ;;
	esac
	if [ $add -eq 1 ]; then
	    MENU=("${MENU[@]}" "$i" "$desc")
	fi
    done
}


function do_menu() {
set -x
    if "${MENU[@]}" 2>${TMPFILE}; then
	choice=`cat ${TMPFILE}`
    else
#	rm -f ${TMPFILE}
	exit
    fi
#    rm -f ${TMPFILE}
}


function show_file() {
    case $2 in
    *.html|*.html.gz|*.html.Z|*.html.bz2)
	if [ -x `which lynx` ]; then
	    lynx "$1"
	else
	    echo Cannot execute lynx, hit enter to continue.
	    read
	fi
	;;
    *)
	"${DIALOG[@]}" --title "$2" --textbox "$1" 20 78
	;;
    esac
}


function show_zipped_file() {
    case $1 in
    *.bz2)
	bzip2 -cdf $1 > $TMPFILE
	;;
    *)
	gzip -cdfq $1 > $TMPFILE
	;;
    esac
    show_file $TMPFILE $1
    rm -f $TMPFILE
}


function show_archived_file() {
    case $1 in
    *.zip)
	unzip -p $1 $2 > $TMPFILE
	;;
    *.tar)
	tar -xOf $1 $2 > $TMPFILE
	;;
    *.tar.gz|*.tgz|*.tar.Z)
	gzip -cdfq $1 | tar -xOf - $2 > $TMPFILE
	;;
    *.tar.bz2|*.tbz)
	bzip2 -cdf $1 | tar -xOf - $2 > $TMPFILE
	;;
    *)
	echo Error reading file, press Enter to continue.
	read
	return
	;;
    esac
    echo Showing file $TMPFILE as $2 1>&2
    show_file $TMPFILE $2
    rm -f $TMPFILE
}


menudir ${1:-.}
do_menu
while [ "$choice" != 'quit' ]; do
    if [ "$CURRENTFILE" != "" ]; then
	if [ "$choice" != ".." ]; then
	    show_archived_file $CURRENTFILE $choice
	else
	    CURRENTFILE=
	    menudir .
	fi
    elif [ -d $choice ]; then
	menudir $choice
    else
	case $choice in
	*.zip|*.tar|*.tar.gz|*.tgz|*.tar.Z|*.tar.bz2|*.tbz)
	    menudir $choice
	    ;;
	*.gz|*.bz2|*.Z)
	    show_zipped_file $PWD/$choice
	    ;;
	*)
	    show_file $PWD/$choice $PWD/$choice
	    ;;
	esac
    fi
	    
    do_menu
done

exit