#!/usr/bin/perl -i.orig
'di';
'ig00';
#
#       Fixmail - convert VMS mail files to Unix form.
#               To save a VMS e-mail folder in a usable text file...
#               inside of VMS MAIL
#               EXTRACT/ALL/MAIL  filename
#
#       N.B.: does in-place editing.
#
$/ = "\f\nFrom" ;                               # Break at messages, not lines

# 27-Jan-99 WGS: Aha!  There are two different formats of the time in
# one mail file!  Test for both.

$frompat1 = '^:\t(\S+)'                          # sender address
          . ' +(".+")?'                          # sender name
          . ' *(\d+)-(\w{3})-(\d{2}(\d{2}))'     # dd-mm-yyyy date
          . ' +(\d+:\d+:\d+)\.\d+'               # hh:mm:ss.tt time
          . " *\n" ;

$frompat2 = '^:\t(\S+)'                          # sender address
          . ' +(".+")?'                          # sender name
          . ' *(\d+)-(\w{3})-(\d{2}(\d{2}))'     # dd-mm-yyyy date
          . ' +(\d+:\d+)'                        # hh:mm time
          . " *\n" ;
 
while (<>)                                      # Read next message
    {
    s|$/$|\n| ;                                 # Drop start of next message
 
    if ($. == 1)                                # First message in input file?
        {
        s/^\s*From// ;                          # Handle missing initial \f\n
        next if ! /\S/ ;                        # Handle leading \f\nFrom
        }
    $mesg++ ;                                   # Count messages
 
    if ((($addr, $name, $day, $mon, $year, $yr, $time) = (/$frompat1/))
      ||(($addr, $name, $day, $mon, $year, $yr, $time) = (/$frompat2/)))
        {
        s/^.*\n// ;                                     # drop From: line
        (($to)   = /^To:\t(\S+) */)     && s/^.*\n// ;  # drop To:   line
        (($cc)   = /^CC:\t(.*)/)        && s/^.*\n// ;  # drop CC:   line
        (($subj) = /^Subj:\t(.*)/)      && s/^.*\n// ;  # drop Subj: line
 
        $name =~ s/^"(.+)"$/$1/ ;                       # drop quotes
        $addr =~ s/^IN%"([^"]+)"$/$1/i ;                # fix IN%
        $addr =~ s/^ARPA%"([^"]+)"$/$1/i ;              # fix ARPA%
        $addr =~ s/^JNET%"([^"]+)"$/$1.bitnet/i ;       # fix JNET%
        $addr =~ s/^UUCP%"([^"]+)"$/$1.uucp/i ;         # fix UUCP%
        $addr =~ s/^BITNET%"([^"]+)"$/$1/i ;            # fix BITNET%
        $addr =~ s/^SMTP%"([^"]+)"$/$1/i ;              # fix SMTP%
        $addr =~ s/(\S+)::(\S+)/$2\@$1/ ;               # fix VMS hosts
        $day  = sprintf ("%2d", $day) ;                 # pad date
        $wkdy = &weekday ($day, $mon, $year) ;
 
        print "From \L$addr\E $wkdy \u\L$mon $day $time $year\n";
        print "From: $addr\n"                               unless $name ;
        print "From: $name <\L$addr\E>\n"                       if $name ;
        print "Subject: $subj\n"                                if $subj ;
        print "To: \L$to\n"                                     if $to ;
        print "Date: $wkdy, $day \u\L$mon\E $yr $time\n" ;
        print "Cc: $cc\n"                                       if $cc ;
        print ;
        next ;
        }
    /^(.*)/ && warn ("\nSkipping garbled message \#$mesg:\n\tFrom$1\n") ;
    }
continue
    {
    close (ARGV) if (eof) ;             # Reset line numbers if multiple files
    }
 
sub weekday
    {
    local ($day, $mon, $year) = @_ ;
    %month  = ( jan, 0, feb, 1, mar, 2, apr, 3, may,  4, jun, 5,
                jul, 6, aug, 7, sep, 8, oct, 9, nov, 10, dec, 11 ) ;
    @offset = ( 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 ) ;
    @wkday  = ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ) ;
 
    $m = $month {"\L$mon"} ;                            # month number
    $y = $year - ($year % 4) ;                          # previous leapyear
    $leap = $year == $y ;                               # current leapyear
    $weekday  = 5 ;                                     # for 1/1/2000
    $weekday += 5 * ($y - 2000) / 4 ;                   # for 1/1/$y
    $weekday += ($year - $y) + !$leap ;                 # for 1/1/$year
    $weekday += $offset [$m] + ($leap && ($m > 1)) ;    # for $m/1/$year
    $weekday += $day - 1 ;                              # for $m/$day/$year
    $wkday [$weekday % 7] ;
    }
