#!/usr/bin/env dash
# the main script for starting and accessing my instantmenus
PAPERAPP=$(cat /opt/instantos/menus/apps | instantmenu -n -h 32 -F)
[ -z "$PAPERAPP" ] && exit

TEMP=$(ls /opt/instantos/menus/dm/$PAPERAPP*.sh | sed 's/^.*\///g' | grep -o '^[^\.]*' | grep "^$PAPERAPP" | grep -o '.$' | uniq)

exescript() {
    [ -e /opt/instantos/menus/dm/$1.sh ] &&
        echo "executing $1" &&
        bash /opt/instantos/menus/dm/$1.sh && exit
}

if [ -n "$TEMP" ] && [ $(echo "$TEMP" | wc -l) -gt "1" ]; then
    if [ -e /opt/instantos/menus/dm/$PAPERAPP$PAPERAPP.sh ]; then
        APP2=$(echo "$TEMP" | instantmenu -n -h 32 -F)
    else
        APP2=$(echo "$TEMP" | instantmenu -n -h 32 -F)
    fi

    if [ -n "$APP2" ]; then
        exescript $PAPERAPP$APP2
    else
        exescript $PAPERAPP$PAPERAPP
        exescript $PAPERAPP
    fi
else
    exescript $PAPERAPP$PAPERAPP
    exescript $PAPERAPP
fi
