#!/bin/bash

#Create submenu directory
	if [ ! -d $wdir/submenus ]
	then
		mkdir $wdir/submenus
	fi

#############################################
#		PARSING LOOP
#--------------------------------------------

#Starting parsing loop
while [ ! -z "$1" ]
do
#	echo $1

#load file to be parsed into variable
parsee=`cat $1`

#Check if NoDisplay is true and skip if true
	if [ `echo "$parsee" | grep NoDisplay | cut -d '=' -f2` ]
	then
		shift
		continue
	fi

#NAME----------------------------------------

#Search for Name in correct language according to $LANG
	name=`echo "$parsee" | grep -m 1 "^Name\[$lang\]" | cut -d '=' -f2`

#Search for Name in correct language but for different country
	if [ -z "$name" ]
	then
		name=`echo "$parsee" | grep -m 1 "^Name\[$langalt" | cut -d '=' -f2`
	fi

#Set Name to Default
	if [ -z "$name" ]
	then
		name=`echo "$parsee" | grep -m 1 "^Name=" | cut -d '=' -f2`
	fi

#/NAME---------------------------------------

#COMMAND-------------------------------------

#Retrieve the required command to run
	command=`echo "$parsee" | grep -m 1 "Exec=" | cut -d '=' -f2 | sed -e 's/%f//; s/%F//; s/%u//; s/%U//'`

#/COMMAND------------------------------------

#ICONS---------------------------------------

#Look if icons are enabled
	if [ $icons = "true" ]
	then

#Retrieve the name of the required icon
		iconname=`echo "$parsee" | grep -m 1 "Icon=" | cut -d '=' -f2 | sed -e 's/.png//; s/.svg//; s/.xpm//'`

#If no icon is defined, set stock icon
		if [ -z $iconname ]
		then
			if [ -f $icontheme/16x16/mimetypes/stock_script.png ]
			then
				iconname="$icontheme/16x16/mimetypes/stock_script"
			else
				iconname="stock_script"
			fi
		fi

#Attempt to find the proper match (theme and file type)
		iconloc=`find $icontheme -wholename *$iconname*.png | grep -v mimetypes | grep -m 1 $iconname`

#Attempt to find a close match (by icon name)
		if [ -z $iconloc ]
		then
			iconloc=`find /usr/share/icons /usr/share/pixmaps -wholename *$iconname* | grep -m 1 "png\|xpm"`
		fi

#Try searching in location given by iconname
		if [ -z $iconloc ]
		then
			if [ -f `echo "$parsee" | grep -m 1 "Icon=" | cut -d '=' -f2 | grep "png\|xpm\|jpg"` ]
			then
				iconloc=`echo "$parsee" | grep -m 1 "Icon=" | cut -d '=' -f2 | sed '/.svg/ d'`
			fi
		fi

#Set icon to stock_script
		if [ -z $iconloc ]
		then
			iconloc=`find /usr/share/icons -name *stock_script*.png | grep -m 1 "png\|xpm"`
		fi

	fi
#/ICONS--------------------------------------

#CATEGORY------------------------------------

#Retrieve category list from .desktop
	category=`echo "$parsee" | grep -m 1 "Categories=" | cut -d '=' -f2`

#Assign categories to submenus

#System
	if [ -z $submenu ]
	then
		if [ `echo $category | grep "System\|AdvancedSettings\|PackageManager\|hardware"` ]
		then
			submenu="System"
		fi
	fi

#Preferences
	if [ -z $submenu ]
	then
		if [ `echo $category | grep "Settings\|HardwareSettings\|Appearance\|settings"` ]
		then
			submenu="Preferences"
		fi
	fi

#Internet
	if [ -z $submenu ]
	then
		if [ `echo $category | grep "Network\|Email\|News\|FileTransfer\|P2P"` ]
		then 
			submenu="Internet"
		fi
	fi

#Multimedia
	if [ -z $submenu ]
	then
		if [ `echo $category | grep "AudioVideo\|Video\|Player\|Audio\|Music"` ]
		then 
			submenu="Multimedia"
		fi
	fi

#Office
	if [ -z $submenu ]
	then
		if [ `echo $category | grep "WordProcessor\|Office\|Spreadsheet"` ]
		then 
			submenu="Office"
		fi
	fi

#Games
	if [ -z $submenu ]
	then
		if [ `echo $category | grep "Game"` ]
		then 
			submenu="Games"
		fi
	fi

#Graphics
	if [ -z $submenu ]
	then
		if [ `echo $category | grep "Graphics\|Photography\|Viewer\|VectorGraphics"` ]
		then 
			submenu="Graphics"
		fi
	fi

#Accessories
	if [ -z $submenu ]
	then
		if [ `echo $category | grep "Utility\|FileManager\|TerminalEmulator\|DesktopSettings\|Core\|TextEditor"` ]
		then 
			submenu="Accessories"
		fi
	fi

#Programming
	if [ -z $submenu ]
	then
		if [ `echo $category | grep "Development\|GUIDesigner"` ]
		then 
			submenu="Programming"
		fi
	fi

#Others
	if [ -z $submenu ]
	then
		submenu="Others"
	fi
	
#/CATEGORY-----------------------------------

#OUTPUT--------------------------------------
#Ouput menu entry to submenu file
	if [ -f $wdir/submenus/$submenu ]
	then
		echo "[exec] ($name) {$command} <$iconloc> #$1"  >> $wdir/submenus/$submenu
	else
		echo "[exec] ($name) {$command} <$iconloc> #$1"  >> $wdir/submenus/$submenu
		if [ -f $appdir/locale/$lang ]
		then
			$appdir/./rootgen $lang
		else
			if [ -f $appdir/locale/$langalt ]
			then
				$appdir/./rootgen $langalt
			else
				$appdir/./rootgen en
			fi
		fi
	fi
#Added submenu file to sortqueue.new
	echo $submenu >> $wdir/sortqueue.new

#/OUTPUT-------------------------------------	

#Reset value of submenu
	submenu=
	
	shift
done
#		/PARSING LOOP
#############################################

#Fix known broken entries
echo"now starting $appdir/./fix"
$appdir/./fix

#Remove duplicates in sortqueue
sort $wdir/sortqueue.new -u > $wdir/sortqueue
rm $wdir/sortqueue.new
exit
