#!/usr/bin/perl

#Stand: 10.3.2000
#(c) by T.Mueller
#sirtobi( A T )stud( D O T )uni-hannover( D O T )de
#http://www.sirtobi.com/stud/toice/

# This program can convert KDE-menues to icewm-menues.
# Just use it like ./kde2ice.pl >programs
#########################################

# This is the Path to your KDE-Menu
$PFAD="$ENV{KDEDIR}/share/applnk";      
if($PFAD eq "/share/applnk")
{
	$PFAD="/usr/share/applnk";
}	
$MYPFAD="$ENV{HOME}/.kde/share/applnk";

if ("$ENV{LANG}" eq "german") #Bugfix for german SuSE-Users
{
        $LANG="de";
}
else
{
	@LAENDER=split("_",$ENV{LANG});
	$LAND=@LAENDER[0];
}
# This is your country code, use "" for don`t know, "de" for german, etc. ! 
# If auto-language-detection doesn`t work, uncomment and chang the
# following line !
#$LAND="pl";

# 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 ###########################################

if (! defined $LAND)
{
	if ("$ENV{LANG}" eq "german") #Bugfix for german SuSE-Users
	{
        	$LAND="de";
	}
	else
	{
        	@LAENDER=split("_",$ENV{LANG});
        	$LAND=@LAENDER[0];
	}
}

if( -e $MYPFAD)
{
	GetFiles($MYPFAD,"");
}
if( -e $PFAD)
{
	GetFiles($PFAD,"");
}
sub GetFiles
{
	my $PFAD=@_[0];		# Uerbegabevariablen abfangen
	my $TABU=@_[1];
	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;
	}
	if($TABU eq "")
	{
		$ICON="KDE";
	}
	printf "$TABU";
	printf "menu \"$COMMENT\" \"$ICON\" {\n";	
	$TABU=join("",$TABU,"    ");
	if(-r join("",$PFAD,"/.directory"))
	{
	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;
				GetFiles("$PFAD/$ENTRY",join("",$TABU,"    "));
				opendir(DIR, $PFAD) || die "Error opening $PFAD: $!";
				seekdir DIR,$DirPos;
			}
			else
			{
					@ENTRYS=split('\.',$ENTRY);
					if((@ENTRYS[1] eq "desktop") | (@ENTRYS[1] eq "kdelnk"))
					{
					        open(PREF,join("","<",$PFAD,"/$ENTRY")) || die "Can`t open $PFAD/$ENTRY";
        					do
        					{
                					@PREFS=readline <PREF> ;
        					}
        					until(eof);
        					close(PREF);
        					$EXEC="";
        					$COMMENT="";
        					$ICON="";
    					    	foreach $ENTRY(@PREFS)
        					{
					                @ENTRYS=split("=",$ENTRY);
					                if(lc(@ENTRYS[0]) eq "exec")
					                {
								@EXECS=split("-caption",$ENTRYS[1]);
								
					                        $EXEC=@EXECS[0];

					                }
					                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";
					        }
						if($ICON eq chr(10))
						{
							$ICON="NoIcon";

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

					        if($EXEC ne "")
					        {
							printf "$TABU";
					                printf "prog \"$COMMENT\" \"$ICON\" $EXEC \n";
					        }

					}
			}
			
		}
	}
	until( $ENTRY eq  "");
	closedir DIR;
	}
	printf "$TABU";
	printf "}\n";

}
