#!/usr/bin/perl

#(c) Copyright 2005 Daniel Milstein
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

$stdout = 0;
$mandriva = 0;
if (@ARGV > 0) { #very crude option parsing
   for (split(//, shift)) {
      if ($_ eq "s") {
	 $stdout = 1;
      }
      elsif ($_ eq "m") {
	 $mandriva = 1;
      }
   }
}

if (@ARGV < 2) {
   print "Syntax: desktop2menu -[options] package category [files]\n";
   print "At least one option must be used\n";
   print "Options are:\nd: generate a debian menu file\nm: generate a mandriva menu file\ns: print to stdout instead of writing to a file\n";
   exit 1;
}

$package = shift;
$s = shift;

for (@ARGV) {
   $desktop = $_;
   if (!s#\.desktop$#.menu#) {
      s#$#.menu#;
   }
   $menuFile = $_;
   open DESKTOP, "< $desktop";
   $needs = "X11";
   while (<DESKTOP>) {
      if (/^Name=(.*)$/) {
	 $name = $1;
      }
      elsif (/^NoDisplay=true$/) {
	 die "No need for a menu file.\n";
      }
      elsif (/^Icon=(.*)$/) {
	 $icon = $1;
	 if (!$mandriva) {
	    $icon =~ s#.png$#.xpm#;
	 }
      }
      elsif (/^Exec=(.*)$/) {
	 $exec = $1;
	 while($exec =~ /\%([fFuUdDnNickv])/) {
	    $param = $1;
	    if ($param eq "F" || $param eq "D") {
	       $multiple_files = 1;
	    }
	    elsif ($param eq "u") {
	       $accept_url = 1;
	    }
	    elsif ($param eq "U") {
	       $accept_url = 1;
	       $multiple_files = 1;
	    }
	    $exec =~ s#\s*\%$param##;
	 }
      }
      elsif (/^StartupNotify=(.*)/) {
	 if ($1 eq "true" || $1 eq "1") {
	    $startupnotify = 1;
	 }
      }
      elsif (/^Terminal=true$/) {
	 $needs = "text";
      }
      elsif (/^Categories=(.*)/) {
	 $categories = $1;
      }
      elsif (/^MimeType=(.*)/) {
	 $mimetype = $1;
	 $mimetype =~ s#;#,#g; #desktop uses ; as a seperator; mandriva menu uses , as a seperator
      }
      elsif (/^Comment=(.*)/) {
	 $longtitle = $1;
      }
   }
   close DESKTOP;
   if (!$name || !$exec) {
      die "Need Name and Exec for $desktop\n";
   }
   #TODO: hints
   if (!$s) {
      die "Cannot find menu category for $desktop; did you enter it?\n";
   }

   if (!$stdout) {
      open MENU, "> $menuFile";
      select MENU;
   }
   print "?package($package): needs=\"$needs\" section=\"$s\" title=\"$name\" command=\"$exec\"";
   if ($icon) {
      print " icon=\"$icon\"";
   }
   if ($longtitle) {
      print " longtitle=\"$longtitle\"";
   }
   if ($mandriva) {
      if ($mimetype) {
	 print " mimetypes=\"$mimetype\"";
      }
      if ($startupnotify) {
	 print " startup_notify=\"true\"";
      }
      if ($accept_url) {
	 print " accept_url=\"true\"";
      }
      if ($multiple_files) {
	 print " multiple_files=\"true\"";
      }
   }
   print "\n";
   if (!$stdout) {
      close MENU;
      select STDOUT;
   }
}

#TODO: add exec params to mandriva menu
