#!/bin/sh
# AppDirGen AppRun

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
}

browser_select()
{
    for browser in firefox konquero seamonkey kazehakase galeon w3m ; do
        if `type $browser 1>& /dev/null` ; then
            if [ $browser == w3m ] ; then
		        browser="xterm -e w3m"
		    fi
		    break;
	    fi
    done
}

readme()
{
    browser_select
    pushd Help
    $browser index.html
    popd
}

exec_command()
{
    term_select
    $terminal $opt $cmd $cmdopt
}

# script start

cd `dirname $0`

if [ $# == 1 ]; then
    case $1 in
        --readme)
            readme
            exit
            ;;
        --force)
            cp app_tree app_tree.refresh
            echo > app_tree
            cmdopt=$1
            ;;
        --extend|--revert)
            cmd="./rox-extend"
            cmdopt=$1
            exec_command
            exit
            ;;
    esac
fi

if ! [ -f app_tree ]; then
    cmd="./notification.sh"
    cmdopt="--first"
    exec_command
    exit
fi

tree /usr/share/applications/ > app_tree.tmp
# /usr/share/applications/ å
if ! diff -q app_tree app_tree.tmp > /dev/null; then
    # ѹͭäȤν
    cmd="./notification.sh"
    exec_command
elif ! [ -d ../Applications ]; then
    # ~/Apps/Applications ¸ߤʤν
    cmd="./notification.sh"
    exec_command
else
    # ѹ̵Ȥν
    cmd="./nochange.sh"
    exec_command
    rm app_tree.tmp 2> /dev/null
fi

