#!/usr/bin/perl -w

# (C) 1999 Marcelo E. Magallon <mmagallo@debian.org>

# This script will only be useful if you are upgrading from the now-ancient
# 0.62 version of wmmail or earlier.

# This script is a HUGE and UGLY hack.  I'm learning Perl, and I
# though this would be a good excersice.  It was.  I learned a few
# things I should avoid doing, I was left with several questions (why
# I can't do that in this way?), and it was amusing.  I held the
# release of a debianized wmmail 0.62 just because this script wasn't
# ready.  I guess the previous version worked ok...

# This script is distributed under the terms and conditions of the GNU
# GPL v2, or at your option, any later version.  On Debian systems,
# you can find a copy of this license in /usr/doc/copyright/GPL.  You
# can also find a copy in the GNU website, http://www.gnu.org/

use strict;

my $i;

my @mailboxes;
my %animations;
my %animspeed;

my %keys = ( Update => "15" );

my %equiv = (
             DisableBeep => "NoBeep",
             DisplayMessageCount => "NumOfMsgMode",
             DisplayColor => "NumOfMsgColor",
             DisplayFont => "NumOfMsgFont",
             ExecuteOnClick => "Execute",
             ExecuteOnNew => "NewMailExecute",
             ExecuteOnNewOnce => "AlwaysNewMailExecute",
);

my $old_pixmap_path = "/usr/X11R6/include/X11/pixmaps/wmmail-";
my $new_pixmap_path = "/usr/lib/GNUstep/Apps/WMMail.app/Anims/";

block: while (<>) {
    next if m/^\s*(:?\#|$)/o;
    chomp;
    if (m/^MailFiles$/) {
        while (<>) {
            next block if m/^End$/o;
            s/^\s*(.*)\s*$/$1/o;
            #s/^(.*)$/"$1"/o if (/\W/o);
            push @mailboxes, $_;
        }
    } elsif (m/^(NoMail|OldMail|NewMail)$/o) {
        s/Mail//o;
        s/No/Empty/o;
        my $key = $_;
        my @frames;
        frame: while (<>) {
            last frame if m/^End$/o;
            s/^\s*(.*)\s*$/$1/o;
            s/^(.*)$/"$1"/o if (/\W/o);
            if (m|$old_pixmap_path(.*)\.xpm|) {
                my $pixmap_class;
                if ($1 =~ m/^e/o) {
                    $pixmap_class = "e/";
                } elsif ($1 =~ m/^Mail/o) {
                    $pixmap_class = "NeXT/";
                } elsif ($1 =~ m/^asmail/o) {
                    $pixmap_class = "asmail/";
                } elsif ($1 =~ m/^monitor-e/o) {
                    $pixmap_class = "monitor-e/";
                };
                s|$old_pixmap_path|$new_pixmap_path$pixmap_class|;
                
            }
            push @frames, $_;
        }
        $animations{$key} = [ @frames ];
        next block;
    } elsif (m/^(TimeStampMode|NoBeep|AlwaysNewMailExcecute)$/o) {
        $keys{$1} = "Yes";
        next block;
    } elsif (m/^((?:(?:Always)?NewMail)?Execute|(?:ExecuteOn)?Update|NumOfMsg(?:Mode|Font|Color))\s+(\"?)(.*)\2\s*$/o) {
        $keys{$1} = $3;
        next block;
    } elsif (m/^(NumOfMsgPosition)\s*(.*)\s+(.*)\s*$/) {
        $keys{$1} = [ ( $2, $3 ) ];
        next block;
    } elsif (m/^(AnimationSpeed)\s*(.*)\s+(.*)\s+(.*)\s*$/) {
        %animspeed = ( Empty => $2, Old => $3, New => $4 );
        next block;
    }
}

print "{\n";

foreach my $key (keys %equiv) {
    my $value;
    if (defined $keys{$equiv{$key}}) {
	$value = $keys{$equiv{$key}};
	$value =~ s/^(.*)$/"$1"/o if ($value =~ /\W/o);
	print "  $key = $value;\n";
    }
}

if (defined $keys{NumOfMsgPosition}) {
    print "  DisplayLocation = ($keys{NumOfMsgPosition}[0], $keys{NumOfMsgPosition}[1]);\n";
}

print "  Animations = {\n";
foreach my $animtype (keys %animations) {
    print "    $animtype = {\n";
    print "      Delay = ", $animspeed{$animtype}, ";\n"
	if defined $animspeed{$animtype};
    print "      Frames = (\n";
    $i = $#{$animations{$animtype}};
    foreach (@{$animations{$animtype}}) {
	print "        $_", ($i--)?",":"", "\n";
    }
    print "      );\n",
          "    };\n";
}
print "  };\n";
    
print "  Mailboxes = (\n";
$i = $#mailboxes;
foreach (@mailboxes) {
    my $name = (m|([^/]*)$|o)?$1:$_;
    print "    {\n",
          "      Name = \"$name\";\n",
          "      Type = ",
          (glob "$_/new")?"maildir":"mbox",
          ";\n";
    print "      UpdateInterval = $keys{Update};\n";
    print "      ExecuteOnUpdate = \"$keys{ExecuteOnUpdate}\";\n"
	if defined $keys{ExecuteOnUpdate};
    print "      Options = {\n",
          "        Path = \"$_\";\n",
          "      };\n",
          "    }", ($i--)?",":"", "\n";
}
print "  );\n";
    
print "};\n";

