#!/bin/bash

### mxfb-menu-generator: a tiny script to Create a (non dynamic) fluxbox menu using information on .desktop files, released by PPC, 7/10/2020, under GPLv3
### Modified by MX Linux Devs for use in MX-Fluxbox August 2021

### How to use this script:
### 1. Copy the script below and paste it into your text editor, saving it in your Home as MXFB_menu_generator.sh
### 2. Navigate to that file, right click it > Proprieties > "Permissions" tab > Check the last field, to allow this script to run as a program
### 3. Add the following entry to your menu to access "All Categories" anywhere you want 
#######################################################################################   
#[submenu] (All Apps)
#       [include] (~/.fluxbox/submenus/full_menu) 
#       [separator]
#       [include] (~/.fluxbox/full_menu)
#   [end]
#######################################################################################
### 4.- Now click Menu > All apps > Update menu. Wait for some seconds while the menu is generated

#exit if in chroot
if [ "$UID" = "0" ]; then
  echo "mxfb-menu-generator:  in a chroot, exiting..."
  exit 0
fi

#exit if menu generation toggled off
if [ "$1" = "auto" ]; then
    if [ -e "$HOME/.fluxbox/mxfb-menu-generator-disabled.chk" ]; then
    exit 0
    fi
fi

Encoding=UTF-8
#
# i18n - Internationalization
export TEXTDOMAIN=mxfb-accessories
export TEXTDOMAINDIR="/usr/share/locale"

CATEGORIES="Accessories Development Games Graphics Internet Multimedia Office System Settings"

echo Please wait, creating menu file - stored in ~/.fluxbox/submenus/full_menu ...

if [ ! -d $HOME/.fluxbox ]; then
	mkdir -p $HOME/.fluxbox
fi


cd $HOME/.fluxbox/
mkdir -p submenus
touch submenus/full_menu
FILE="$HOME/.fluxbox/submenus/full_menu"

#echo
#echo If you want to insert this 'All categories'  sub-menu into your MXFB menu:
#echo 'rootMenu > Settings > Configure > Menu'
#echo and insert, anywhere you want, this code:
#echo  ' [submenu] (All apps)'
#echo  '  [include] (~/.fluxbox/submenus/full_menu)'
#echo  '  [separator] '
#echo  '  [exec] (Update Menu) {mxfb-menu-generator} '
#echo  ' [end]'
#echo


#Get system language (to allow localization):
lang=$(locale | grep LANG | cut -d= -f2 | cut -d_ -f1)
#Loop through all .desktop files in the applications folder
#lang=fr

LIST="/usr/share/applications/*.desktop"

if [ -n "$(find /var/lib/flatpak/exports/share/applications/ -name *.desktop 2>/dev/null)" ]; then
	LIST="$LIST /var/lib/flatpak/exports/share/applications/*.desktop"
fi

if [ -n "$(find $HOME/.local/share/applications/ -name *.desktop 2>/dev/null)" ]; then
    LIST="$LIST $HOME/.local/share/applications/*.desktop"
fi

if [ -n "$(find $HOME/.local/share/flatpak/exports/share/applications/ -name *.desktop 2>/dev/null)" ]; then
    LIST="$LIST $HOME/.local/share/flatpak/exports/share/applications/*.desktop"
fi

if [ -n "$(find /usr/share/applications/antix/ -name *.desktop 2>/dev/null)" ]; then
    LIST="$LIST /usr/share/applications/antix/*.desktop"
fi

for file in $LIST
do

if [ -f "$file" ]; then
name1=$(grep -o -m 1 '^Name=.*' "$file")
 ### localized menu entries generator (slows the script down, but produces nearly perfect localized menus):
    name2=$name1
	translated_name1=$(grep -o -m 1 "^Name\[$lang\]=.*" "$file")
	[ -z "$translated_name1" ] && note="No localized name found, using the original one" || name2=$translated_name1
	#if the desktop file has the string "Desktop Action" simple use the original untranslated name, to avoid using a translation that's not the name of the app, but the action it does
	grep -q "Desktop Action" "$file" && name2=$name1
    name1=$name2
### end of localized menu entries generator	 

name=$(echo $name1|sed 's/.*\=//') 
command1=$(grep -o -m 1 'Exec=.*' "$file")
command=$(echo $command1|sed -E 's/Exec\=//g')
terminal=$(grep -o -m 1 'Terminal=.*' "$file")
  if [[ $terminal == *"true"* ]]; then
   command=$(echo xfce4-terminal -e $command1|sed -E 's/Exec\=//g') 
  fi
 categories=$(grep -o -m 1 'Categories=.*' "$file")

 nodisplay=$(grep -o -m 1 'NoDisplay=.*' "$file")
  if [[ $nodisplay == *"rue"* ]]; then
    Note="not adding this entry to menu because it has a nodisplay flag"
   else
    echo "[exec] ("$name ")" "{" $command "}" $categories
  fi
  fi
done > /tmp/list.txt
sort /tmp/list.txt > ~/.fluxbox/pre-global-menu.txt
rm $FILE
#fix for repeated synaptic menu entry- first instance does not run, so, delete it:
sed -i '/{ synaptic }/d' ~/.fluxbox/pre-global-menu.txt

#### Now divide applications into categories:
#This array has all the available .desktop file categories we want to have on the menu (note: the "." is a quick and dirty workaround, so the script ignores entry nr 0):
array1=( . Utility Development Game Graphics Network AudioVideo Office System Settings)
#This array corresponds to the previous one, but it's the "user friendly name" that shows on the menu- NOTE: this can be translated to any language!
array2=( . $CATEGORIES)

#Begin LOCALIZATION OF Category sub-menus to a language - pt, add equivalent section for other languages:
#    if [[ $lang == *"pt"* ]];
#    then
#		array2=( . Acessórios Desenvolvimento Jogos Graficos Internet Multimédia Escritório Sistema 'Definições globais')
#		echo A criar submenus em Português
#    fi
#End LOCALIZATION OF Category sub-menus
    
#Create the header of the file
echo '[begin] (All Categories)' >> $FILE
#Loop through the array- the last number below has to match the number of entries on $array1
for i in {1..9};
do 
 #Create a submenu entry for the current item on $array2
 echo '[submenu] ('${array2[$i]}')'  >> $FILE
#Nested loop to check if the item on the current line of the "pre-global-menu.txt" file matches the current item on $array1, if it matches, add it to the "$FILE" file 
 while read p; 	do
		if [[ $p =~ ${array1[$i]} ]]
			then
				echo $p >> $FILE
		fi
	done <~/.fluxbox/pre-global-menu.txt ;
 echo '[end]' >> $FILE ;	  
done

#Process only entries that don't fit anywhere else:
sed -i '/Utility/d' ~/.fluxbox/pre-global-menu.txt
sed -i '/Development/d' ~/.fluxbox/pre-global-menu.txt
sed -i '/Game/d' ~/.fluxbox/pre-global-menu.txt
sed -i '/Graphics/d' ~/.fluxbox/pre-global-menu.txt
sed -i '/AudioVideo/d' ~/.fluxbox/pre-global-menu.txt
sed -i '/Office/d' ~/.fluxbox/pre-global-menu.txt
sed -i '/System/d' ~/.fluxbox/pre-global-menu.txt
sed -i '/Settings/d' ~/.fluxbox/pre-global-menu.txt
sed -i '/Network/d' ~/.fluxbox/pre-global-menu.txt

#check if the file is not empty, create last submenu and populate it
file="~/.fluxbox/pre-global-menu.txt"
if [ -s "$file" ]
then 
 note="empty file, do nothing"
else
echo '[submenu] ('Other applications')'  >> $FILE
  while read p; 	do
		echo $p >> $FILE
  done <~/.fluxbox/pre-global-menu.txt ;
echo '[end]' >> $FILE
fi

###Fix menu errors, so Libreoffice, etc, work without errors
delete="%U"
sed -e s/$delete//g -i $FILE
delete="%u"
sed -e s/$delete//g -i $FILE
delete="%F"
sed -e s/$delete//g -i $FILE
delete="%f"
sed -e s/$delete//g -i $FILE
echo Menu file created and ready to be used

#if fluxbox running, reload menu

test=$(ps -aux)
if [[ "$test" == *"startfluxbox" ]]; then
	fluxbox-remote restart
  killall -SIGUSR1 conky 2>/dev/null
fi
exit 0

