#!/usr/bin/perl

#Stand: 10.0.2000
#(c) by T.Mueller
#http://www.sirtobi.com/stud/toice/

# This program can convert Gnome-menues to icewm-menues.
# Just use it like ./gno2ice.pl >programs
#
# 2012/02/19 Update: Primary Goal: Put all possible items on the menu with a
# good enough folder structure so that you can see all of them on the screen.
# Read the Categories field in the shortcut files, make a hash of
# (category, shortcut) so we can decide which categories to show. Also sort
# shortcuts by name.
#    Note: It is recommended to use the following line in .icewm/preferences:
#    IconPath="/usr/share/pixmaps:/usr/share/icons/hicolor/16x16/apps:/usr/share/icons/gnome/16x16/apps:/usr/share/pixmaps/comps"
#########################################

# This is the Path to your Gnome-Menu
#$PFAD="$ENV{GNOMEDIR}/share/gnome/apps";
# Fedora 16
$PFAD="/usr/share/applications";
if($PFAD eq "/share/gnome/apps")
{
        $PFAD="/usr/share/gnome/apps";
}
$MYPFAD="$ENV{HOME}/.gnome/apps";
# This is your country code, use "" for don`t know ! 
@LAENDER=split("_",$ENV{LANG});
$LAND=@LAENDER[0];
# This is the maximal length of menue-entrys. If it is to long, icewm will fail to show it.
$MAXCOMMENT=30;



################################## Nothing to change here ###########################################
my %CATS;
if( -e $PFAD)
{
	print "menu GNOME gnome-about-logo {\n\n";
	ReadApps($PFAD,\%CATS);
	PrintGNOMEApps(\%CATS);
	print "\n}\n\n";
}
if( -e $MYPFAD)
{
	print "menu \"My Apps\" folder {\n\n";
	ReadApps($MYPFAD,\%CATS);
	PrintMyApps(\%CATS);
	print "\n}\n\n";
}
# Read shortcut files (*.desktop) into hash.
sub ReadApps
{
	my $PFAD=@_[0];		# Uerbegabevariablen abfangen
	my $ENTRY;
	my $DirPos;

	if ( -r join("",$PFAD,"/.directory"))
	{
		open(DIRPREF,join("","<",$PFAD,"/.directory")) || die "Can`t open $PFAD/.directory";

		do
		{
			@PREFS=readline <DIRPREF> ;
		}
		until(eof);
		close(DIRPREF);
		$COMMENT="";
		$ICON="";
		foreach $ENTRY(@PREFS)
		{
			@ENTRYS=split("=",$ENTRY);
	                if(lc(@ENTRYS[0]) eq "name")
                	{
        	                if($COMMENT eq "")
	                        {
					$COMMENT=@ENTRYS[1];

                	        }
        	        }
	                if(lc(@ENTRYS[0]) eq "name[$LAND]")
                	{
        	        	$COMMENT=@ENTRYS[1];
	                }

                	if(lc(@ENTRYS[0]) eq "icon")
        	        {
				$ICON=@ENTRYS[1];
				@ICONS=split('\.',$ICON);
				$ICON=@ICONS[0];
        	        }
		}
		if($COMMENT eq "")
		{
			$COMMENT="No Name";
		}
		else
		{
			@COMMENTS=split(chr(10),$COMMENT);
			$COMMENT=@COMMENTS[0];
		}

        	if($ICON eq "")
	        {
        	        $ICON="NoIcon";
	        }
		$COMMENT=substr $COMMENT,0,$MAXCOMMENT;
	}
	else
	{
		$COMMENT="$PFAD";
		$ICON="$PFAD";
	}
	opendir(DIR, $PFAD) || die "Error opening $PFAD: $!";	# Verzeichnis einlesen
	do
	{
		$ENTRY=readdir DIR;
		if($ENTRY ne '.' & $ENTRY ne '..' & $ENTRY ne '')	# Systemverzeichnisse rausschmeissen
		{
			if(-d "$PFAD/$ENTRY")	# Verzeichnisse rekursiv beackern
			{
				$DirPos=telldir DIR;
				closedir DIR;
				ReadApps("$PFAD/$ENTRY",\%{ $_[1] });
				opendir(DIR, $PFAD) || die "Error opening $PFAD: $!";
				seekdir DIR,$DirPos;
			}
			else
			{
				@ENTRYS=split('\.',$ENTRY);
				if(@ENTRYS[1] eq "desktop")
				{
				        open(PREF,join("","<",$PFAD,"/$ENTRY")) || die "Can`t open $PFAD/$ENTRY";
       					do
       					{
               					@PREFS=readline <PREF> ;
       					}
       					until(eof);
       					close(PREF);
       					$EXEC="";
       					$COMMENT="";
       					$ICON="";
					$CAT="";
    				    	foreach $ENTRY(@PREFS)
       					{
				                @ENTRYS=split("=",$ENTRY);
				                if(lc(@ENTRYS[0]) eq "exec")
				                {
				                        $EXEC=@ENTRYS[1];
							chomp($EXEC);

				                }
				                if(lc(@ENTRYS[0]) eq "name")
				                {
				                        if($COMMENT eq "")
				                        {
				                                $COMMENT=@ENTRYS[1];

				                        }
				                }
				                if(lc(@ENTRYS[0]) eq "name[$LAND]")
				                {
				                        $COMMENT=@ENTRYS[1];
				                }

				                if(lc(@ENTRYS[0]) eq "icon")
				                {
				                        $ICON=@ENTRYS[1];
				                        @ICONS=split('\.',$ICON);
				                        $ICON=@ICONS[0];
							chomp($ICON);
				                }
				                if(lc(@ENTRYS[0]) eq "categories")
				                {
				                        $CAT=@ENTRYS[1];
				                }
				        }
				
				        if($COMMENT eq "")
				        {
				                $COMMENT="No Name";
				        }
				        else
				        {
				                @COMMENTS=split(chr(10),$COMMENT);
				                $COMMENT=@COMMENTS[0];
				        }

				        if($ICON eq "")
				        {
				                $ICON="NoIcon";
				        }
					if($ICON eq chr(10))
					{
						$ICON="NoIcon";

					}
					$COMMENT=substr $COMMENT,0,$MAXCOMMENT;

					# Build a hash of all categories in all shortcuts,
					# so shortcuts will be in multiple categories,
					# so we can decide later which categories to use.
					@CATS=split(';',$CAT);
					foreach $ICAT(@CATS)
					{
						if ($ICAT ne "\n")
						{
							$_[1]{$ICAT}{$COMMENT}={
								icon=>$ICON,
								exec=>$EXEC  
								};
						}
					}
				}
			}
			
		}
	}
	until( $ENTRY eq  "");
	closedir DIR;
}
sub PrintGNOMEApps
{
	# Start with some well-used categories
	ChooseCategory(\%{ $_[0] },"DesktopSettings","gnome-desktop");
	ChooseCategory(\%{ $_[0] },"Game","games");
	#ChooseCategory(%_[0], "Game");
	ChooseCategory(\%{ $_[0] },"Graphics","graphics");
	ChooseCategory(\%{ $_[0] },"HardwareSettings","hardware-support");

	# Combine Audio, AudioVideo, and Video into Multimedia
	$_[0]{Multimedia}=$_[0]{Audio};
	for $ITEM (keys %{ $_[0]{AudioVideo} })
	{
		$_[0]{Multimedia}{$ITEM}=$_[0]{AudioVideo}{$ITEM};
	}
	for $ITEM (keys %{ $ALLCATS2{Video} })
	{
		$_[0]{Multimedia}{$ITEM}=$_[0]{Video}{$ITEM};
	}
	ChooseCategory(\%{ $_[0] },"Multimedia","sound-and-video");
	ChooseCategory(\%{ $_[0] },"Network","graphical-internet");
	ChooseCategory(\%{ $_[0] },"Settings","base-system");
	ChooseCategory(\%{ $_[0] },"System","server-cfg");
	ChooseCategory(\%{ $_[0] },"Utility","system-tools");

	# Print remaining categories alphabetically.
	for $CAT (sort(keys %{ $_[0] }))
	{
		if ($_[0]{$CAT} ne "")
		{
			ChooseCategory(\%{ $_[0] },$CAT,"folder");
		}
	}
}
sub PrintMyApps
{
	# Print remaining categories alphabetically.
	for $CAT (sort(keys %{ $_[0] }))
	{
		if ($_[0]{$CAT} ne "")
		{
			ChooseCategory(\%{ $_[0] },$CAT,"folder");
		}
	}
}
sub ChooseCategory
{
	my $CHOICE=$_[1];
	my $ICON=$_[2];

	print "  menu \"$CHOICE\" $ICON {\n";

	# Iterate over the items in the chosen category.
	for $ITEM (sort(keys %{ $_[0]{$CHOICE} }))
	{
		print "    prog \"$ITEM\" \"$_[0]{$CHOICE}{$ITEM}{icon}\" $_[0]{$CHOICE}{$ITEM}{exec}\n";
		
		# Delete references to this item in other categories.
		for $CAT (keys %{ $_[0] })
		{
			for $ITEM2 (keys %{ $_[0]{$CAT} })
			{
				if ($ITEM2 eq $ITEM)
				{
					delete($_[0]{$CAT}{$ITEM});
				}
			}
			# Delete category if it's empty now.
			if (scalar(keys %{ $_[0]{$CAT} }) eq 0)
			{
				delete($_[0]{$CAT});
			}
		}
	}

	print "  }\n";
}
