#!/usr/local/bin/perl

#
# dbformmail
# Copyright (C) 1997-1998 by John Heidemann <johnh@isi.edu>
# $Id: dbformmail,v 1.11 1999/11/23 02:04:29 johnh Exp $
#
# This program is distributed under terms of the GNU general
# public license, version 2.  See the file COPYING
# in $dblibdir for details.
#
sub usage {
    print <<REALEND;
usage: $0 [-ds] form

Read a ``form mail'' message from a file,
filling in underscore-preeded column-names with data.
Produces a shell script which will send each message through sendmail.

Do not use this program for evil or I will have to come over
and have words with you.

Options:
    -s write a script to send the mail (the default)
    -S send the mail directly (not currently supported)
    -d debugging mode

Sample input:
#h account passwd uid gid fullname homedir shell
johnh * 2274 134 John_Heidemann /home/johnh /bin/bash
greg * 2275 134 Greg_Johnson /home/greg /bin/bash
root * 0 0 Root /root /bin/bash
# this is a simple database

Sample form (in the file form.txt):
    To: _account
    From: the sysadmin <root>
    Subject: time to change your password

    Please change your password regularly.


Sample command:
cat DATA/passwd.jdb | dbformmail form.txt

Sample output:
#!/bin/sh
sendmail 'johnh' <<'END'
To: johnh
From: the sysadmin <root>
Subject: time to change your password

Please change your password regularly.

END
sendmail 'greg' <<'END'
(etc.)

REALEND
    #';   hack for font-lock mode
    exit 1;
}

BEGIN {
    $dblibdir = "/usr/local/lib/jdb";
    push(@INC, $dblibdir);
}
require "$dblibdir/dblib.pl";
use DbGetopt;

my(@orig_argv) = @ARGV;
my($prog) = &progname;
my($write_script) = 1;
my($debug) = 0;
my($dbopts) = new DbGetopt("ds?", \@ARGV);
my($ch);
while ($dbopts->getopt) {
    $ch = $dbopts->opt;
    if ($ch eq 's') {
	$write_script = 1;
    } elsif ($ch eq 'd') {
	$debug++;
    } else {
	&usage;
    };
};
&usage if ($#ARGV != 0);
my($form_file) = $ARGV[0];
die ("$prog: non- -s verison not currently implemented.\n") if (!$write_script);

#
# Read the form.
#
open(FORM, "<$form_file") || die("$prog: cannot open $form_file.\n");
@form = ();
while (<FORM>) {
    s/\@/\\\@/;   # quote @'s
    push(@form, $_);
}
close FORM;
die ("$prog: no To: line in form.\n")
    if (!grep(/^To:/i, @form));


#
# Do it.
#
&readprocess_header;
# Generate the code.
my($code) = codify("<<END;\n" . join("", @form) . "END\n");

print $code if ($debug);

print "#!/bin/sh\n";

while (<STDIN>) {
    if (&is_comment) {
	push(@comments, $_);
	next;
    };
    &split_cols;
    $result = eval $code;   $@ && die ("$prog: internal eval error ``$@''.\n");

    # This is not a very elegant to extract the destination.  :-<
    my($to) = undef;
    foreach (split(/\n/, $result)) {
	next if (!/^to:\s*(.*)$/i);
	$to = $1;
	last;
    };
    die ("$prog: to missing.\n") if (!defined($to));

    # Quote single quotes in $to.
    $to =~ s/\'/\'\\\'\'/g;

    print "sendmail '$to' <<'END'\n$result\nEND\n\n";
};

print "\n" . join("", @comments) . "# | $prog " . join(" ", @orig_argv) . "\n";
exit 0;
