#!/bin/bash
#Name: remove-desktop
#Version: 1.4
#Author: Dave (david@daveserver.info)
#Purpose: Make and add .desktop files to the desktop, applications menu, or personal menu
#License: gplv3

TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=remove-desktop 

function checkcancel {
    CANOK="$?";
    if [ "$CANOK" = "1" ]; then
    exit;
    fi
}

GetDesktopInformation() {
    for file in $(ls $FILE_LOCATION | grep ".desktop")
    do
    echo "$file"
        if [ "$CATEGORIES" = "X-Personal" ]; then
            if grep "X-Personal" $FILE_LOCATION/$file 1>/dev/null ; then
                echo "$file"
            fi
        fi
    done | yad --title=$"Menu Manager" --center --width="400" --height="300" --text=$"Select the items to remove" --list --separator="" --column=$"Menu Files"
    checkcancel;
}

function UpdateMenus() {
    case $1 in
        0) true ;;
        1)  ans=`yad --image dialog-question --title Alert --button=gtk-yes:0 --button=gtk-no:1 --text $"Disable the automatic applications menu and utilize custom user applications menu?"`;
            checkcancel;
            rm $HOME/.$wm/menu-applications;
            desktop-menu --write-out;
            ;;
        2) desktop-menu --menu-file "/etc/xdg/menus/TCM-MENU.menu" --write-out --write-out-file personal ;;
    esac
}

SELECTED=$(yad --title=$"Desktop Manager" --center --width="400" --height="300" --text=$"Select the location you would like to remove the desktop file from"  --item-separator="|" --form --field=$"Item Location:CB" $"1) Application Menu|2) Desktop Shortcut|3) Personal")
checkcancel;

case $(echo $SELECTED | cut -d ")" -f1) in
"1") #Application Menu 
    FILE_LOCATION="$HOME/.local/share/applications/"
    CATEGORIES=""
    MENU_UPDATE=1
    ;;
"2") #Desktop Shortcut
    FILE_LOCATION="$HOME/Desktop"
    CATEGORIES=""
    MENU_UPDATE=0
    ;;
"3") #Task Centric Menu
    FILE_LOCATION="$HOME/.local/share/applications/TCM"
    CATEGORIES="X-Personal"
    MENU_UPDATE=2
    ;;
esac

SELECTED=$(GetDesktopInformation)
if [ -n "$SELECTED" ]; then
    rm "$FILE_LOCATION/$SELECTED"
fi

UpdateMenus "$MENU_UPDATE"
