#!/usr/bin/perl
#
#   cvs_notify - This program can be run from the $CVSROOT/CVSROOT/notify
#                   file as the notification system rather than just using
#                   Mail.
#
#	Copyright (C) 2002 Arizona Board of Regents on behalf of the
#   Planetary Image Research Laboratory, Lunar and Planetary Laboratory
#   at the University of Arizona
#
#   CS $Id: cvs_notify,v 1.2 2002/02/17 00:46:54 rbeyer Exp $
#
#
#   License & Copyright Information
#   -------------------------------
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
#   Program Description
#   ------------------- 
#
#   This file was written alongside CVS 1.11, and may have
#   issues (but probably not) with later versions.
#
#   In order for this Perl program to work on your system, you 
#   may have to change the path to the Perl executable on the
#   first line of this program.  Additionally, you may need to
#   install the Net Perl module and the Mail-Tools Perl module,
#   if they are not installed on your system.
#
#   Usage from within the $CVSROOT/CVSROOT/notify file:
#
#       ALL cvs_notify.pl -m %s
#
#   where ALL can be replaced with DEFAULT or regular expressions, see 
#   the information on the CVS admin files.


use Sys::Hostname;
use Net::SMTP;
use Mail::Header;
use Getopt::Std;

my $usage = qq{cvs_notify.pl Usage:
    cvs_notify.pl
        -m <mailto address>
        [-u <from user>]
        [-s <subject>]
        [-e]

    This program requires a <mailto address> to send the e-mail to.
    Additionally, it listens to STDIN for creating the body.

    -m The <mailto address> is the address that this e-mail will get
           sent to.
    -u If a value for <from user> is not specified, then the
           program will use your username.
    -s The <subject> will be used as the subject of the e-mail.
    -e If this optional switch is given, the first line from STDIN will
       be appended to the Subject of the e-mail.};

# Parse command line arguments
my %option = ();
unless( getopts ('m:u:s:e', \%option) ) {die "$usage\n";}

unless( $option{m} )
    {
    die "A <mailto address> is required.\n$usage\n";
    }
my $to_address = $option{m};

# Get a login name for the sender
my $from_address = $option{u};
unless( $from_address )
    {
    $from_address = getlogin || (getpwuid($<))[0] || "nobody";
    }

# Gather the stuff from STDIN
my @body = <STDIN>;


# Send the mail.
my $smtphost = hostname();
my $helo = $smtphost;
my $subject = $option{s};
if( $option{e} )
    {
    my $first_line_of_body = $body[0];
    chomp( $first_line_of_body );
    $subject .= " $first_line_of_body";
    }

# Form the Header
my $header = Mail::Header->new();
map{ $header->add('To', $_); } $to_address;
$header->combine('To',',');
$header->add('From',$from_address);
$header->add('Subject',$subject);

## The header and the body are formed, let's send the e-mail:
my $smtp = Net::SMTP->new($smtphost, Hello => $helo,);
my $ok = $smtp->mail($from_address) &&
    $smtp->to($to_address) &&
    $smtp->data(join("",@{$header->header},"\n",@body));
$smtp->quit();


## Must exit cleanly
exit(0);
