#!/usr/bin/perl
#
# Strategy:
# Instead of using net_usr pipe via STDIN, we directly modify the
# database tables and files in the mail directory. I only don't know
# if Franta is using some kind of lock mechanism.
#
# Last updated: DL6RAI, Tue Jul 20 18:25:01 GMT 1999
# 
# Todo: make directories other than `mail' available too for sending mail


require 'getopts.pl';

$home = (getpwnam('clx_us'))[7];
$now = gmtime();
$db_name = "clx_db";
$psql = "psql -d $db_name -qtc";
$from = "system";
$quiet = 0;

&Getopts('qs:t:f:');

$to = lc($opt_t) if ($opt_t);
if (! $to) {
	print <<NNNN;
Usage: $0 -q -s <subject> -t <to> -f <from>

This program is used to turn a file or STDIN into a mail message and
can be used to generate automated mail messages to users on your
system. Currently, mail messages are not being forwarded to other nodes.

At least an addressee must be stated using -t option.
NNNN
	exit 1;
}

$dir = "$home/box/mail/$to";

$from = lc($opt_f) if ($opt_f);
$subject = $opt_s if ($opt_s);
$quiet = 1 if ($opt_1);

# See if postmaster is running
$qry = "\\q";
$cmd = sprintf("%s \"%s\"",$psql,$qry);
$rc = system($cmd);
die "Postmaster is not running - file cannot be sent.\n" if ($rc != 0);

if ($#ARGV < 0) { 
	$ARGV[0] = "-";  # read from STDIN
	$subject = "File transmission" if (! $opt_s);
}

foreach $filename (@ARGV) {

	if ( ! -f $filename && $filename ne '-' ) { 
		print "File $filename does not exist.\n";
		next;
	}

	$subject = $filename if (! $opt_s);
	open(IN,$filename);

	# get latest number from ml_dir, d_count
	$qry = "select d_count from ml_dir where d_name='mail'";
	$cmd = sprintf("%s \"%s\"",$psql,$qry);
	$nr = `$cmd` + 1;

	# update d_last (current date and time), latest number+1
	$qry = "update ml_dir set d_count=$nr, 
		d_last='$now' where d_name='mail'";
	$cmd = sprintf("%s \"%s\"",$psql,$qry);
	system("$cmd");

	# Add an entry to ml_file, f_name is next number 
	# (latest+1), f_thema=mail,
	# f_ldir=addressee, f_size=0 in bytes,f_cntr=?, 
	# f_auth=system or # sender's callsign or system 
	# callsign, f_titel=subject,f_readc=0, f_date=now, 
	# f_dact=now

	$qry = sprintf("insert into ml_file (f_name,f_thema,
		f_ldir,f_size,
		f_auth,f_titel,f_readc,f_date,f_dact) values
		('%d','mail','%s','-1','%s','%s','0','%s','%s')",
		$nr,$to,$from,$subject,$now,$now);
	$cmd = sprintf("%s \"%s\"",$psql,$qry);

	# If necessary, Create a directory adressee 
	# in the ~/box/mail directory.
	mkdir ($dir,0644) if ( ! -d  $dir);
	system("$cmd");

	# Create a file named $nr in ~/box/mail/{addressee}
	$size = 0;
	$output = "$dir/$nr";
	open(OUT,"> $output") || die "cannot open $output\n";
	while(<IN>) {
		$size += length($_);
		print OUT $_;
	}

	# Update record where f_name=latest+1 and 
	# f_thema=mail, set size=filesize
	$qry = "update ml_file set f_size=$size where 
		f_name='$nr' and f_thema='mail'";
	$cmd = sprintf("%s \"%s\"",$psql,$qry);
	system("$cmd");

	close(IN);

	print("File $filename, size $size sucessfully sent to $to as nr. $nr.\n");
}
