#!/usr/bin/perl

use strict;
use warnings;

my $file = $ARGV[0];
my $fh;

my %categories;
my %descriptions;
my %filenames;

# Define the special packages
my @fixed = ( "efs", "xemacs-base" );
my @minimal = ( "edit-utils", "efs", "texinfo", "xemacs-base" );
my @recommended = ( "c-support", "cc-mode", "debug", "dired", "ecb",
		    "edebug", "ediff", "edit-utils", "efs", "eieio",
		    "fsf-compat", "latin-euro-standards",
		    "latin-unity", "locale", "lookup", "mail-lib",
		    "mule-base", "net-utils", "os-utils", "pc",
		    "perl-modes", "prog-modes", "semantic",
		    "sh-script", "sounds-wav", "speedbar", "texinfo",
		    "text-modes", "time", "xemacs-base",
		    "xemacs-devel" );

open $fh, '<', $file or die "unable to open file '$file' for reading : $!";
while (<$fh>) {
    # New package description
    if ( /^\(package-get-update-base-entry/ ) {
	$_ = <$fh>;
	die "unexpected EOF on $file" if !defined $_;
	chop;
	s@^\(@@;
	my $pkg = $_;
	my $pkg_category = "";
	my $pkg_description = "";
	my $pkg_filename = "";
	while ( $pkg_category eq "" || $pkg_description eq "" || $pkg_filename eq "" ) {
	    $_ = <$fh>;
	    die "unexpected EOF on $file" if !defined $_;
	    if ( /^\s*category\s+"(.*)"/ ) {
		$pkg_category = $1;
	    }
	    elsif ( /^\s*description\s+"(.*)"/ ) {
		$pkg_description = $1;
	    }
	    elsif ( /^\s*filename\s+"(.*)"/ ) {
		$pkg_filename = $1;
	    }
	}
	#print "pkg = ", $pkg, "\n";
	#print "    category = ", $pkg_category, "\n";
	#print "    description = ", $pkg_description, "\n";
	#print "    filename = ", $pkg_filename, "\n";

	# Work around an oddity in InnoSetup
	$pkg_description =~ s/{/{{/g;

	$categories{$pkg} = $pkg_category;
	$descriptions{$pkg} = $pkg_description;
	$filenames{$pkg} = $pkg_filename;
    }
}
close $fh;

#foreach my $key (sort (keys %categories)) {
#    print "package = ", $key, "\n";
#    print "    category = ", $categories{$key}, "\n";
#    print "    description = ", $descriptions{$key}, "\n";
#    print "    filename = ", $filenames{$key}, "\n";
#}

# Print out the source section
foreach my $key (sort (keys %categories)) {
    my $component = $key;
    $component =~ s/-/_/g;
    if ( $categories{$key} eq "mule" ) {
	print "#ifdef MULE\n";
	print "Source: \"{#PkgSrc}\\", $key, "-*-pkg.tar\"; DestDir: \"{app}\\mule-packages\"; Components: ", $component, "; Flags: ignoreversion\n";
	print "#endif\n";
    }
    else {
	print "Source: \"{#PkgSrc}\\", $key, "-*-pkg.tar\"; DestDir: \"{app}\\xemacs-packages\"; Components: ", $component, "; Flags: ignoreversion\n";
    }
}

# Print out the component section
print "

[Types]
Name: \"recommended\"; Description: \"Recommended installation\"
Name: \"complete\"; Description: \"Install XEmacs {#XEmacsVersion} and all packages\"
Name: \"minimal\"; Description: \"Minimalist installation\"
Name: \"custom\"; Description: \"Custom installation\"; Flags: iscustom

[Components]
Name: \"base\"; Description: \"XEmacs {#XEmacsVersion} executable and essential support files\"; Types: complete minimal recommended custom; Flags: fixed\n";
foreach my $key (sort (keys %categories)) {
    my $component = $key;
    $component =~ s/-/_/g;

    my $typ = "Types: complete custom";
    $typ = $typ . " recommended" if grep(/^$key$/, @recommended);
    $typ = $typ . " minimal" if grep(/^$key$/, @minimal);
    $typ = $typ . "; Flags: fixed" if grep(/^$key$/, @fixed);

    print "#ifdef MULE\n" if $categories{$key} eq "mule";
    print "Name: \"", $component, "\"; Description: \"", $key, ": ", $descriptions{$key}, "\"; ", $typ, "\n";
    print "#endif\n" if $categories{$key} eq "mule";
}
