#!/usr/bin/perl -wT

################################################################
# @(#) $Id: db_admin.pl,v 1.5 2000/09/13 01:58:33 andr Exp $
# 
# A.Gaponenko, August 2000
#

use Pg;

delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};   # Make %ENV safer
$ENV{'PATH'} = "/bin:/usr/bin";

umask 077;

sub mylog {
#    local $LOG;
#    open(LOG, ">>/tmp/admlog") or die "Can't open log";
#    print LOG scalar(localtime()) . ": @_\n";
    print "@_<br>\n";
#    close(LOG);
}

################################################################
# The following information is required:
my @fields = ('firstname', 
	      'lastname',
	      'othernames',
	      'login',
	      'email',
	      'phone',
	      'pw',
	      'admin',
	      'apw',
	      'dbname'
	      );

my %input;
foreach my $i (@fields) {
    $input{$i} = undef;
}

# Read data
MAIN:
    while($line = <STDIN>){
	chomp($line);
	last MAIN if($line =~ /^EOF$/);
##	mylog "Read: $line<br>\n";

	if ($line =~ /^([^:]+):(.*)$/) {
	    $input{$1} = $2;
	} else {
	    die mylog("Bad line:\n$line\n");
	}
    }

# Check that all data required were defined.
foreach my $i (@fields) {
    die mylog("Error: undefined $i\n")
	unless defined($input{$i});
}

################################################################
## mylog("OK, all data are here!\n");

$conn = Pg::connectdb("dbname=$input{dbname}"
		      ." user=$input{admin} "
		      ." password=$input{apw} "
		      );
die mylog("Can't connect to the database.")
    if($conn->status != PGRES_CONNECTION_OK);

$result = $conn->exec("CREATE USER $input{login}"
		      ." IN GROUP e614writers;");
die mylog("Can't CREATE USER")
    if($result->resultStatus != PGRES_COMMAND_OK);

$result = $conn->exec("BEGIN;");
die mylog("Can't BEGIN")
    if($result->resultStatus != PGRES_COMMAND_OK);

$result = $conn->exec("LOCK TABLE users IN EXCLUSIVE MODE;");
if($result->resultStatus != PGRES_COMMAND_OK) {
    $conn->exec("ROLLBACK;");
    die mylog("Can't LOCK TABLE users");
}

$result = $conn->exec("INSERT INTO users"
		      ." (id,firstname,lastname,"
		      ."  othernames,email,phone)"
		      ." SELECT usesysid,"
		      ."'$input{firstname}',"
		      ."'$input{lastname}',"
		      ."'$input{othernames}',"
		      ."'$input{email}',"
		      ."'$input{phone}'"
		      ."  FROM pg_user"
		      ." WHERE usename='$input{login}';"
		      );

if($result->resultStatus != PGRES_COMMAND_OK) {
    $conn->exec("ROLLBACK;");
    $conn->exec("DROP USER $input{login};");
    die mylog("Can't INSERT INTO users");
}

################
## Set passwd

local $PWPROG;
if(!open(PWPROG, 
	 "|/usr/bin/pg_passwd "
	 ."/var/lib/pgsql/data/passwd "
#	 ."/tmp/passwd "
	 .">/dev/null 2>&1")) 
{
    $conn->exec("ROLLBACK;");
    $conn->exec("DROP USER $input{login};");
    die mylog("Can't pipe to PWPROG");
}

print PWPROG "$input{login}\n$input{pw}\n$input{pw}\n"
and close(PWPROG)
or (
    $conn->exec("ROLLBACK;"),
    $conn->exec("DROP USER $input{login};"),
    die mylog("Can't write to PWPROG")
    );

#####
$result = $conn->exec("COMMIT;");
if($result->resultStatus != PGRES_COMMAND_OK) {
    $conn->exec("ROLLBACK;");
    $conn->exec("DROP USER $input{login};");
    die mylog("Can't COMMIT");
}

mylog("Successfully added user $input{login}.");

exit 0;
################################################################
