#!/bin/bash
#Script to localize the first layer of antix menus (also restores the first layer of the menus to their default configuration)
# By PPC, 11/3/2024, full GPL license, meant for use with antiX Linux. Please keep this attribution
#
# Localization for this script:
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=translate-menus-antix

#Get full locale code (like "pt_br")
lang1=$(locale | grep LANGUAGE= | cut -d= -f2| cut -d. -f1)
#Get locale for languages without underscore (like "pt")
lang2=$(locale | grep LANGUAGE= | cut -d= -f2| cut -d. -f1| cut -d_ -f1)

#Create copy of a menu file, for comparing later
cp ~/.icewm/menu /tmp/_icewm_menu_backup

#change to the full locale code folder, if it does not exist, try to change to que smaller locale folder, without underscore
cd /usr/share/antiX/localisation/$lang1 || cd /usr/share/antiX/localisation/$lang2

#if menu files for IceWM exist in the templates folder, copy them to the user's configuration folder
if [ -f "./icewm/menu" ]; then cp ./icewm/menu ~/.icewm/; fi
if [ -f "./icewm/menu-papirus" ]; then cp ./icewm/menu-papirus ~/.icewm/; fi
if [ -f "./icewm/menu-numix-bevel" ]; then cp ./icewm/menu-numix-bevel ~/.icewm/; fi
if [ -f "./icewm/menu-numix-square" ]; then cp ./icewm/menu-numix-square ~/.icewm/; fi

#if menu a file for Fluxbox exists in the templates folder, copy them to the user's configuration folder
if [ -f "./fluxbox/menu" ]; then cp ./fluxbox/menu ~/.fluxbox/; fi

#if menu files for JWM exist in the templates folder, copy them to the user's configuration folder
if [ -f "./jwm/menu" ]; then cp ./fluxbox/menu ~/.jwm/; fi
if [ -f "./jwm/menu-numix-bevel" ]; then cp ./jwm/menu-numix-bevel ~/.jwm/; fi
if [ -f "./jwm/menu-numix-square" ]; then cp ./jwm/menu-numix-square ~/.jwm/; fi
if [ -f "./jwm/menu-papirus" ]; then cp ./jwm/menu-papirus ~/.jwm/; fi

# Remove app-select if not installed
if [ ! -x "/usr/local/bin/app-select" ]; then
        sed -i "/app-select/d" ~/.icewm/menu 
        sed -i "/app-select/d" ~/.icewm/menu-papirus 
        sed -i "/app-select/d" ~/.icewm/menu-numix-bevel 
        sed -i "/app-select/d" ~/.icewm/menu-numix-square
		sed -i "/app-select/d" ~/.fluxbox/menu 
		sed -i "/app-select/d" ~/.jwm/menu 
fi

#Checks for live mode, in order to remove the "install" menu entry (CURRENTLY NOT WORKING)#####:
live=0
# Check for /run/live directory
if [ -d "/run/live" ]; then
    #echo "Running in live mode (detected /run/live directory)."
    live=1
fi
# Check for 'live' kernel parameter in /proc/cmdline
if grep -q 'live' /proc/cmdline; then
    #echo "Running in live mode (detected 'live' kernel parameter)."
    live=1
fi
#If not in live mode, remove the "install" menu entry
if [ "$live" -eq 0 ]; then
    #not running in live mode:
    echo System is not running in live mode
    #sed -i "/minstall/d" ~/.icewm/menu
   else 
    #running in live mode:
    echo System is running in live mode, so, not touching the Install menu entry
fi

#Refresh the (all) Applications sub-menu:
desktop-menu --write-out-global

#Compare initial config file and current config file to check if the procedure was sucefull:
cmp -s ~/.icewm/menu /tmp/_icewm_menu_backup

if [ $? -eq 0 ]; then
    #The files are exactly the same:  
    yad --center --borders=10 --window-icon=preferences-desktop-locale --title="antiX" --text=$"Your menu seems not to have changed." --button=" x " --timeout=10 --timeout-indicator=bottom

else
    #The files are different:
    yad --center --borders=10 --window-icon=preferences-desktop-locale --title="antiX" --text=$"Your menu seems to have been correctly localized." --button=" x " --timeout=10 --timeout-indicator=bottom
fi

exit