#!/usr/local/bin/perl
#
# $Header: /home/makoto/perl/mail/spool-to-procmail,v 1.1 2000/06/17 22:23:24 makoto Exp $
# $Log: spool-to-procmail,v $
# Revision 1.1  2000/06/17 22:23:24  makoto
# Initial revision
#
  $rcsid = q$Id: spool-to-procmail,v 1.1 2000/06/17 22:23:24 makoto Exp $;

#  home directory $B$,0n$l$F!"(B/var/mail/makoto $B$KF~$C$F$7$^$C$?;~$K!"(B
#  $B$b$&0lEY(B procmail $B$r5/F0$9$k$3$H$K;H$&!#(B
# 
#------------ com_leaf for appropriate help message ----
# command name, strip all before '/' inclusively
# /home/fujiwara/perl/com_path-test -> com_path-test
$my_name = $0;
$my_name =~ s#(.*)/##;
#------------ com_path for getting right @INC path -----
# command location, strip rightmost '/' and right of it.
# /home/fujiwara/perl/com_path-test -> /home/fujiwara/perl
$my_path = $0;
$my_path =~ s|/[^/]+$||;
#---------------------
push(@INC,$my_path);
require 'getopts.pl';

&Getopts('h');  
# ----------------- help -------------
if ( $opt_h ) {  &usage ; }
$SPOOL  = '/var/mail/';
$SPOOL .= $ENV{'USER'};
if ( $ARGV[0] ) { 
    $SPOOL = $ARGV[0];}
if ( ! -r $SPOOL ) {
    die " spool($SPOOL) is not readable.\n";}

open(SPOOL) || " print problem opening SPOOL($SPOOL)\n";
# -----------------------------------
$in_mail = 0;
while (<SPOOL>) { 
    if ( /^From / ) {
	print "** processing: $_";
	if (flush() == 0 ) {
	    $in_mail++; } # very first ^From: line 
	push(@list,$_)}
    else {
	push(@list,$_);}}
flush();
exit;
# -----------------------------------
#       S U B   R O U T I N E S  
# -----------------------------------
sub flush    {
	if ( $#list > -1 ) {
	    procmail();
	    @list = ();	
	    return 1;}
	return 0; }
sub procmail {
    open(PROCMAIL,"|procmail") || print " problem invoking procmail\n";
    print PROCMAIL @list;
    close(PROCMAIL); }
# ----------------
sub usage {
    print <<HELPHELP;
$my_name: 
Synonyms:
  $my_name [ -d ] [ -h ] 
Where:

Note:

version: $rcsid
HELPHELP
    exit;
}
__END__
