#!/bin/sh
#
# GNOME AppDir generator
# (use *.desktop file registered in /usr/share/applications/)
#

term_select()
{
    for terminal in mlterm gnome-terminal konsole terminal xterm kterm ;  do
        if [ -x `which $terminal` ]; then
            case $terminal in
                gnome-terminal|terminal)
                    opt="-x"
                    ;;
                *)
                    opt="-e"
                    ;;
            esac
            break
        fi
    done
}

set_category()
{
    DIRICON=""

    if [ -n "`echo $1 | grep Vine`" ]; then
        category=""
    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"
    else
        category="Etc"
    fi

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

    if [ -n "`echo $1 | grep XFCE`" ]; then
        category=XFCE/"$category"
    fi

}

app_search()
{
    for app in *.desktop; do
        # AppDir name setting
        app_name=`echo "$app" | sed "s/.desktop$//"`

        # AppRun command-line setting (from "Exec" value of *.desktop file)
        command_line=`cat "$app" | grep -e ^Exec | cut -d "=" -f 2`
        command_line=`echo "$command_line" | sed "s/\%[FfimUu]//g"`
        # extracting command name (from command-line)
        exec_command=`echo "$command_line" | cut -d " " -f1`

        # add terminal to command line for CLI apps
        if ! [ -z "`cat "$app" | grep "Terminal=true"`" ]; then
            command_line="$terminal $opt ""$command_line"
        fi
        if ! [ -z "`cat "$app" | grep sbin`" ]; then
            command_line="gksu ""\"$command_line\""
        fi
        # generate AppDir (if command is exists)
        if ! [ -z "$exec_command" ]; then
            if [ `which "$exec_command" 2> /dev/null` ]; then
                echo -n " $app_name"

                # AppDir path setting (from "Categories" value)
                dir=`cat "$app" | grep -e ^Categories | cut -d '=' -f 2 | sed "s/;/\//g"`
                dir="$dir"/
                dir=`echo "$dir" | sed "s/\/\//\//g"`
                set_category "$dir"
                app_path="$APP_DIR"/"$category"/"$app_name"
                ### echo $app_path

                # search app icon
                if [ -f $HOME/.icons/AppIcons/"$exec_command".png ]; then
                    icon=$HOME/.icons/AppIcons/"$exec_command".png
                else
                    icon=`cat "$app" | grep -e ^Icon | cut -d '=' -f 2`
                    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/kdeclassic/48x48/apps/"$icon" ]; then
                            icon=/usr/share/icons/kdeclassic/48x48/apps/"$icon"
                        elif [ -f /usr/share/icons/crystalsvg/48x48/apps/"$icon" ]; then
                            icon=/usr/share/icons/crystalsvg/48x48/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/"`
                            if [ -n "`find /usr/share/icons/gnome/scalable/ -name "$icon"`" ]; then
                                icon=`find /usr/share/icons/gnome/scalable/ -name "$icon"`
                            fi
                        fi
                    fi
                fi

                # make AppDir directory
                mkdir -p "$app_path"
                if ! [ -f "$APP_DIR"/"$category"/.DirIcon ]; then
                    cp "$DIRICON" "$APP_DIR"/"$category"/.DirIcon
                    cp "$curdir"/app_run "$APP_DIR"/"$category"/AppRun
                fi
                echo -n "."
                echo "#!/bin/sh" > "$app_path"/AppRun
                echo exec "$command_line" \$\@ >> "$app_path"/AppRun
                chmod u+x "$app_path"/AppRun

                # make AppInfo.xml
                comment=`cat "$app" | grep -e "^Comment\[ja\]\="`
                comment=`echo "$comment" | nkf -w`
                comment=`echo "$comment" | sed "s/\&/＆/g"`
                if [ -z "$comment" ]; then
                    comment=`cat "$app" | grep -e ^Comment\=`
                fi
                if [ -z "$comment" ]; then
                    comment=`cat "$app" | grep -e "^Name\[ja\]\="`
                    comment=`echo "$comment" | nkf -w`
                    comment=`echo "$comment" | sed "s/\&/＆/g"`
                fi
                if [ -z "$comment" ]; then
                    comment=`cat "$app" | grep -e ^Name\=`
                fi
                if ! [ -z "$comment" ]; then
                    comment=`echo "$comment" | cut -d "=" -f 2`
                    echo "<?xml version=\"1.0\"?>" > "$app_path"/AppInfo.xml
                    echo "<AppInfo>" >> "$app_path"/AppInfo.xml
                    echo "  <About>" >> "$app_path"/AppInfo.xml
                    echo "    <Purpose>""$comment""</Purpose>" >> "$app_path"/AppInfo.xml
                    echo "  </About>" >> "$app_path"/AppInfo.xml
                    echo "  <Summary>""$comment""</Summary>" >> "$app_path"/AppInfo.xml
                    echo "</AppInfo>" >> "$app_path"/AppInfo.xml
                fi

                # make .DirIcon
                if [ -f "$icon" ]; then
                    ### echo icon = "$icon"
                    cp "$icon" "$app_path"/".DirIcon"
                fi
            fi
        fi
    done
}

kde_app_search()
{
    # KDE AppDir generate
    # (use *.desktop file registered in /usr/share/applications/kde)

    if [ -d /usr/share/applications/kde ]; then
        APP_DIR=$HOME/Apps/Applications/KDE
        rm -rf "$APP_DIR"
        mkdir -p "$APP_DIR"
        cp $kde_icon "$APP_DIR"/.DirIcon
        pushd /usr/share/applications/kde > /dev/null
        app_search
        popd > /dev/null
        echo
    fi # End of KDE AppDir generate
}

vine_app_search()
{
    # Vine AppDir generate
    # (use *.desktop file registered in /usr/share/applications/Vine)

    if [ -d /usr/share/applications/Vine ]; then
        APP_DIR=$HOME/Apps/Applications/Vine
        rm -rf "$APP_DIR"
        mkdir -p "$APP_DIR"
        cp $vine_icon "$APP_DIR"/.DirIcon
        pushd /usr/share/applications/Vine > /dev/null
        app_search
        popd > /dev/null
    fi
}

make_appinfo()
{
    for dir in "$1"/* ; do
        if [ -d "$dir" ]; then
            case `basename "$dir"` in
                KDE|XFCE)
                    make_appinfo  "$dir"
                    ;;
            esac
            pushd "$dir"
            echo "<?xml version=\"1.0\"?>" > AppInfo.xml
            echo "<AppInfo>" >> AppInfo.xml
            echo -n "  <Summary>" >> AppInfo.xml
            a=0
            for i in *; do
                if [ -d $i ]; then
                    echo -en "$i\\t" >> AppInfo.xml
                    a=`expr $a + 1`
                    if [ $a == "4" ]; then
                        echo >> AppInfo.xml
                        a=0
                    fi
                fi
            done
#           echo >> AppInfo.xml
            echo "  </Summary>" >> AppInfo.xml
            echo "</AppInfo>" >> AppInfo.xml
            popd
        fi
    done


}

# Start

cd `dirname $0`
curdir=`pwd`
diricon="$curdir"/Applications.png
kde_icon="$curdir"/KDE.png
xfce_icon="$curdir"/XFCE.png
vine_icon="$curdir"/VINE.png
APP_DIR=$HOME/Apps/Applications
term_select

rm -rf "$APP_DIR"
mkdir -p "$APP_DIR"
cp $diricon "$APP_DIR"/.DirIcon

cd /usr/share/applications/


app_search
if [ -d "$APP_DIR"/XFCE ]; then
    cp "$xfce_icon" "$APP_DIR"/XFCE/.DirIcon
fi
echo
echo
kde_app_search
echo
echo
vine_app_search
APP_DIR=$HOME/Apps/Applications
make_appinfo "$APP_DIR"

# End of AppsGen
