#!/usr/local/bin/bash

if [[ "X$1" = "X" || "X$2" = "X" ]]; then
    echo "Usage: importasmenu <foreignmenudir> <targetasmenudir>";
    exit;
fi

curpwd=$PWD
targetdir=$2

#rm -fr $2

if [ ! -e $2 ]; then
    if ! mkdir $2; then exit; fi
fi

cd $1

for d in `find . -maxdepth 1 -type d` ; do
    if [[ "X$d" != "X.." && "X$d" != "X." ]] ; then 
	importasmenu $d $2/$d
    fi
done;

# processing KDE entries

for f in `find . -maxdepth 1 -type f -name "*.kdelnk"` ; do

    name=`grep -w "Name" < $f| head -n 1 | cut -c '6-'`
#	name="aaaa"
    if [ "X$name" == "X" ]; then name=$f ; fi
    echo "$name"  
    if grep "Exec=" < $f > /dev/null ; then
		cmd=`grep "Exec=" < $f | head -n 1 | grep -v Swallow | cut -c '6-'`
		if echo $cmd | grep "%" > /dev/null ; then
			echo "skipping $cmd"
		else
			echo "Exec \"$name\" exec " $cmd "&" >"$2/$f"
			echo "Exec \"$name\" exec " $cmd "&"
		fi
    fi
done;

#now processing GNOME entries

for f in `find . -maxdepth 1 -type f -name "*.desktop"` ; do

    name=`grep -w "Name" < $f | head -n 1 | cut -c '6-'`
#	name="aaa"
    if [ "X$name" == "X" ]; then name=$f ; fi
    echo "$name"  
    if grep "Exec=" < $f > /dev/null ; then
		cmd=`grep "Exec=" < $f | head -n 1 | grep -v Swallow | grep -v Try |cut -c '6-'`
		if echo $cmd | grep "%" > /dev/null ; then
			echo "skipping $cmd"
		else
			echo "Exec \"$name\" exec " $cmd "&" >"$2/$f"
			echo "Exec \"$name\" exec " $cmd "&"
		fi
    fi
done;


cd $curpwd

