#!/usr/bin/perl
#	testpath a b c ...
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Generate a test message to a!b!c!...  and mail it.  Note that we first scan #
# for  -* options, and convert them into an option list.  This works only for #
# simple options without args.  This script is mostly called with an optional #
# -v  as  an  option  and  a single destination arg.  It is useful to have it #
# catenate args into a mailpath, due to the problems with typing '!' in a csh #
# environment.                                                                #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
chop($H = `hostname`);
$S = $ENV{"USER"} || $ENV{"LOGNAME"} || getlogin 
	|| (getpwuid($>))[0] || (getpwuid($<))[0] || "unknown";
$T = 'bang';
@O = ();
$P = '';
for $a (@ARGV) {
	if ($a =~ /^[-+](.*)/) {
		$o = $1;
		if ($o =~ /^%/) {
			$T = 'pct';
		} elsif ($o =~ /^@/) {
			$T = 'at';
		} elsif ($o =~ /^::/) {
			$T = 'dec';
		} elsif ($o =~ /^!/) {
			$T = 'bang';	# Default.
		} else {
			@O = ($O , $a);
		}
	} elsif ($a =~ '^%$') {
		$T = 'pct';
	} elsif ($a =~ '^\@$') {
		$T = 'at';
	} elsif ($a =~ '^::$') {
		$T = 'dec';
	} elsif ($a =~ /^!$/) {
		$T = 'bang';	# Default.
	} else {
		if ($T eq 'bang') {
			$P = $P . '!' . $a;
		} elsif ($T eq 'dec') {
			$P = $P . '::' . $a;
		} elsif ($T eq 'pct') {
			$P = $P . '%' . $a;
		} elsif ($T eq 'at') {
			$P = $P . '@' . $a;
		} else {
			print STDERR "### Can't handle address type \"$T\"\n";
			exit 1;
		}
	}
}
$P =~ s'^[!%@:]*'';
$P =~ s'[!%@:]*$'';
open(SM,"| smail @O $P") || die "Can't run smail.";
print SM "Subject: test\n";
print SM "\n";
print SM "This is a test of mail:\n";
print SM "Command: " , $0 , "\n";
print SM " SentTo: " , $P , "\n";
print SM " Origin: " , $H , '!' , $S , "\n";
exit 0;
