#!/usr/bin/perl

# Copyright (c) 2000-2001, Will Guaraldi
# 
# Permission is hereby granted, free of charge, to any person obtaining a 
# copy of this software and associated documentation files (the "Software"), 
# to deal in the Software without restriction, including without limitation 
# the rights to use, copy, modify, merge, publish, distribute, sublicense, 
# and/or sell copies of the Software, and to permit persons to whom the 
# Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in 
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
# SOFTWARE.
# --------------------------------------------------------------------------


$vercredit = "\n-----\nnew day faq: http://www.bluesock.org/~willg/newday.php\nmailday.pl v.1.8.4, last edited: 03/12/2002, wbg\n\n";
$maildayrc = "maildayrc";
$tempfile = "/tmp/001100tmp";
$emailaddr = "willg";

if (@ARGV[0]) {
   $maildayrc = @ARGV[0];
}

open (FILE, $maildayrc) or die "no maildayrc file!";

$subject = "The new day cometh? ".`date +%m-%d-%Y`;
chomp $subject;

$newmessage = "\n";

while (!eof(FILE)) {
   $newline = <FILE>;

   if ($newline =~ m/^message/) {
      $message = $newline;
      chomp $message;
      $message =~ s/^message: (.*)/$1/;
   }

   if ($newline =~ m/^address/) {
      # print "address --> $newline\n";
      $add = $newline;
      chomp $add;
      $add =~ s/^address: (.*)/$1/;

      if (open (NEWITEM, $add)) {
         @addresslisti = <NEWITEM>;
      }
      close (NEWITEM);

      for (@addresslisti) {
         chomp;

         # remove comments from the end of the line
         s/\#.*$/ /g;
         if (!(m/^\s*$/g)) {
            push @addresslist, $_;
         }
      }
      $addresses = join(" -b ", @addresslist);
      $addresses = "-b ".$addresses;
      $addresses =~ s/\n/ /g;
      $addresses =~ s/-b\s*-b/-b/g;
   }

   if ($newline =~ m/^item/) {
      $newitem = $newline;
      chomp $newitem;
      $newitem =~ s/^item: (.*)/$1/;

      # print "item --> '$newitem'\n";
      if (open (NEWITEM, $newitem)) {
         open (NEWFILE, "> $tempfile");
         $/ = "\n%\n";
         $joe = <NEWITEM>;
         print NEWFILE $joe;
         $newmessage = $newmessage.$joe;

         $joe2 = <NEWITEM>;
         $newmessage = $newmessage.$joe2;
         $newmessage = $newmessage."\n";

         while (!eof(NEWITEM)) {
            $joe = <NEWITEM>;
            print NEWFILE $joe;
         }
         print NEWFILE $joe2;
         close NEWFILE;
         close NEWITEM;
         system ("cp -f $tempfile $newitem");
      } else {
         print "$newitem: non-existant\n";
      }
      $/ = "\n";
   }
}
close (FILE);

$newmessage = $newmessage."\n";
$newmessage =~ s/\n\%\n/\n\n/g;

if ($message ne "0") {
   if (open (MESSAGE, $message)) {
      @endmessage = <MESSAGE>;
      $sclendmessage = join ("", @endmessage);
      $newmessage = $newmessage.$sclendmessage;
      close (MESSAGE);
   } else {
      print "$message: non-existant\n";
   }
}

$newmessage = $newmessage.$vercredit;
$newmessage =~ s/\`/\'/g;
$newmessage =~ s/\$/\\\$/g;
$runstring = "cat <<:EOD | mail -s '$subject' $emailaddr $addresses  \n$newmessage";
if ($ARGV[0] ne "test") {
   system($runstring);
   system("cat <<:EOD > /home/willg/mailday/m\n$newmessage");
} else {
   print $runstring;
}
