#!<PathToPerl>
#
# This script grabs waiting email from a Pilot and pipes it through sendmail.
# It is written in perl 5, though it may work just fine in earlier versions.
# I wrote it on a Sparc 20 running SunOS 4.1.3.  I make no warrenties for
# complete failure on other machines, though you can contact me for help if
# you encounter problems.
#
# Leave the below comment in here, since you are going to have to put in the
# hard-coded paths.  THIS IS NOT A GENERIC TOOL IN ANY WAY!
#

$Debug = 0;             # When the $Debug option is zero, no debug 
                        # statements are executed.
$Line = 0;              # The line number of the file (Debug parameter if
                        # it is still used)

$Start = 0;             # Flag to indicate whether it is an [email] type
                        # memo.

print ("Hit the HotSynch Button on your Pilot cradle\n");
                        # Instructions for the user

system ('<PathToMemos> <PathToDevice> > <PathToTempMemoFile>');
                        # Run Kenneth's memo downloader and put the results
                        # into a temporary file.

print ("Mailing files...\n");
                        # More user info

open (FILE, "<PathToTempMemoFile>");
                        # open the memo file
open (FILE2, "><PathToTempMailFile>");
                        # open a temporary file which is to be mailed
print (FILE2 "Subject: <GenericSubjectLine>\n");
                        # Stock subject line -- will change in later versions


while (<FILE>)          # While we are in the temporary memo file...
{
  if (($_ =~ /^Subject\: \[email\]/) && ($Start == 0))
  {                     # If it is an 'email' type memo and our flag
                        # indicates we are in the header
    ($TempSub, $TempCategory, $To) = split (/ /, $_, 3);
                        # Chop off 'Subject:' and '[email]'
    chop ($To);
                        # kill the trailing carriage return
    if ($Debug > 0) { print ("To: $To\n"); }
                        # Debug statement
    $Start = 1;         # Set our flag so we don't keep doing this
    next;               # go the the next line in the file
  }

  if (($_ =~ /^From your.pilot/) && ($Start == 1))
  {                     # If a new memo has been encountered...
    $Start = 0;         # Reset the flag...
    
    close (FILE2);      # Close an open file if necessary so we can mail it

  print ("\nMailing message to $To...");
                        # Tell the user what you're doing
  system ("cat <PathToTempMailFile> | <PathToSendmail> $To");
                        # Send the mail
  print ("Sent!\n");    # Tell the user the mail was sent

    system ('rm <PathToTempMailFile>');
                        # delete the temporary file
    open (FILE2, "><PathToTempMailFile>");
                        # open a new file so we can begin putting stuff
                        # into it
    print (FILE2 "Subject: <GenericSubjectLine>\n");
                        # Put in the crappy hard coded subject

    next;               # Go to the next line
  }
  elsif ($_ =~ /^Received\: Pilot/)
  {                     # If we are in the header...
    next;               # just ignore it
  }
  elsif ($_ =~ /^To\: you/)
  {                     # If we are in the header...
    next;               # just ignore it
  }
  elsif ($Start == 1)
                        # If the flag is set...
  {
    if ($Debug > 1) { print ("$To"); }
                        # Debug statement

    chop($_);           # Kill the trailing carriage return

    if ($_ ne $To)
    {                   # if this isn't the first line in the memo... 
      print (FILE2 "$_\n");
                        # then put it in the file to be mailed
    }

  }

}

if ($Start == 1)
{                       # We are out of the file, but might still have a
                        # message to be mailed since I was using the next
                        # memo to trigger when to send the one I was holding
                        # onto
  close (FILE2);        # close the open file so we can mail it.

  print ("\nMailing message to $To...");
                        # tell the user we are mailing a file
  system ("cat <PathToTempMailFile> | <PathToSendmail> $To");
                        # mail the file
  print ("Done!\n");    # Tell the user you are done with mailing

  system ('rm <PathToTempMailFile>');
                        # remove the file to me mailed
}

close (FILE2);          # close the mailed file like a responsible adult
close (FILE);           # close the memo file like a responsible adult

system ('rm <PathToTempMemoFile>');
                        # remove the memo file

exit;                   # Say "Good night," Gracie.
 
