#!/usr/ug/bin/perl5
#			vim:fo=croql:cin:com=\:#
#


use strict;
use POSIX qw(strftime);

use vars qw($BON $BOFF $CEOL);



exit if $ENV{'GALE_CATEGORY'} =~ m-/ping$|/ping:-;
$BON= `tput md 2>/dev/null`;
if ($? == 0) {
	$BOFF= `tput me`;
}
else {
	$BON= `tput bold`;
	$BOFF= `tput sgr0`;
}

$CEOL= `tput el`;


if ($ENV{'GALE_CATEGORY'} =~ m-/receipt$|/receipt:-) {
	&printReceipt;
}
else {
	&printMessage;
}

sub printReceipt {
#* <time> Received by <jtr@ugcs.caltech.edu> (John Reese) 12/26 16:29
	print "* ", $ENV{'GALE_TIME_ID_TIME'}, " received: ",
		$ENV{'GALE_TEXT_NOTICE_PRESENCE'}, " ";
	if (defined($ENV{'GALE_SIGNED'})) {
		print "<${BON}", $ENV{'GALE_SIGNED'}, "${BOFF}>";
	}
	else {
		print "*${BON}unverified${BOFF}*";
	}
	print " (", $ENV{'HEADER_FROM'}, ")" if defined($ENV{'HEADER_FROM'});
	print "\n";
}

sub printMessage {
	print "\n\r";
	print "+" if defined($ENV{'GALE_TEXT_QUESTION_RECEIPT'});
	print "[${BON}", $ENV{'GALE_CATEGORY'}, "${BOFF}]";
	print " from ", ${BON}, $ENV{'GALE_TEXT_MESSAGE_SENDER'}, ${BOFF}
	    if defined $ENV{'GALE_TEXT_MESSAGE_SENDER'};
	print " to ${BON}", $ENV{'GALE_TEXT_MESSAGE_RECIPIENT'}, ${BOFF}
	    if defined $ENV{'GALE_TEXT_MESSAGE_RECIPIENT'};

	print "\n";
	system("/bin/cat", "-v");

	my $x= "-- ";
	if (defined($ENV{'GALE_SIGNED'})) {
		$x .= $BON . $ENV{'GALE_SIGNED'} . $BOFF;
	}
	else {
		$x .= "${BON}*anonymous jackass*${BOFF}";
	}
	$x .= " for ";
	
	if (defined($ENV{'GALE_ENCRYPTED'})) {
		$x .= ${BON} . $ENV{'GALE_ENCRYPTED'} . $BOFF;
	}
	else {
		$x .= "${BON}*everyone*${BOFF}";
	}

	$x .= " at " . substr($ENV{'GALE_TIME_ID_TIME'}, 5) if
		defined($ENV{'GALE_TIME_ID_TIME'});
	my $xlen= length($x) - 2 * (length($BON) + length($BOFF));

	print ' ' x (79 - $xlen), "${BON}$x${BOFF}\n";
}


