#!/bin/bash

#############################################
#		VARIABLES
#--------------------------------------------
#
#echo "Exporting variables"

#Get current working directory
export wdir="$HOME/.mint-fm2"

#Get application directory
export appdir=/usr/lib/linuxmint/mint-fm2

#Define language
export lang=`echo $LANG | cut  -d '.' -f1`
export langalt=`echo $LANG | cut -c-2`

#Monitored directory
export trigdir=$wdir/triggers

#Working lock marker
export usrlock=$wdir/lock

#Root lock
export rootlock=/tmp/rootlock

#Get user name
export usrname=`whoami`

#Ciao trigger dir
export ciaodir=/etc/ciao-trigger

#Other variables defined in the scripts are $name, $command, $iconname, $iconloc, $category, $submenu
#
#		/VARIABLES
#############################################

#############################################
#		CHECKS
#--------------------------------------------
#
#echo "Starting checks"

#Kill running sessions of the user daemon
killall mfmuserdaemon

ps -eo pid,user,args --sort pid | grep "inotifywait -q -m -r --format %f -e create $trigdir" | sed -e '/grep/d' | while read line
do
	if [ ! -z "$line" ]
	then
#		echo "daemon found $line"
		pid=`echo $line | cut -d ' ' -f1`
#		echo "killing $pid"
		kill $pid
	fi
done

#remove lock
if [ -f $usrlock ]
then
	rm $$usrlock
fi

#check for default folders
if [ -d $wdir ]
then
	sleep 0
else
	mkdir $wdir
fi

if [ -d $wdir/submenus ]
then
	sleep 0
else
	mkdir $wdir/submenus
	#remove Fluxbox menu file to force generation of new menu
	if [ -f $HOME/.fluxbox/menu.backup ]
	then
		rm $HOME/.fluxbox/menu.backup
	fi
	mv $HOME/.fluxbox/menu $HOME/.fluxbox/menu.backup
fi

if [ -d $trigdir ]
then
	sleep 0
else
	mkdir $trigdir
fi

#check for config file
if [ -f $wdir/mint-fm2.conf ]
then
	sleep 0
else
	cp $appdir/mint-fm2.conf $wdir/mint-fm2.conf
fi

#check for $HOME/.fluxbox/custom-menu-entries
if [ -f $HOME/.fluxbox/custom-menu-entries ]
then
	sleep 0
else
	touch $HOME/.fluxbox/custom-menu-entries
fi

#check for Fluxbox menu file is at the end

#	EXPORT LANGUAGE VARIABLES
#With or without icons
export icons=`cat $wdir/mint-fm2.conf | grep Icons | sed '/^#/ d' | cut -d ' ' -f2`
#
#Define icon theme
export icontheme=`cat $wdir/mint-fm2.conf | grep IconTheme | sed '/^#/ d' | cut -d ' ' -f2`
#	/EXPORT LANGUAGE VARIABLES

#
#		/CHECKS
#############################################


#############################################
#	START MINT-FM2
#--------------------------------------------
#

#Check for existing menu
if [ -f $HOME/.fluxbox/menu ]
then

#	CHECK FOR PENDING UPDATES
files=`ls $trigdir`

if [ "$files" ]
then
	ls $trigdir | while read line
	do
#		echo "Updating..."
		cat $trigdir/$line
#Assign change to variable
		list=`cat $trigdir/$line`

#Run updater
#		echo "$appdir/$line $list"	
		$appdir/./update $list

#		echo "Done"
		rm $trigdir/$line		
	done
fi

#	/CHECK FOR PENDING UPDATES

	$appdir/./mfmuserdaemon&
else	
#	GENERATE NEW MENU
generate () {
	echo 0
	if [ -d $wdir/submenus.backup ]
	then
		rm -rf $wdir/submenus.backup
	fi
	echo 10
	if [ -d $wdir/submenus ]
	then
		mv $wdir/submenus $wdir/submenus.backup
		mkdir $wdir/submenus
	fi
	echo 20
	
	$appdir/./main
	echo 70
	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
	echo 100
}

#get text to display in zenity

if [ -f $appdir/locale/$lang ]
then
	genlang=$lang
else
	if [ -f $appdir/locale/$langalt ]
	then
		genlang=$langalt
	else
		genlang=en
	fi
fi

gentext=`sed '19!d' $appdir/locale/$genlang`

#/get text to display in zenity

generate | zenity --progress --title="mint-fm2" --text="$gentext" --percentage=0 --auto-close

#	/GENERATE NEW MENU
	$appdir/./mfmuserdaemon&
fi
#
#	/START MINT-FM2
#############################################

exit
