#!/usr/bin/perl
########################################################################
# WMAmpMenu - A script to build a Window Maker menu from a 
# hierarchy of XMMS playlists
#
# Copyright (C) 1999 Sami Lempinen
# See the file COPYING for copyright and warranty information.
#######################################################################
$VERSION = substr(q$Revision: 1.5 $, 10);

use Getopt::Std; # For command line parsing

getopts ('m:vh');
$VERBOSE = $opt_v;

# Check if we received any arguments
if ($#ARGV < 0 || $opt_h) 
{
    Usage ();
    exit -1;
}

# Check if the user specified a menu file
$menufile = $opt_m;
if (! $menufile) 
{
    $menufile = "$ENV{HOME}/GNUstep/Library/WindowMaker/wmampmenu.menu";
}
print "Outputting to $menufile.\n" if ($VERBOSE);

# Check if the user's directory argument makes sense
$dir = shift;
if (! -d $dir) 
{
    print STDERR "$dir is not a directory. Bailing out.\n";
}

# Write the actual menu file
$timestamp = localtime;
open (MENUFILE, ">$menufile") 
    || die "Could not open $menufile for writing. Bailing out";

print MENUFILE <<EOT;
/* Menu autogenerated by WMAmpMenu on $timestamp */ 
"MP3 Options" MENU
        ">" EXEC xmms -p
        "||" EXEC xmms -u
        "<<" EXEC xmms -r
        ">>" EXEC xmms -f
        "*" EXEC xmms -s
        "Playlists" OPEN_MENU $dir WITH mp3launch
        "WMAmpMenu Home" EXEC xterm -e lynx "http://www.iki.fi/lempinen/wmampmenu"
        "XMMS Home" EXEC xterm -e lynx "http://www.xmms.org/"
"MP3 Options" END
EOT

close MENUFILE;

print "\nDone. Now edit your Window Maker root menu definition\n",
    "(typically ~/GNUstep/Library/WindowMaker/menu) and add the line\n\n",
    "\t\"MP3\" OPEN_MENU $menufile\n\n",
    "in the location of your choice and you should be all set.\n";

exit 0;

# Help and copyright information
sub Usage {
    print <<EOT;

* WMAmpMenu $VERSION Copyright (C) 1999 Sami Lempinen
* A script to build a Window Maker menu from a 
* hierarchy of XMMS playlists

WMAmpMenu comes with ABSOLUTELY NO WARRANTY; for details see the file COPYING.
This is free software, and you are welcome to redistribute it
under certain conditions; for details see the file COPYING.

  Usage: mp3launch [-v] [-h] [-m menufile] directory

      -v         Verbose mode
      -h         This help screen
      menufile   The file where you want to output the WM menu
	         definition (optional, default is
		 ~/GNUstep/Library/WindowMaker/wmampmenu.menu)
      directory  The directory where you keep your playlists
	         or playlist subdirectories
EOT
}
