#!/bin/sh
#
# menu generator 0.1.3
# (use Desctop Entries)
# Requires: librsvg2 (for convert svg to png

# アイコン検索
search_icon()
{
    local icon=""

    ICON=""
    # search app icon
    icon=`grep -i ^Icon $1 | cut -d '=' -f 2`
    [ -z "$icon" ] && return 1 # not Icon key
    icon=`basename "$icon"`
    if [ -z "`echo "$icon" | grep \.`" ]; then
        icon="$icon".png
    fi
    if [ -f $HOME/.icons/AppIcons/"$icon" ]; then
        icon=$HOME/.icons/AppIcons/"$icon"
    else
        icon=`grep -e ^Icon $1 | cut -d '=' -f 2 | cut -d'
' -f1`
        if [ -z "`echo $icon | grep \.png`" ]; then
            if [ -z "`echo $icon | grep \.xpm`" ]; then
                if [ -z "`echo $icon | grep -e \.svg$`" ]; then
                    icon="$icon".png
                fi
            fi
        fi
        if ! [ -f "$icon" ]; then
            if [ -f /usr/share/pixmaps/"$icon" ]; then
                icon=/usr/share/pixmaps/"$icon"
            elif [ -f /usr/share/icons/hicolor/48x48/apps/"$icon" ]; then
                icon=/usr/share/icons/hicolor/48x48/apps/"$icon"
            elif [ -f /usr/share/icons/gnome/48x48/apps/"$icon" ]; then
                icon=/usr/share/icons/gnome/48x48/apps/"$icon"
            elif [ -f /usr/share/icons/oxygen/32x32/apps/"$icon" ]; then
                icon=/usr/share/icons/oxygen/32x32/apps/"$icon" # kde4 themes

            elif [ -f /usr/share/icons/kdeclassic/48x48/apps/"$icon" ]; then
                icon=/usr/share/icons/kdeclassic/48x48/apps/"$icon"
            elif [ -f /usr/share/icons/crystalsvg/32x32/apps/"$icon" ]; then
                icon=/usr/share/icons/crystalsvg/32x32/apps/"$icon"

            elif [ -f /usr/share/icons/"$icon" ]; then
                icon=/usr/share/icons/"$icon"

            elif [ -f /usr/share/icons/hicolor/128x128/apps/"$icon" ]; then
                icon=/usr/share/icons/hicolor/128x128/apps/"$icon"
            elif [ -f /usr/share/icons/gnome/128x128/apps/"$icon" ]; then
                icon=/usr/share/icons/gnome/128x128/apps/"$icon"
            elif [ -f /usr/share/icons/crystalsvg/128x128/apps/"$icon" ]; then
                icon=/usr/share/icons/crystalsvg/128x128/apps/"$icon"

            elif [ -f /usr/share/icons/gnome/32x32/apps/"$icon" ]; then
                icon=/usr/share/icons/gnome/32x32/apps/"$icon"
            elif [ -f /usr/share/icons/kdeclassic/32x32/apps/"$icon" ]; then
                icon=/usr/share/icons/kdeclassic/32x32/apps/"$icon"
            elif [ -f /usr/share/icons/gnome/32x32/categories/"$icon" ]; then
                icon=/usr/share/icons/gnome/32x32/categories/"$icon"
            elif [ -f /usr/share/icons/gnome/32x32/devices/"$icon" ]; then
                icon=/usr/share/icons/gnome/32x32/devices/"$icon"
            elif [ -f /usr/share/icons/gnome/32x32/places/"$icon" ]; then
                icon=/usr/share/icons/gnome/32x32/places/"$icon"
            elif [ -f /usr/share/icons/hicolor/32x32/apps/"$icon" ]; then
                icon=/usr/share/icons/hicolor/32x32/apps/"$icon"

            elif [ -f /usr/share/icons/hicolor/24x24/apps/"$icon" ]; then
                icon=/usr/share/icons/hicolor/24x24/apps/"$icon"
            elif [ -f /usr/share/icons/gnome/24x24/apps/"$icon" ]; then
                icon=/usr/share/icons/gnome/24x24/apps/"$icon"
            elif [ -f /usr/share/icons/hicolor/16x16/apps/"$icon" ]; then
                icon=/usr/share/icons/hicolor/16x16/apps/"$icon"

            else
                icon=`echo "$icon" | sed "s/.png/.svg/"`
                tmp=`find /usr/share/icons/gnome/scalable/ -name "$icon"`

                if [ -n "$tmp" ]; then
                    icon="$tmp"
                else
                    icon=""
                fi
            fi
        fi
    fi
    if [ -n "$icon" ]; then
        if [ -n "`echo $icon | grep \.svg$`" ]; then
            # convert svg to png
            if [ ! -d ~/.fluxbox/icons ]; then
                mkdir -p ~/.fluxbox/icons
            fi
            pngname=`echo $icon | sed 's/.svg$/.png/'`
            pngname=~/.fluxbox/icons/`basename "$pngname"`
            rsvg "$icon" "$pngname" 2> /dev/null
            if [ $? == 0 ]; then
                ICON="<$pngname>"
            else
                rm "$pngname"
                ICON=""
            fi
        else
            ICON="<$icon>"
        fi
    else
        ICON=""
    fi
}

# カテゴリ設定
set_category()
{
    local category=""
 #   DIRICON=""

    if [ -n "`echo $1 | grep Vine`" ]; then
        category="Vine"
    elif [ -n "`echo $1 | grep Development`" ]; then
        category="Development"
    elif [ -n "`echo $1 | grep Office`" ]; then
        category="Office"
    elif [ -n "`echo $1 | grep Graphics`" ]; then
        category="Graphics"
    elif [ -n "`echo $1 | grep Network`" ]; then
        category="Network"
    elif [ -n "`echo $1 | grep AudioVideo`" ]; then
        category="AudioVideo"
    elif [ -n "`echo $1 | grep Game`" ]; then
        category="Game"
    elif [ -n "`echo $1 | grep Education`" ]; then
        category="Education"
    elif [ -n "`echo $1 | grep Editor`" ]; then
        category="Editor"
    elif [ -n "`echo $1 | grep Amusement`" ]; then
        category="Amusement"
    elif [ -n "`echo $1 | grep information`" ]; then
        category="Information"
    elif [ -n "`echo $1 | grep System`" ]; then
        category="System"
    elif [ -n "`echo $1 | grep Utility`" ]; then
        category="Utility"
    elif [ -n "`echo $1 | grep -e Settings -e settings`" ]; then
        category="Settings"
    elif [ -n "`echo $1 | grep Core`" ]; then
        category="Core"
    elif [ -n "`echo $1 | grep Screensaver`" ]; then
        category="Screensaver"

    else
        category="Etc"
    fi

#    DIRICON="$curdir"/icons/"$category".png

    CATEGORY="$category"
}

# メニュー生成
menu_gen()
{
    local ENT_DIR=/usr/share/applications
    local out_dir=/tmp/fb_menu_gen.`date '+%y%m%d%H%M%S'`
    local o_dir="$out_dir"
    local ENT_FILES="$ENT_DIR/*.desktop $ENT_DIR/Vine/*.desktop \
$ENT_DIR/kde/*.desktop $ENT_DIR/kde4/*.desktop"
    local ENTRIES=`ls $ENT_FILES | sort`

    mkdir "$out_dir"
    ### output DesktopEntry information
    for i in $ENTRIES; do
        # initialize variables
        KDE_MENU=""
        KDE4_MENU=""
        XFCE_MENU=""
        INDENT=""
        out_dir="$o_dir"
        # Set Exec command (Exec key)
        EXEC=`grep -i "^Exec" $i | cut -d "=" -f2`
        if [ -z "$EXEC" ]; then
            continue # don't generate
        fi
        EXEC=`echo "$EXEC" | sed "s/\%[FfimUu]//g"`
        EXEC=`echo "$EXEC" | sed "s/\"\%c\"//g"`
        EXEC=`echo "$EXEC" | sed "s/ -caption //"`
        # Set Submenu name (Categories key)
        tmp_category=`grep -i "^Categories" $i 2> /dev/null | cut -d "=" -f2`
        [ $? == 0 ] && CATEGORY="$tmp_category"
        set_category "$CATEGORY"    # カテゴリ設定
        if [ "$CATEGORY" == "Vine" ]; then
            out_dir="$o_dir/Vine"
            if [ ! -d "$out_dir" ]; then
                mkdir "$out_dir"
            fi
        fi
        if [ -n "`echo $i | grep -i kde4`" ]; then
            out_dir="$o_dir/KDE4"
            KDE4_MENU=true
            INDENT="    "
            if [ ! -d "$out_dir" ]; then
                mkdir "$out_dir"
            fi
        elif [ -n "`echo $i | grep -i kde`" ]; then
            out_dir="$o_dir/KDE"
            KDE_MENU=true
            INDENT="    "
            if [ ! -d "$out_dir" ]; then
                mkdir "$out_dir"
            fi
        fi
        if [ -n "`echo "$tmp_category" | grep -i xfce`" ]; then
            out_dir="$o_dir/XFCE"
            XFCE_MENU=TRUE
            INDENT="    "
            if [ ! -d "$out_dir" ]; then
                mkdir "$out_dir"
            fi
        fi
        if [ "$SYSPROG" == "false" ]; then
            case "$CATEGORY" in
                Settings|Core|Information|System)
                    continue;;
            esac
        fi
        # Set Menu title (Name key)
        NAME=`grep -i "^Name\[ja\]=" $i | cut -d "=" -f2`
        if [ -z "$NAME" ]; then
            NAME=`grep -i "^Name=" $i | cut -d "=" -f2`
        fi
        [ -z "$NAME" ] && continue # broken sy
        if [ -n "`echo "$NAME" grep "("`" ]; then
            NAME=`echo "$NAME" | sed "s/(/[/" | sed "s/)/]/"`
        fi
        # Set Menu Icon (Icon key)
        ICON=""
        if [ -n "$ICON_MODE" ]; then
            search_icon $i    # アイコン検索
        fi
        # Output to temporally file
        out_file="$out_dir/$CATEGORY".txt
        echo  "$INDENT    [exec] ($NAME) {"$EXEC"} $ICON" >> "$out_file"
    done

    # User Additional Apps
    out_dir="$o_dir"
    if [ -n "$BROWSERS$IMS$MAILERS$NEWS$P2PS$FTPS$SMBUTLS$ANALYZERS" ]; then
        for i in $BROWSERS $IMS $MAILERS $NEWS $P2PS $FTPS $SMBUTLS \
            $ANALYZERS; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> \
                    "$out_dir/Network.txt"
            fi
        done
    fi
    if [ -n "$BROWSERS_CLI$IMS_CLI$MAILERS_CLI$NEWS_CLI$P2PS_CLI\
$FTPS_CLI$SMBUTLS_CLI$ANALYZERS_CLI" ]; then
        for i in $BROWSERS_CLI $IMS_CLI $MAILERS_CLI $NEWS_CLI $P2PS_CLI \
            $FTPS_CLI $SMBUTLS_CLI $ANALYZERS_CLI; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i)  {${DEFAULT_TERM} -e $i} $ICON" \
                    >> "$out_dir/Network.txt"
            fi
        done
    fi

    if [ -n "$EDITORS" ]; then
        for i in $EDITORS; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> "$out_dir/Editor.txt"
            fi
        done
    fi
    if [ -n "$EDITORS_CLI" ]; then
        for i in $EDITORS_CLI; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i)  {${DEFAULT_TERM} -e $i} $ICON" >> \
                    "$out_dir/Editor.txt"
            fi
        done
    fi

    if [ -n "$EDUCATIONS" ]; then
        for i in $EDUCATIONS; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> "$out_dir/Education.txt"
            fi
        done
    fi
    if [ -n "$EDUCATIONS_CLI" ]; then
        for i in $EDUCATIONS_CLI; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i)  {${DEFAULT_TERM} -e $i} $ICON" \
                    "$out_dir/Education.txt"
            fi
        done
    fi

    if [ -n "$FILERS$UTILITIES" ]; then
        for i in $FILERS $UTILITIES ; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> "$out_dir/Utility.txt"
            fi
        done
    fi
    if [ -n "$FILERS_CLI$UTILITIES_CLI" ]; then
        for i in $FILERS_CLI $UTILITIES_CLI; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i)  {${DEFAULT_TERM} -e $i} $ICON" \
                    "$out_dir/Utility.txt"
            fi
        done
    fi

    if [ -n "$GRAPHICS" ]; then
        for i in $GRAPHICS ; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> "$out_dir/Graphics.txt"
            fi
        done
    fi

    if [ -n "$AUDIOS$VIDEOS" ]; then
        for i in $AUDIOS $VIDEOS; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> "$out_dir/AudioVideo.txt"
            fi
        done
    fi
    if [ -n "$AUDIOS_CLI" ]; then
        for i in $AUDIOS_CLI; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> "$out_dir/AudioVideo.txt"
            fi
        done
    fi

    if [ -n "$OFFICES" ]; then
        for i in $OFFICES; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> "$out_dir/Office.txt"
            fi
        done
    fi

    if [ -n "$GAMES" ]; then
        for i in $GAMES; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> "$out_dir/Game.txt"
            fi
        done
    fi
    if [ -n "$GAMES_CLI" ]; then
        for i in $GAMES_CLI; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> "$out_dir/Game.txt"
            fi
        done
    fi

    if [ -n "$SYSTOOLS$BURNINGS" ]; then
        for i in $SYSTOOLS $BURNINGS; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> "$out_dir/System.txt"
            fi
        done
    fi
    if [ -n "$SYSTOOLS_CLI$BURNINGS_CLI" ]; then
        for i in $SYSTOOLS_CLI $BURNINGS_CLI; do
            hash "$i" 2> /dev/null
            if [ $? == 0 ]; then
                search_icon $i
                echo "    [exec] ($i) {"$i"} $ICON" >> "$out_dir/System.txt"
            fi
        done
    fi

    # Generate menu
    CATEGORIES="\
ネットワーク:Network グラフィック:Graphics オーディオ・ビデオ:AudioVideo \
エディタ:Editor オフィス:Office 開発:Development ゲーム:Game \
教育:Education アミューズメント:Amusement ユーティリテイ:Utility"
    if [ "$SYSPROG" == "true" ]; then
        CATEGORIES="\
$CATEGORIES セッティング:Settings コア:Core 情報:Information \
システム:System ETC:Etc 
"
    fi
    # general menu
    echo "[separator]" >> $OUT_FILE
    echo "$CATEGORIES" | while read -d " " i; do
###     echo -n "$i "
        TITLE=`echo $i | cut -d ":" -f 1`
        CATEG=`echo $i | cut -d ":" -f 2`
        if [ -f "$out_dir/$CATEG".txt ]; then
            echo "[submenu] ($TITLE)" >> $OUT_FILE
            cat "$out_dir/$CATEG".txt | sort >> $OUT_FILE
            echo "[end]" >> $OUT_FILE
        fi
    done
    # Vine menu
    if [ -d $out_dir/Vine ]; then
        echo "[submenu] (Vine)" >> $OUT_FILE
        cat "$out_dir"/Vine/Vine.txt >> $OUT_FILE
        echo "[end]" >> $OUT_FILE
    fi
    # KDE4 menu
    if [ -d $out_dir/KDE4 ]; then
        echo "[submenu] (KDE4)" >> $OUT_FILE
        echo $CATEGORIES | while read -d " " i; do
            TITLE=`echo $i | cut -d ":" -f 1`
            CATEG=`echo $i | cut -d ":" -f 2`
            if [ -f "$out_dir/KDE4/$CATEG".txt ]; then
                echo "    [submenu] ($TITLE)" >> $OUT_FILE
                cat "$out_dir/KDE4/$CATEG".txt | sort >> $OUT_FILE
                echo "    [end]" >> $OUT_FILE
            fi
        done
        echo "[end]" >> $OUT_FILE
    fi
    # KDE menu
    if [ -d $out_dir/KDE ]; then
        echo "[submenu] (KDE)" >> $OUT_FILE
        echo $CATEGORIES | while read -d " " i; do
            TITLE=`echo $i | cut -d ":" -f 1`
            CATEG=`echo $i | cut -d ":" -f 2`
            if [ -f "$out_dir/KDE/$CATEG".txt ]; then
                echo "    [submenu] ($TITLE)" >> $OUT_FILE
                cat "$out_dir/KDE/$CATEG".txt | sort >> $OUT_FILE
                echo "    [end]" >> $OUT_FILE
            fi
        done
        echo "[end]" >> $OUT_FILE
    fi
    if [ -d $out_dir/XFCE ]; then
        echo "[submenu] (XFCE)" >> $OUT_FILE
        echo $CATEGORIES | while read -d " " i; do
            TITLE=`echo $i | cut -d ":" -f 1`
            CATEG=`echo $i | cut -d ":" -f 2`
            if [ -f "$out_dir/XFCE/$CATEG".txt ]; then
                echo "    [submenu] ($TITLE)" >> $OUT_FILE
                cat "$out_dir/XFCE/$CATEG".txt | sort >> $OUT_FILE
                echo "    [end]" >> $OUT_FILE
            fi
        done
        echo "[end]" >> $OUT_FILE
    fi
    echo "[separator]" >> $OUT_FILE
    rm -rf "$out_dir"
    return 0
}

# 引数チェック
check_args()
{
    for i in $@; do
        case $i in
            -a)
                # cancel sys prog
                SYSPROG=false;;
            -in)
                ICON_MODE=""
        esac
    done
}


# start -------------------------------------------
extend_menu_gen()
{
    SYSPROG=true
    ICON_MODE=true
    OUT_FILE=${MENUFILENAME}
    check_args $@      # 引数チェック

    menu_gen $SYSPROG
}
