#!/usr/local/bin/perl

# NAME
#    postclip - send only the headers to Postmaster
#
# SYNOPSIS
#    postclip [ -v ] [ to ... ]
#
# AUTHOR
#    Michael S. Muegel <mmuegel@mot.com>
#
# RCS INFORMATION
#    /usr/local/ustart/src/mail-tools/dist/foo/src/postclip,v
#    1.1 of 1993/07/28 08:09:02

# We use this to send off the mail
require "newgetopts.pl";
require "mail.pl";

# Get the basename of the script
($Script_Name = $0) =~ s/.*\///;

# Some famous constants
$USAGE          = "Usage: $Script_Name [ -v ] [ to ... ]\n";
$VERSION        = "${Script_Name} by mmuegel; 1.1 of 1993/07/28 08:09:02";
$SWITCHES       = "v";

# Let getopts parse for switches
$Status = &New_Getopts ($SWITCHES, $USAGE);
exit (0) if ($Status == -1);
exit (1) if (! $Status);

# Who should we send the modified mail to?
@ARGV = ("postmaster") if (! @ARGV);
$Users = join (" ", @ARGV);
@ARGV = ();

# Suck in the original header and save a few interesting lines
while (<>) 
{
    $Buffer .= $_ if (! /^From /);
    $Subject = $1 if (/^Subject:\s+(.*)$/);
    $From = $1 if (/^From:\s+(.*)$/);
    last if (/^$/);
};

# Do not filter the message unless it has a subject and the subject indicates
# it is an NDN
if ($Subject && ($Subject =~ /^returned mail/i))
{
   # Slurp input by paragraph. Keep track of the last time we saw what
   # appeared to be NDN text. We keep this.
   $/ = "\n\n";
   $* = 1;
   while (<>)
   {
      push (@Paragraphs, $_);
      $Last_Error_Para = $#Paragraphs 
	 if (/unsent message follows/i || /was not delivered because/);
   };

   # Now save the NDN text into $Buffer
   $Buffer .= join ("", @Paragraphs [0..$Last_Error_Para]);
}

else
{
   undef $/;
   $Buffer .= <>;
};

# Send off the (possibly) modified mail
($Status, $Msg) = &Send_Mail ($Users, "", $Buffer, 0, $opt_v, 1);
die "$Script_Name: $Msg\n" if (! $Status);
