#!/bin/bash
# Add a program system-wide to the icewm menu
# first passed variable is menu SECTION
# second is PROG NAME
# third is ICON NAME
# fourth is MENU ITEM NAME
#
# example usage:
# addiceitem Graphics xnview pictureview Xnview
# (put "" marks around any multiple-word name)
#
# so for most apps on install we'd add 
# the folowing lines to the "doinst.sh" script:
# update-mime-database /usr/share/mime 2>/dev/null
# update-desktop-database 2>/dev/null
# addiceitem Graphics xnview pictureview 2>/dev/null
#
# c2010-2013 by Paul Sherman  <psherman2001@gmail.com>
# This program is free software; you can redistribute it and/or	
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
#
# if section or program name was ommited,
# just walk away........................
if [ "$1" == "" ] || [ "$2" == "" ]; then
	exit 0
fi

# base menu file locations
rootfile="/root/.icewm/menu"
skelfile="/etc/skel/.icewm/menu"

# if icon name not given, we default to program name for the icon
if [ "$3" == "" ]; then
	# icon name will default to name of program
	$3=$2
fi

# if menu item name not given, we default to program name
if [ "$4" == "" ]; then
	$4=$2
fi

# find out if program already has a menu entry for root
ret=`sed -n "/menu \"$1\"/,/^\}/p" $rootfile | grep $2`
if [[ ! "$ret" =~ "prog" ]]
then
	# put into menu
	section_line=`grep  "menu \"$1\"" $rootfile`
	new_line="$section_line\n\t\tprog \"$4\" \"$3\" $2"
	# do this for the root menu
	sed -i "s/$section_line/$new_line/" $rootfile
else
	new_line="\tprog \"$4\" \"$3\" $2"
	sed -i "s/$ret/$new_line/" $rootfile
fi

# find out if program already has a menu entry in skel directory
ret=`sed -n "/menu \"$1\"/,/^\}/p" $skelfile | grep $2`
if [[ ! "$ret" =~ "prog" ]]
then
	# put into menu
	section_line=`grep  "menu \"$1\"" $rootfile`
	new_line="$section_line\n\t\tprog \"$4\" \"$3\" $2"
	# and for the icewm menu in the SKEL directory
	sed -i "s/$section_line/$new_line/" $skelfile
else
	new_line="\tprog \"$4\" \"$3\" $2"
	sed -i "s/$ret/$new_line/" $skelfile
fi

# Now we do the same for each user directory on the machine
# this means we have to find each user and add menu item
# if it is not already there
cd /home
# any user folders?
num=`find . -type d | wc -l`
if [ "$num" -gt 1 ]; then
	# if so, then start adding app to their menus
	for f in `ls -d */`
	do 
		userfile="/home/$f.icewm/menu"
		ret=`sed -n "/menu \"$1\"/,/^\}/p" $userfile | grep $2`
		if [[ ! "$ret" =~ "prog" ]]
		then
			# put into menu
			section_line=`grep  "menu \"$1\"" $rootfile`
			new_line="$section_line\n\t\tprog \"$4\" \"$3\" $2"
			# do this for the user menu
			sed -i "s/$section_line/$new_line/" $userfile
		else
			new_line="\tprog \"$4\" \"$3\" $2"
			sed -i "s/$ret/$new_line/" $userfile
		fi
	done
fi
# restart icewm for new menu item to show up
if [ "$DISPLAY" ]; then
	killall -HUP icewm& > /dev/null 2>&1
fi


