#!/usr/local/bin/perl -w
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  Fwd - Forward a file or email message to a list of recipients
#
#SYNOPSIS
#  Fwd [file] to rcpt..
#
#REQUIRES
	$LNAM = $ENV{'LOGNAME'} || $ENV{'USER'};
    $HOME = $ENV{'HOME'} || "/home/$LNAM";
    @INC = ($HOME, "$HOME/mh","$HOME/sh",@INC);
    require 'jcmh.pm';
#
#DESCRIPTION
#
#OPTIONS
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu> 2005-4-21
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$exitstat = 0;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || $ENV{"D_$P"} || 1;	# Verbose level.

$fldr = &getfolder();
print STDERR "$P: fldr='$fldr'\n" if $V>1;

$phase = 'file';
for $arg (@ARGV) {
	if ($phase eq 'file') {
		if ($arg eq 'to') {
			$phase = 'rcpt';	# Args are now recipients
			print STDERR "$P: phase='$phase'\n" if $V>2;
		} else {
			if (-f $arg) {		# File arg 
				print STDERR "$P: File '$arg'\n" if $V>2;
				push @files, $arg;
			} elsif ($arg =~ /^\d+$/) {	# Numeric arg is message number
				print STDERR "$P: Msg# '$arg'\n" if $V>2;
			} else {
				print STDERR "$P: Arg '$arg' not a file or number.\n" if $V>0;
			}
		}
	} elsif ($phase eq 'rcpt') {
		print STDERR "$P: Rcpt '$arg'\n" if $V>2;
		push @rcpts, $arg;
	} else {
		print STDERR "$P: Don't recognize arg \"$arg\"\n" if $V>0;
		++$exitstat;
	}
}

unless (@files) {
	$ndx = &getindex();
	print STDERR "$P: Current msg is $FDIR/$ndx.\n" if $V>1;
	push @files, "$FDIR/$ndx";
}

unless (@rcpts) {
	print STDERR "$P: No recipients!\n" if $V>0;
}

for $f (@files) {
	for $r (@rcpts) {
		print STDERR "$P: Send '$f' to '$r'\n" if $V>1;
		$cmd = "Send file $f to $r";
		system $cmd;
		if ($?) {
			print STDERR "$P: Exit $? from \"$cmd\"\n" if $V>0;
			++$exitstat;
		}
	}
}

print "$P: Exit with status $exitstat.\n" if $V>1;
exit $exitstat;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
