#!/bin/sh

# FluxBox User MENU GENerator (ROX-Filer AppDir Support)
# Copyright(C) 2006 m.kato <mkato@par.odn.ne.jp>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

version=0.4.2

mk_umenu() {

    local HOMEDIR=/home/`whoami`
    local FBDIR=$HOMEDIR/.fluxbox
    local USERMENUDIR=$FBDIR/UserMenu
    local curdir

    for item in * .fluxbox  ; do
	if [ -d "$item" ] ; then
	    # avoid infinite loop
	    check_realpath
	    if [ "$realname" == "$HOMEDIR" ] ; then
		continue
	    elif [ "$realname" == "$FBDIR" ] ; then
		continue
	    elif [ "$realname" == "$USERMENUDIR" ] ; then
		continue
	    fi
	    # if directory
	    curdir=`pwd`
	    if [ -x "$item"/AppRun ] ; then
		# if ROX AppDir
		line=$indent"[exec] ($item) {$curdir/$item/AppRun}"
		if [ $ICONMODE == true ] ; then
		    pushd "$item" 2> /dev/null
		    if [ -a .DirIcon ] ; then
			rm .DirIcon.png
			ln -s .DirIcon .DirIcon.png
			line="$line <$curdir/$item/.DirIcon.png>"
		    elif [ -a "AppIcon.xpm" ] ; then
			line=$line" <$curdir/$item/AppIcon.xpm>"
		    fi
		    popd 2> /dev/null
		fi
		echo -e $line | sed s/\\/home\\/`whoami`/~/g >> $OUTFILE
		# end ROX AppDir
	    else
		# if normal directory
		cd $item
		line=$indent"[submenu] ("$item")"
		if [ $ICONMODE == true ] ; then
		    pushd "$item" 2> /dev/null
		    if [ -a .DirIcon ] ; then
			rm .DirIcon.png
			ln -s .DirIcon .DirIcon.png
			line="$line <$curdir/$item/.DirIcon.png>"
				fi
		    popd 2> /dev/null
		fi
		echo -e $line | sed s/\\/home\\/`whoami`/~/g >> $OUTFILE
		indent2=$indent
		indent=$indent"\\t"
		mk_umenu
		echo -e $indent"[end]"	>> $OUTFILE
		indent=$indent2
		# end normal directory
	    fi
	    # end directory
	elif [ -x "$item" ] ; then
	    # if executable
	    line=$indent"[exec] ("$item") "{`pwd`/\""$item\""}
	    if [ $ICONMODE == true ] ; then
		if [ -a ./"$item".png ] ; then
		    line=$line" <`pwd`/"$item".png>"
		elif [ -a ./."$item".png ] ; then
		    line=$line" <`pwd`/."$item".png>"
		elif [ -a ~/.fluxbox/icons/"$item".png ] ; then
		    line=$line" <~/.fluxbox/icons/"$item".png>"
		fi
	    fi
	    echo -e $line | sed s/\\/home\\/`whoami`/~/g >> $OUTFILE
	    # end executable
	elif $ROXUSR ; then
	    # if rox user
	    if [ -f "$item" ] ; then
		# if normal file
		line=$indent"[submenu] ($item)"
		if [ $ICONMODE == true ] ; then
		    # if icon mode
		    if [ -a ./"$item".png ] ; then
			line=$line" <`pwd`/"$item".png>"
		    elif [ -a ./."$item".png ] ; then
			line=$line" <`pwd`/."$item".png>"
		    elif [ -a ~/.fluxbox/icons/"$item".png ] ; then
			line=$line" <~/.fluxbox/icons/"$item".png>"
		    else
			if [ $THMBMODE == true ] ; then
			    # if thumbnail mode
			    check_pix
			    case $ext in
				png|xpm|jpg|ppm)
				    line=$line" <`pwd`/"$item">"
				    ;;
			    esac
			    # end thumbnail mode
			fi
		    fi
		    # end icon mode
		fi
		echo -e $line | sed s/\\/home\\/`whoami`/~/g >> $OUTFILE
		line=$indent"\t[exec] (Open $item) {rox "`pwd`/"$item"}
		echo -e $line | sed s/\\/home\\/`whoami`/~/g >> $OUTFILE
		check_realpath
		pathname=$realpath
		if [ $FULLPATH == false ] ; then
		    pathname=`basename $realpath`
		fi
		line=$indent"\t[exec] (Open $pathname/) {rox ""$realpath}"
		echo -e $line | sed s/\\/home\\/`whoami`/~/g >> $OUTFILE
		echo -e "$indent[end]" >> $OUTFILE
		# end normal file
	    fi
	    # end rox user
	fi

    done
    cd ..
    curdir=`pwd`
    if [ $curdir == $UMDIR ] ; then
	indent2="\\t"
    fi
    indent=$indent2
}

check_realpath () {

    local tmpname
    local string

    realpath=`pwd`
    realname=""

    for string in `ls -l "$item"` ; do
	tmpname=$string
    done
    if ! [ $tmpname == "$item" ] ; then
	realpath=`dirname $tmpname`
	realname=$tmpname
    fi
}

check_pix () {

    local string

    ext=""
    for string in `echo "$item" | sed s/\\\./' '/g` ; do
	ext=$string
    done
}

term_search() {

    local term

    for term in mlterm kterm rxvt xterm Eterm ; do
	if `type $term 1>& /dev/null` ; then
	    MY_TERM=$term
	    break
	fi
    done
}

help() {
    echo
    echo "FluxBox User MENU GENerator $version (C) 2006 m.kato"
    echo
    echo "usage fbumenu_gen {[-o out_file] [-t|ni|h]} "
    echo "Options:"
    echo "    -o out_file : output to out_file.(default:usermenu)"
    echo "    -i submenu_file : include submenu_file.(default:usersubmenu)"
    echo "    -t  : output to stdout."
    echo "    -T \"title name\"	 : set usermenu title."
    echo "    -b  : make outfile.backup"
    echo "    -ni : generate without icon."
    echo "    -th : thumbnail mode."
    echo "    -fp : fullpath mode."
    echo "    -h  : display this message."
    echo
    echo "example:"
    echo "    fbumenu_gen"
    echo "	  դ menu ~/.fluxbox/usermenu "
    echo "	  񤭽Фޤ (ǥեȡ"
    echo "    fbumenu_gen -ni"
    echo "	  ʤ menu ~/.fluxbox/usermenu "
    echo "	  񤭽Фޤ"
    echo
}

usage() {

    term_search

    TMP=fbumenu_gen.tmp
    echo " " > $TMP
    echo "error : ~/.fluxbox/UserMenu ǥ쥯ȥ꤬ޤ" >> $TMP
    echo " " >> $TMP
    echo "fbumenu_gen դƤξܺ٤ϡ ">> $TMP
    echo "  ˥塼 - [Fluxbox ] - [FluxboxˤĤ]" >> $TMP
    echo "    - [Fluxbox Tips] 򳫤" >> $TMP
    echo "    - [˥塼 tips] 򥯥å" >> $TMP
    echo "  ֥桼˥塼롧(fbumenu_gen) ι" >> $TMP
    echo "  " >> $TMP
    echo " " >> $TMP
    echo "Push 'q' key" >> $TMP
    echo " " >> $TMP

    $MY_TERM -geometry 65x12+300+100 -e less $TMP
    rm $TMP
}

# Start user menu generate -----------------------------

OUTFILE=~/.fluxbox/usermenu
INFILE=~/.fluxbox/usersubmenu
UMDIR=~/.fluxbox/UserMenu
ICONMODE=true
THMBMODE=false
BACKUP=false
FULLPATH=false
FBDIR=~/.fluxbox
ROXUSR=false
TITLE=`echo "桼˥塼" | nkf -w`

if `type rox 1>& /dev/null` ; then 
    ROXUSR=true
fi

# set option
for i in $@; do
    case $i in
	-o)
	    if [ "$2" != "" ] ; then
		OUTFILE="$2"
	    else
		help
		exit 1
	    fi
	    ;;
	-i)
	    if [ "$2" != "" ] ; then
		INFILE="$2"
	    else
		help
		exit 1
	    fi
	    ;;
	-t)
	    OUTFILE=/dev/stdout
	    ;;
	-T)
	    if [ "$2" != "" ] ; then
		case $2 in
		    -*)
			echo fbumenu: Bad Title name $2
			exit 1
			;;
		esac
		TITLE="$2"
	    else
		help
		exit 1
	    fi
	    ;;
	-b)
	    BACKUP=true
	    ;;
	-ni)
	    ICONMODE=false
	    ;;
	-th)
	    THMBMODE=true
	    ;;
	-fp)
	    FULLPATH=true
	    ;;
	-h)
	    help
	    exit 0
	    ;;
	-*)
	    echo fbumenu_gen: unknown option $i.
	    help			
	    exit 1
	    ;;
    esac
    shift
done

# check UserMenu dir
if ! [ -d $UMDIR ] ; then
    usage
    exit 1
fi

# make backup
if [ -a $OUTFILE ] ; then
    if [ $BACKUP == true ] ; then
	cp $OUTFILE $OUTFILE.backup
    fi
    T=`date +%Y_%m_%d`
    mv $OUTFILE $OUTFILE.$T
fi

cd $UMDIR
line="[submenu] (""$TITLE"")"
echo $line > $OUTFILE
if [ "$INFILE" != "" ] ; then
    cat $INFILE	 >> $OUTFILE
fi
indent="\\t"
indent2=$indent

mk_umenu

echo "[end]"  >> $OUTFILE

exit 0
