# This file is originally called 'genconf' in the src, but it needs to
# run on the machine that this actually gets installed on, so it will
# be an install script here...

case "$ACTION" in
    post)
        mkdir -p etc/xlunch
        APPS=/usr/share/applications
        ICONS=/usr/share/icons/hicolor
        SIZE=48

        echo "generating config file from $APPS ..." >&2


        find $APPS | fgrep .desktop | while read DESKTOPFILE; do
            NAME="$(cat $DESKTOPFILE | egrep -i "^Name=" | head -n 1 | cut -d "=" -f 2-)"
            EXEC="$(cat $DESKTOPFILE | egrep -i "^Exec=" | head -n 1 | cut -d "=" -f 2-)"
            ICON="$(cat $DESKTOPFILE | egrep -i "^Icon=" | head -n 1 | cut -d "=" -f 2-)"
            TERM="$(cat $DESKTOPFILE | egrep -i "^Terminal=" | head -n 1 | cut -d "=" -f 2-)"

            if [ "$TERM" = "true" ]; then
                EXEC="xterm -e ""$EXEC"
            fi

            if [ "$ICON" != "" ]; then
                ICON=$(find "$ICONS" | grep $SIZE"x"$SIZE | grep $ICON"[.]png" | head -n 1)
                if [ "$ICON" != "" -a "$EXEC" != "" -a "$NAME" != "" ]; then
                    echo "$NAME;$ICON;$EXEC"
                fi
            fi
        done | sort | uniq > etc/xlunch/entries.dsv
        ;;
esac
