#!/afs/cmf/@sys/bin/perl
#
# $Id: sfax,v 1.12 1995/09/15 17:00:21 kenh Exp $
#
# sfax - cheap command-line front end to HylaFAX
#

#
# The following three fields will probably have to be changed for your site
#

$faxcover='faxcover';				# location of faxcover binary
$sendfax='sendfax';				# location of sendfax binary
$sendmail='/usr/lib/sendmail -t';		# mail program
$faxform='/usr/local/lib/FAXform';		# location of form file

%faxheaders = ('FaxTo:','faxto','FaxNumber:','faxnumber','FaxCompany:',
		'faxcompany','FaxLocation:','faxlocation','FaxCommentStart:',
		'dummy','FaxCommentEnd:',dummy,'FaxSubject:','faxsubject',
		'Cc:',carboncopy);

(($fromname, $homedir) = (getpwuid($<))[6,7]) ||
	die "Who the hell are you??\n";

$fromname =~ s/,.*$//;		# Edit out other user info in GCOS field

$SIG{'INT'} = 'sigabort';
$SIG{'QUIT'} = 'sigabort';
$SIG{'TERM'} = 'sigabort';

$tmpfaxcover="/tmp/sfaxcover.$$";
$tmpfile="/tmp/sfax.$$";
$textfile="/tmp/sfaxtxt.$$";

#
# Check to see if the person has a "dead fax" file, and ask if they want to
# use that instead
#

if (-r "$homedir/dead.fax") {
	$q = &response("$homedir/dead.fax exists - load that instead?",'y');
	if ($q =~ /^[Yy]/) {
		system("mv $homedir/dead.fax $tmpfile");
	} else {
		system("cp $faxform $tmpfile");
	}
} else {
	system("cp $faxform $tmpfile");
}

if ($?) {
	die "Could not copy FAXform to temporary file\n"
}

$editor = $ENV{'EDITOR'} || 'vi';
system("$editor $tmpfile");

&parsefile($tmpfile);

&abort("No fax number specified") if ! $faxnumber;

#
# Ask the user if they want to add files to the fax
#
$fileq = &response("Append file(s) to fax?","n");

if ($fileq =~ /^[Yy]/) {
	print "\nPlease enter filenames below, one per line.\n";
	print "Hit <return> on a blank to to exit.\n";

	while(1) {
		print "filename: ";
		chop($file = <STDIN>);
		last if ! $file;
		if (! -e $file) {
			$fileq = &response("File \"$file\" does not exist.  Add to fax list anyway?",'n');
			if (! ($fileq =~ /^[Yy]/)) {
					next;
			}
		}
		print "Adding file \"$file\" to fax list\n";

		push(@filelist,$file);
	}
}

#
# Verify everything
#
print <<EOF;

Sending the following fax:

	Name of recipient: 	$faxto
	Fax number:		$faxnumber
	Recipient's company:	$faxcompany
	Recipient's location:	$faxlocation
	Subject of fax:		$faxsubject

EOF

print "Cover page comments:\n\n$comment\n\n" if $comment;

print "Text message included\n" if $faxtxt;

print "Carbon copy will be e-mailed to: $carboncopy\n" if $carboncopy;
print "\n";

if (@filelist) {
	print "Files to append to fax:\n\n";

	foreach (@filelist) {
		print "\t\t$_\n";
	}
}

$q = &response('Send this fax?','y');

#
# Actually send the fax.  Build up an argument list and then call sendfax
#

if ($q =~ /^[Yy]/) {

	#
	# Since we want to be able to only send a cover page, we call
	# faxcover by hand
	#

	push(@faxcoverargs, '-x', $faxcompany) if $faxcompany;
	push(@faxcoverargs, '-l', $faxlocation) if $faxlocation;
	push(@faxcoverargs, '-r', $faxsubject) if $faxsubject;
	push(@faxcoverargs, '-c', $comment) if $comment;
	push(@faxcoverargs, '-t', "$faxto");
	push(@faxcoverargs, '-f', "$fromname");
	push(@faxcoverargs, '-n', "$faxnumber");

	print "$faxcover @faxcoverargs \> $tmpfaxcover\n";

	#
	# Sigh.  I wish open() could take argument as lists and assign
	# each element to argv[], like system() and exec() can.
	#

	open(COVEROUT, ">$tmpfaxcover") ||
	     &abort("Could not open temporary file for cover page, aborting");

	if (($pid = fork()) == 0) {
		open(STDOUT, ">&COVEROUT");
		exec($faxcover, @faxcoverargs);
	}

	waitpid($pid, 0);

	if ($?) {
		&abort("faxcover failed, fax was not sent");
	}

	push(@faxargs, '-d', "$faxnumber");
	push(@faxargs, '-n', '-D');
	push(@faxargs, $tmpfaxcover);
	push(@faxargs, $textfile) if $faxtxt;
	push(@faxargs, @filelist);

	print "$sendfax @faxargs\n";
	system($sendfax,@faxargs);
	if ($?) {
		&abort("Sendfax failed, fax was not sent");
	}
} else {
	&abort("Fax aborted");
}

print "Fax queued successfully\n";

#
# Carbon copy a user via e-mail, if so directed
#

if ($carboncopy) {
	if ($faxtxt) {
		open(TMP,$textfile) ||
			&abort("Could not open temp file for carbon copy");
	}
	open(MAIL,"|$sendmail") || die "Could not pipe to sendmail: $!\n";

	print MAIL "To: $carboncopy\n";
	print MAIL "Subject: Fax Message CC: $faxsubject\n" if $faxsubject;
	print MAIL "\nSFax: Carbon copy from fax sent to ";
	print MAIL "$faxto \@ " if $faxto;
	print MAIL "$faxnumber\n";
	if ($comment) {
		print MAIL "SFax: Fax cover page comment:\n";
		print MAIL "SFax:\t$comment\n";
	}

	if (@filelist) {
		print MAIL "SFax: Files appended to fax:\n";
		foreach (@filelist) {
			print MAIL "SFax:\t$_\n";
		}
	}

	print MAIL "\n";

	if ($faxtxt) {
		while(<TMP>) {
			print MAIL;
		}
		close TMP;
	}

	close MAIL;

	print "Carbon copy mailed to: $carboncopy\n";
}

#
# Remove temp files and exit
#
unlink($tmpfaxcover, $tmpfile, $textfile);

exit(0);
	
#
# Parse a fax form template file
#
sub parsefile {
	local($filename) = shift @_;

	open(FILE,$filename) || die "Could not open file $filename: $!\n";

# This is icky

	while(<FILE>) {
		next if /^#/;
		next if /^$/;
		if (/^(\S+:)\s*(\S.*)?$/) {
			if ($faxheaders{$1}) {
				eval "\$$faxheaders{$1} = '$2';";
				if ($1 eq "FaxCommentStart:") {
					$faxcomment = 1;
				} elsif ($1 eq "FaxCommentEnd:") {
					$faxend=1;
					last;
				}
			} else {
				&abort("Unknown header item: $1");
			}
		} elsif ($faxcomment) {
			chop($_);
			$comment .= $_ . ' ';
			next;
		} else {
			&abort("Bad template line: \"$_\"");
		}
	}

	if ($faxend && !eof(FILE)) {
		open(TMPFILE,">$textfile") ||
			die "Cannot open temp file \"$textfile\": $!\n";
		while(<FILE>) {
			print TMPFILE;
		}
		close TMPFILE;
		$faxtxt = 1;
	}
		
}

#
# Abort the fax - save a copy of the template and clean up
#

sub abort {
	local($message) = @_;

	system("cp $tmpfile $homedir/dead.fax");
	unlink($tmpfaxcover,$tmpfile,$textfile);
	warn "$message\n";
	warn "A copy of your fax template has been saved in $homedir/dead.fax\n";
	exit(1);
}
		
sub sigabort {
	&abort("Received interrupt signal");
}

#
# Ask a question, get a response, with optional default
#

sub response {
	local($text,$default,$nullok) = @_;
	local($reply);

	while(! $reply) {

		print $text;
		print " [$default]" if $default;
		print " ";

		chop($reply=<STDIN>);
		if ($default && $reply eq '') {
			$reply = $default;
		}
	}

	return $reply;
}
