#!/bin/bash

#############################################
#		ROOT MENU GENERATOR
#--------------------------------------------
#

#	GENERATE EACH ENTRY IF REQUIRED

#Install
if [ -f /usr/bin/ubiquity ]
then
	item=
	icon=
	item=`sed '1!d' $appdir/locale/$1`
	if [ $icons = "true" ]
	then
		icon="/usr/share/pixmaps/ubiquity.png"
	fi
	install="[exec]  ("$item")  {ubiquity}  <"$icon">"
fi

#Home
item=
icon=
item=`sed '2!d' $appdir/locale/$1`
if [ $icons = "true" ]
then
		icon=`find $icontheme/ -wholename *home*.png | grep -m 1 home`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *home*.png | grep -m 1 home`
		fi
fi
home="[exec]  ("$item")  {thunar ~/} <$icon>"

#Web Browser
item=
icon=
item=`sed '3!d' $appdir/locale/$1`
if [ $icons = "true" ] && [ -f $wdir/submenus/Internet ]
then
	icon=`cat $wdir/submenus/Internet | grep -m 1 firefox |cut -d '<' -f2 | cut -d '>' -f1`
fi
browser="[exec]  ("$item")  {firefox} <$icon>"

#Terminal
item=
icon=
item=`sed '4!d' $appdir/locale/$1`
if [ $icons = "true" ]
then
		icon=`find $icontheme/ -wholename *terminal*.png | grep -m 1 terminal`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *terminal*.png | grep -m 1 terminal`
		fi
fi
terminal="[exec]  ("$item")  {lxterminal} <$icon>"

#mintConfig
item=
icon=
item=`sed '5!d' $appdir/locale/$1`
if [ $icons = "true" ] && [ -f $wdir/submenus/Preferences ]
then
	icon=`cat $wdir/submenus/Preferences | grep -m 1 mintConfig |cut -d '<' -f2 | cut -d '>' -f1`
fi
mintconf="[exec]  ("$item")  {mintConfig-fluxbox} <$icon>"

#mintInstall
item=
icon=
item=`sed '6!d' $appdir/locale/$1`
if [ $icons = "true" ] && [ -f $wdir/submenus/System ]
then
	icon=`cat $wdir/submenus/System | grep -m 1 mintInstall |cut -d '<' -f2 | cut -d '>' -f1`
fi
mintinstall="[exec]  ("$item")  {mintInstall} <$icon>"

#Synaptic
item=
icon=
item=`sed '20!d' $appdir/locale/$1`
if [ $icons = "true" ] && [ -f $wdir/submenus/System ]
then
	icon=`cat $wdir/submenus/System | grep -m 1 synaptic |cut -d '<' -f2 | cut -d '>' -f1`
fi
synaptic="[exec]  ("$item")  {gksu synaptic} <$icon>"

#Run
item=
icon=
item=`sed '7!d' $appdir/locale/$1`
if [ $icons = "true" ]
then
	icon=`find $icontheme/ -wholename *run*.png | grep -m 1 run`
	if [ -z $icon ]
	then
		icon=`find /usr/share/icons -wholename *run*.png | grep -m 1 run`
	fi

fi
run="[exec]  ("$item")  {/usr/bin/fbrun} <$icon>"

#Fluxbox
item=
icon=
item=`sed '8!d' $appdir/locale/$1`
if [ $icons = "true" ]
then
	icon=`find $icontheme/ -wholename *preferences-system.png | grep -m 1 preferences`
	if [ -z $icon ]
	then
		icon=`find /usr/share/icons -wholename *preferences*.png | grep -m 1 preferences`
	fi

fi
fluxbox="[submenu] ("$item") {Settings} <$icon>
 		[config] (Fluxbox Menu)
		[reconfig] (Reload Config)
		[restart] (Restart Fluxbox)
		[workspaces] (Workspaces)
		[submenu] (Styles) {}
			[stylesdir] (/usr/share/fluxbox/styles)
			[stylesdir] (~/.fluxbox/styles)
		[end]
[end]"

#Accessories
item=
icon=
if [ -f $wdir/submenus/Accessories ]
then
	item=`sed '9!d' $appdir/locale/$1`
	if [ $icons = "true" ]
	then
		icon=`find $icontheme/ -wholename *applications-accessories*.png | grep -m 1 accessories`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *applications-accessories*.png | grep -m 1 accessories`
		fi
	fi
	accessories="[submenu]  ("$item")  <$icon>
	[include] ($wdir/submenus/Accessories)
[end]"
else
accessories=
fi

#Games
item=
icon=
if [ -f $wdir/submenus/Games ]
	then
	item=`sed '10!d' $appdir/locale/$1`
	if [ $icons = "true" ]
	then
		icon=`find $icontheme/ -wholename *applications-games*.png | grep -m 1 games`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *applications-games*.png | grep -m 1 games`
		fi
	fi
	games="[submenu]  ("$item")  <$icon>
	[include] ($wdir/submenus/Games)
[end]"
else
games=
fi

#Graphics
item=
icon=
if [ -f $wdir/submenus/Graphics ]
	then
	item=`sed '11!d' $appdir/locale/$1`
	if [ $icons = "true" ]
	then
		icon=`find $icontheme/ -wholename *applications-graphics*.png | grep -m 1 graphics`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *applications-graphics*.png | grep -m 1 graphics`
		fi
	fi
	graphics="[submenu]  ("$item")  <$icon>
	[include] ($wdir/submenus/Graphics)
[end]"
else
graphics=
fi

#Internet
item=
icon=
if [ -f $wdir/submenus/Internet ]
	then
	item=`sed '12!d' $appdir/locale/$1`
	if [ $icons = "true" ]
	then
		icon=`find $icontheme/ -wholename *applications-internet*.png | grep -m 1 internet`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *applications-internet*.png | grep -m 1 internet`
		fi
	fi
	internet="[submenu]  ("$item")  <$icon>
	[include] ($wdir/submenus/Internet)
[end]"
else
internet=
fi

#Office
item=
icon=
if [ -f $wdir/submenus/Office ]
	then
	item=`sed '13!d' $appdir/locale/$1`
	if [ $icons = "true" ]
	then
		icon=`find $icontheme/ -wholename *applications-office*.png | grep -m 1 office`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *applications-office*.png | grep -m 1 office`
		fi
	fi
	office="[submenu]  ("$item")  <$icon>
	[include] ($wdir/submenus/Office)
[end]"
else
office=
fi

#Others
item=
icon=
if [ -f $wdir/submenus/Others ]
	then
	item=`sed '14!d' $appdir/locale/$1`
	if [ $icons = "true" ]
	then
		icon=`find $icontheme/ -wholename *applications-other*.png | grep -m 1 other`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *applications-other*.png | grep -m 1 other`
		fi
	fi
	others="[submenu]  ("$item")  <$icon>
	[include] ($wdir/submenus/Others)
[end]"
else
others=
fi

#Programming
item=
icon=
if [ -f $wdir/submenus/Programming ]
	then
	item=`sed '15!d' $appdir/locale/$1`
	if [ $icons = "true" ]
	then
		icon=`find $icontheme/ -wholename *applications-development*.png | grep -m 1 development`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *applications-development*.png | grep -m 1 development`
		fi
	fi
	programming="[submenu]  ("$item")  <$icon>
	[include] ($wdir/submenus/Programming)
[end]"
else
programming=
fi

#Sound & Video
item=
icon=
if [ -f $wdir/submenus/Multimedia ]
	then
	item=`sed '16!d' $appdir/locale/$1`
	if [ $icons = "true" ]
	then
		icon=`find $icontheme/ -wholename *applications-multimedia*.png | grep -m 1 multimedia`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *applications-multimedia*.png | grep -m 1 multimedia`
		fi
	fi
	multimedia="[submenu]  ("$item")  <$icon>
	[include] ($wdir/submenus/Multimedia)
[end]"
else
multimedia=
fi

#System Tools
item=
icon=
if [ -f $wdir/submenus/System ]
	then
	item=`sed '17!d' $appdir/locale/$1`
	if [ $icons = "true" ]
	then
		icon=`find $icontheme/ -wholename *applications-system*.png | grep -m 1 system`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *applications-system*.png | grep -m 1 system`
		fi
	fi
	system="[submenu]  ("$item")  <$icon>
	[include] ($wdir/submenus/System)
[end]"
else
system=
fi

#Preferences
item=
icon=
if [ -f $wdir/submenus/Preferences ]
	then
	item=`sed '21!d' $appdir/locale/$1`
	if [ $icons = "true" ]
	then
		icon=`find $icontheme/ -wholename *preferences-system*.png | grep -m 1 system`
		if [ -z $icon ]
		then
			icon=`find /usr/share/icons -wholename *preferences*.png | grep -m 1 system`
		fi
	fi
	preferences="[submenu]  ("$item")  <$icon>
	[include] ($wdir/submenus/Preferences)
[end]"
else
preferences=
fi

#System
item=
icon=
item=`sed '22!d' $appdir/locale/$1`
if [ $icons = "true" ]
then
	icon=`find $icontheme/ -wholename *preferences-system*.png | grep -m 1 preferences`
	if [ -z $icon ]
	then
		icon=`find /usr/share/icons -wholename *preferences-system*.png | grep -m 1 preferences`
	fi

fi
submenu="[submenu] ("$item") {System} <$icon>
		$system
		$preferences
		$fluxbox
[end]"

#Quit
item=
icon=
item=`sed '18!d' $appdir/locale/$1`
if [ $icons = "true" ]
then
	icon=`find $icontheme/ -wholename *exit*.png | grep -m 1 exit`
	if [ -z $icon ]
	then
		icon=`find /usr/share/icons -wholename *exit*.png | grep -m 1 exit`
	fi

fi
quit="[exec]  ("$item")  {ciao} <$icon>"

#	/END GENERATE EACH MENU IF REQUIRED

#Output the menu file
echo "
[begin] (Linux Mint) </usr/share/icons/MintFB/24x24/places/start-here.png>
	$install
	$home
	$browser
	$terminal
	$mintconf
	$mintinstall
	$synaptic
	$run
	[include] (~/.fluxbox/custom-menu-entries)
    [separator] (--------)
	$accessories
	$games
	$graphics
	$internet
	$office
	$others
	$programming
	$multimedia
	$submenu
    [separator] (--------)
	$quit
[end]

" > $HOME/.fluxbox/menu

#
#		/ROOT MENU GENERATOR
#############################################

