#!/bin/bash

# fbumenu_gen 0.5.0 'FluxBox Usermenu GENerator (ROX-Filer AppDir Support)'
# Copyright(C) 2007 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.5.0

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 "fbumenu_gen(FluxBox User MENU GENerator) $version (C) 2006-1007 m.kato"
    echo
    case $LANG in
	ja_JP.eucJP)
	    echo "使い方 fbumenu_gen {[-o out_file] [-t|ni|h]} "
	    echo "オプション:"
	    echo "    -o out_file : out_file に出力.(デフォルト:usermenu)"
	    echo "    -i submenu_file : submenu_file を取り込む.(デフォルト:usersubmenu)"
	    echo "    -t  : 標準出力に出力."
	    echo "    -T \"title name\"	 : ユーザーメニューのタイトルを\"title name\"にする."
	    echo "                                         (デフォルト:\"ユーザーメニュー\")"
	    echo "    -b  : 出力ファイルのバックアップを作る.(デフォルト:オフ)"
	    echo "    -ni : アイコンなしメニューを生成.(デフォルト:オフ)"
	    echo "    -th : メニュー項目が画像である場合、サムネイル表示させる.(デフォルト:オフ)"
	    echo "    -fp : フルパスモード.(デフォルト:オフ)"
	    echo "    -h  : このヘルプを表示."
	    echo
	    echo "example:"
	    echo "    fbumenu_gen"
	    echo "	  アイコン付きメニューを~/.fluxbox/usermenu に書き出します. (デフォルト）"
	    echo "    fbumenu_gen -ni"
	    echo "	  アイコンなしメニューを~/.fluxbox/usermenu に書き出します."
	    echo "    fbumenu_gen -th"
	    echo "	   メニュー項目に画像ファイルを含む場合、メニューにサムネイル表示します. "
	    echo "             対応画像フォーマット png、xpm、jpg、ppm"
	    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 "	  output usermenu to ~/.fluxbox/usermenu (with icons) :default"
	    echo "    fbumenu_gen -ni"
	    echo "	  output usermenu to ~/.fluxbox/usermenu (without icons)"
	    echo "    fbumenu_gen -th"
	    echo "        generate thumbnail for Image file"
	    echo "            corresponding Image format: png, xpm, jpg, ppm"
	    echo
	    ;;
    esac
}

usage() {

    term_search

    TMP=fbumenu_gen.tmp
    echo " " > $TMP
    echo "error : ~/.fluxbox/UserMenu ディレクトリが見当たりません。" >> $TMP
    echo " " >> $TMP
    echo "fbumenu_gen に付いての詳細は、 ">> $TMP
    echo "  メニュー → Fluxboxメニュー → Vineツール" >> $TMP
    echo "    → Fluxbox Information] を開き、" >> $TMP
    echo "    → menu tips → ユーザーメニューを作る " >> $TMP
    echo "を、ご覧下さい" >> $TMP
    echo " " >> $TMP
    echo "Push 'q' key" >> $TMP
    echo " " >> $TMP

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

error_check() {
    case "$checkstr" in
	-*|"")
	    help
	    echo "fbumenu_gen:" $error_message
	    exit 1
	    ;;
    esac
}

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

FBDIR=$HOME/.fluxbox
OUTFILE=$FBDIR/usermenu
INFILE=$FBDIR/usersubmenu
UMDIR=$FBDIR/UserMenu
ICONMODE=true
THMBMODE=false
BACKUP=false
FULLPATH=false
ROXUSR=false
PROTODIR="/usr/share/fluxbox/UserMenu"
TITLE="User Menu"
case "$LANG" in
    "ja_JP.eucJP"|"ja_JP.UTF-8")
	TITLE="ユーザーメニュー"
	;;
    *)
	;;
esac

GENERATE_ENGMENU=no
if [ -f ${HOME}/.fluxbox/langset ]; then
    . ${HOME}/.fluxbox/langset
    if [ "$GENERATE_ENGMENU" == "yes" ]; then
        TITLE="User Menu"
    fi
fi

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

# set option
for i in $@; do
    case $i in
	-o)
	    error_message="Output filename is missing."
	    checkstr=$2
	    error_check
	    OUTFILE="$2"
	    if [ "`dirname $2`" == "." ]; then
		OUTFILE=`pwd`/`basename $2`
	    fi
	    ;;
	-i)
	    error_message="Input filename is missing."
	    checkstr=$2
	    error_check
	    INFILE="$2"
	    if [ "`dirname $2`" == "." ]; then
		INFILE=`pwd`/`basename $2`
	    fi
	    ;;
	-t)
	    OUTFILE=/dev/stdout
	    ;;
	-T)
	    error_message="Title name is missing."
	    checkstr=$2
	    error_check
	    TITLE="$2"
	    while [ -n "$3" ]; do
		case $3 in
		    -*|"")
			break;;
		    *)
			TITLE="$TITLE ""$3"
			shift
			;;
		esac
	    done
	    ;;
	-b)
	    BACKUP=true
	    ;;
	-ni)
	    ICONMODE=false
	    ;;
	-th)
	    THMBMODE=true
	    ;;
	-fp)
	    FULLPATH=true
	    ;;
	-h)
	    help
	    exit 0
	    ;;
	-*)
	    help			
	    echo -e fbumenu_gen: unknown option \'$i\'.
	    exit 1
	    ;;
    esac
    shift
done

# check UserMenu dir
if ! [ -d $UMDIR ] ; then
#    mk_prototype
    cp -a "$PROTODIR" "$FBDIR/"
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 [ -f "$INFILE" ] ; then
    echo -e Submenu file \[$INFILE\] found.
    cat $INFILE	 >> $OUTFILE
fi
indent="\\t"
indent2=$indent

mk_umenu

echo "[end]" >> $OUTFILE

exit 0
