#!/bin/bash
# -*- ENCODING: UTF-8 -*-

# menugen
#
# Generate menu entries.
#
# Copyright (c) 2013-2026: Alexis Puente Montiel   < pica (a) picalibre.org >
#
# Licensed according to GNU AGPL version 3.0.
#
# It is libre/free software; you can use, redistribute and/or modify it according to the terms of GNU AGPL as published by GNU, version 3.0, 19 November 2007.
#
# It is distributed in the hope that it will be useful, but without any warranty. Read GNU AGPL version 3.0 for additional details.
#
# A copy of GNU AGPL version 3.0 is available at /usr/share/doc/<software-package-name>/agpl-3.0.txt (additionally on Internet as text at https://www.gnu.org/licenses/agpl-3.0.txt and as HTML at https://www.gnu.org/licenses/agpl-3.0-standalone.html ).
#
# Note: Additionally to the official e-mails, picalibre.org is strictly the only official site for this software project, please consider using it to download, report bugs and contribute.
#
# Depends: bash, coreutils, findutils, gawk | mawk | original-awk, graphicsmagick-imagemagick-compat, grep, menu, pica-apt, sed
# Recommends: x11-xkb-utils
#
# View https://bugs.debian.org/815184 https://bugs.debian.org/741573


### SCRIPT VARIABLES ########################################

CNAME="menugen"
VERSION="1.3.1"
TITLE="MenuGen"

# Translations
if [ "$LANG" = "" ] ; then export $(cat /etc/default/locale | grep -a 'LANG=') ; fi
TEXTDOMAIN=pica-skel
TEXTDOMAINDIR=/usr/share/locale/

# Write errors to log
ERRORLOG="$HOME/.${CNAME}.log"
if [ -e "$ERRORLOG" ] ; then
	mv -f $ERRORLOG ${ERRORLOG}.ant
fi
if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi

for i in /etc/pica-global.dist /etc/pica-global.orig /etc/pica-global /etc/pica-global.local ~/.pica-global ~/pica-global ; do
	if [ -f "$i" ] ; then
	cat "$i"
	source "$i"
	source <(cat $i | sed -e "s/=\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)/=Y/g" -e "s/=\(No\|no\|n\)/=N/g" -e "s/=\"\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)\"/=Y/g" -e "s/=\"\(No\|no\|n\)\"/=N/g")
	fi
done
if [ "$DEBUG" = "Y" ] ; then
	set -xv
	DEBUG="Y"
else
	ERRORLOG="/tmp/.${CNAME}_$(id -nu).log"
	if [ -e "$ERRORLOG" ] ; then mv -f $ERRORLOG ${ERRORLOG}.ant ; fi
	if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi
fi
#if [ "$DEBUG" = "" ] ; then DEBUG="N" ; fi
if [ "$DEBUG" != "N" ] ; then
exec > >(tee -a "$ERRORLOG") 2>&1
echo "$0" "$*" >> "$ERRORLOG"
echo "$CNAME" "$VERSION" >> "$ERRORLOG"
echo $(date +%Y-%m-%d_%H:%M:%S) $"Start" >> "$ERRORLOG"
echo "env:" >> "$ERRORLOG"
env >> "$ERRORLOG"
echo "set:" >> "$ERRORLOG"
set >> "$ERRORLOG"
#else
#exec 2>>"$ERRORLOG"
fi

# Description:
BDESCRIP=$"Generate menu entries."
LDESCRIP=$"$TITLE is a tool to generate /usr/share/menu entries from /usr/share/applications."

# Documentation:
docu_info () {
echo "$CNAME ($VERSION) - $BDESCRIP"
echo 
echo $"Usage:" $CNAME [$"OPTIONS"]
echo 
echo $"Options:"
echo -e "$ODESCRIP"
echo 
echo $"'man $CNAME' for more information."
echo 
}
ODESCRIP=" -x""\t"$"Show help documentation."

while getopts x OPTION ; do
	case $OPTION in
		x )   docu_info ; exit 0 ;;
	esac
done


### ROOT CHECK ########################################

#if [ "$(cat /etc/passwd | grep -aE ^$(whoami): | cut -d ':' -f 3 )" -ne 0 ] ; then
if [ "$(id -u)" -ne 0 ] ; then
	echo $"$TITLE must be launched as SuperUser. Cannot continue."
#	$DIALOG $WICON $WTITLE "$TITLE" $ERROR $WTEXT $"$TITLE must be launched as SuperUser.\n\nCannot continue." $H $W
	exit 1
fi
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/trinity/bin


### SCRIPT ########################################

if [ "$MENU_PATH" = "" ] ; then
	MENU_PATH="/usr/share/menu"
fi

if [ "$DESKTOP_PATH_LIST" = "" ] ; then
	DESKTOP_PATH_LIST="/usr/share/applications /opt/trinity/share/applications/tde"
fi

if [ ! -e /usr/share/menugen/ ] ; then mkdir -p /usr/share/menugen/ ; fi


for DESKTOP_PATH in $DESKTOP_PATH_LIST ; do
	if [ -e "$DESKTOP_PATH" ] ; then
		ls $DESKTOP_PATH | grep -aEi "\.desktop$" | sed "s|.desktop$||g" | while read i ; do
		if [ -e $MENU_PATH/"$i" ] ; then
			# Exists:
			printf "\r%-77.77s" "$i" ; printf "\r"
		elif [ "$(cat "$DESKTOP_PATH"/"$i".desktop | grep -aEi 'NoDisplay=true')" != "" ] ; then
			# NoDisplay=true
			printf "\r%-77.77s" "$i" ; printf "\r"
		elif [ "$(cat "$DESKTOP_PATH"/"$i".desktop | grep -aEi 'tdecmshell')" != "" ] ; then
			# Discard:
			printf "\r%-77.77s" "$i" ; printf "\r"
		elif [ "$(command -v $i)" = "" ] ; then
			# Error
			printf "\r%-77.77s" "$i" ; printf "\r"
		else
			# NOT exist, created it:

			printf "$DESKTOP_PATH"/"$i".desktop'\t'"$MENU_PATH"/"$i"'\n' | tee -a /usr/share/menugen/menugen_create

			printf '?package('"$(app o $(command -v $i) | cut -d ':' -f 1)"'): \\''\n' >> "$MENU_PATH"/"$i"

			printf '\t''needs="X11" \\''\n'   >> "$MENU_PATH"/"$i"

			CATEGORIES=$(cat $DESKTOP_PATH/"$i".desktop | grep -a Categories)

				if [ "$(echo $CATEGORIES | grep -ai Math )" != "" ] ; then
					printf '\t''section="Applications/Science/Mathematics" \\''\n'  >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Electronics )" != "" ] ; then
					printf '\t''section="Applications/Science/Electronics" \\''\n'  >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Engineering )" != "" ] ; then
					printf '\t''section="Applications/Science/Engineering" \\''\n'  >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Geography )" != "" ] ; then
					printf '\t''section="Applications/Science/Geoscience" \\''\n'   >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Astronomy )" != "" ] ; then
					printf '\t''section="Applications/Science/Astronomy"  \\''\n'   >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Science )" != "" ] ; then
					printf '\t''section="Applications/Science/Data Analysis"  \\''\n'       >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai P2P )" != "" ] ; then
					printf '\t''section="Applications/Network/File Transfer" \\''\n'		>> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai FileTransfer )" != "" ] ; then
					printf '\t''section="Applications/Network/File Transfer"  \\''\n'       >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Telephony )" != "" ] ; then
					printf '\t''section="Applications/Network/Communication"  \\''\n'       >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai VideoConference )" != "" ] ; then
					printf '\t''section="Applications/Network/Communication" \\''\n'		>> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai InstantMessaging )" != "" ] ; then
					printf '\t''section="Applications/Network/Communication"  \\''\n'       >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Email )" != "" ] ; then
					printf '\t''section="Applications/Network/Communication"  \\''\n'       >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai News )" != "" ] ; then
					printf '\t''section="Applications/Network/Communication" \\''\n'		>> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai WebBrowser )" != "" ] ; then
					printf '\t''section="Applications/Network/Web Browsing"  \\''\n'		>> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Network )" != "" ] ; then
					printf '\t''section="Applications/Network"  \\''\n'     >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai WebDevelopment )" != "" ] ; then
					printf '\t''section="Applications/Web Development" \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Viewer )" != "" ] ; then
					printf '\t''section="Applications/Viewers" \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Graphics )" != "" ] ; then
					printf '\t''section="Applications/Graphics" \\''\n'     >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Scanning )" != "" ] ; then
					printf '\t''section="Applications/Graphics"  \\''\n'    >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai OCR )" != "" ] ; then
					printf '\t''section="Applications/Graphics"  \\''\n'    >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai -a DiscBurning )" != "" ] ; then
					printf '\t''section="Applications/File Management" \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Audio )" != "" ] ; then
					printf '\t''section="Applications/Video" \\''\n'		>> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Video )" != "" ] ; then
					printf '\t''section="Applications/Video" \\''\n'		>> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Mixer )" != "" ] ; then
					printf '\t''section="Applications/Sound" \\''\n'		>> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai AudioVideo )" != "" ] ; then
					printf '\t''section="Applications/Video" \\''\n'		>> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Viewer )" != "" ] ; then
					printf '\t''section="Applications/Viewers" \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Accessibility )" != "" ] ; then
					printf '\t''section="Applications/Accessibility"  \\''\n'       >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Security )" != "" ] ; then
					printf '\t''section="Applications/System/Security" \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai TerminalEmulator )" != "" ] ; then
					printf '\t''section="Applications/Terminal Emulators" \\''\n'   >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Education )" != "" ] ; then
					printf '\t''section="Applications/Education"  \\''\n'   >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai BoardGame )" != "" ] ; then
					printf '\t''section="Games/Board" \\''\n'       >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Game )" != "" ] ; then
					printf '\t''section="Games" \\''\n'     >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai -a Development )" != "" ] ; then
					printf '\t''section="Applications/Programming" \\''\n'  >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai IDE )" != "" ] ; then
					printf '\t''section="Applications/Programming"  \\''\n' >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai FileManager )" != "" ] ; then
					printf '\t''section="Applications/File Management" \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai FileTools )" != "" ] ; then
					printf '\t''section="Applications/File Management" \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai WordProcessor )" != "" ] ; then
					printf '\t''section="Applications/Office"  \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Office )" != "" ] ; then
					printf '\t''section="Applications/Office" \\''\n'       >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai -a Database )" != "" ] ; then
					printf '\t''section="Applications/Data Management" \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai TextEditor )" != "" ] ; then
					printf '\t''section="Applications/Editors" \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Archiving )" != "" ] ; then
					printf '\t''section="Applications/File Management" \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai PackageManager )" != "" ] ; then
					printf '\t''section="Applications/System/Package Management"  \\''\n'   >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Emulator )" != "" ] ; then
					printf '\t''section="Applications/Emulators" \\''\n'    >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai -a Documentation )" != "" ] ; then
					printf '\t''section="Help" \\''\n'      >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Utility )" != "" ] ; then
					printf '\t''section="Applications/System/Administration" \\''\n'		>> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai System )" != "" ] ; then
					printf '\t''section="Applications/System/Administration" \\''\n'		>> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Application )" != "" ] ; then
					printf '\t''section="Applications/System/Administration"  \\''\n'       >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Settings )" != "" ] ; then
					printf '\t''section="Applications/System/Administration"  \\''\n'       >> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai HardwareSettings )" != "" ] ; then
					printf '\t''section="Applications/System/Administration" \\''\n'		>> "$MENU_PATH"/"$i"
			
				elif [ "$(echo $CATEGORIES | grep -ai Filesystem )" != "" ] ; then
					printf '\t''section="Applications/System/Administration" \\''\n'		>> "$MENU_PATH"/"$i"
			
				else
					printf '\t''section="Applications/System/Administration" \\''\n'		>> "$MENU_PATH"/"$i"
			
				fi
			
			printf '\t''title="'"$(cat $DESKTOP_PATH/"$i".desktop | grep -aEi '^Name=' | head -n 1 | sed 's|Name=||g')"'" \\''\n'   >> "$MENU_PATH"/"$i"

			if [ -f "/usr/share/pixmaps/$i.xpm" ] ; then
				printf '\t''icon="/usr/share/pixmaps/'$i'.xpm" \\''\n'   >> "$MENU_PATH"/"$i"
			elif [ -f "/usr/share/pixmaps/$i.png" ] ; then
				convert /usr/share/pixmaps/"$i".png -resize 32x32 /usr/share/pixmaps/"$i".xpm
				if [ "$?" -eq 0 ] ; then
					printf '\t''icon="/usr/share/pixmaps/'$i'.xpm" \\''\n'   >> "$MENU_PATH"/"$i"
					printf "$DESKTOP_PATH"/"$i".desktop'\t'"/usr/share/pixmaps/$i.xpm"'\n' | tee -a /usr/share/menugen/menugen_create
				fi
			else
				ICON_TEST=$(find /usr/share/icons/ | grep -aE "/32x32/|/36x36/|/40x40/|/42x42/|/48x48/|/64x64/" | grep -a "$i" | sort -u | awk '{ print length, $0 }' | sort -n -s | cut -d " " -f 2 | head -n 1 )
				if [ -f "$ICON_TEST" ] ; then
					convert "$ICON_TEST" -resize 32x32 /usr/share/pixmaps/"$i".xpm
					if [ "$?" -eq 0 ] ; then
						printf '\t''icon="/usr/share/pixmaps/'$i'.xpm" \\''\n'   >> "$MENU_PATH"/"$i"
						printf "$DESKTOP_PATH"/"$i".desktop'\t'"/usr/share/pixmaps/$i.xpm"'\n' | tee -a /usr/share/menugen/menugen_create
					fi
				fi
			fi

			#printf "%b\n" '\t''command="'"$(cat $DESKTOP_PATH/"$i".desktop | grep -aEi '^Exec=' | sed "s|Exec=||g" | sed 's|"||g')"'"' >> "$MENU_PATH"/"$i"
			echo -e '\t''command="'$(cat $DESKTOP_PATH/"$i".desktop | grep -aEi '^Exec=' | sed "s|Exec=||g" | sed 's|"||g')'"' >> "$MENU_PATH"/"$i"

		fi
		done
	fi
done


# Clean menugen

if [ -e /usr/share/menugen/menugen_create ] ; then
	cat /usr/share/menugen/menugen_create | sort -u | grep -aEi "[a-zA-Z0-9]" | while read i ; do
		if [ ! -e "$(echo $i | awk '{print $1}')" ] ; then
			rm -f "$(echo $i | awk '{print $2}')"
			sed -i "s|$i||g" /usr/share/menugen/menugen_create
		fi
	done
	sed -i ':a;N;$!ba;s|\n\n|\n|g' /usr/share/menugen/menugen_create
	sed -i ':a;N;$!ba;s|\n\n|\n|g' /usr/share/menugen/menugen_create
fi


update-menus


exit 0
