#!/usr/bin/perl 
$|=1;

# Copyright (C) 1999 Brent A. Fulgham <bfulgham@debian.org>

# aoldbadd
#
# Program to add a database to postgresql with AOLserver ownership

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#
#  The following are the standard Debian locations for various
#  files and utilities.  You should be able to port this script to
#  other file schemes by only modifying these fields.  However,
#  it hasn't been tested so use caution.
#

$NSD_CONFDIR="/etc/aolserver";
$GROUP="www-data";
$PASSWD="/etc/passwd";

$PGBASE="/usr/lib/postgresql";
$PGLIB="$PGBASE/lib";
$PGBIN="$PGBASE/bin";


#
#  You shouldn't need to modify beyond this point
#

$write_nsdconf = 0;
$changed = 0;
$q = 1;

use Getopt::Long;
use Sys::Hostname;

$opt_domain = "server1";
$opt_assert_perl = 0;

###########################################################################
sub load_config_files ()
{
    $main::nsdconf = `cat $NSD_CONF`;
    $main::servername = loadconf ("ServerName", "localhost");
    $main::serveradmin = loadconf ("ServerAdmin", "www-data");
    $main::serveruser = loadconf ("User", "www-data");
    $main::servergroup = loadconf ("Group", "www-data");
    chomp (my $tmproot = `egrep "^aolserver:" /etc/passwd | cut -f 6 -d :`);
    $tmproot = "/var/www" if ($tmproot eq "");

    $main::servergroup = "www-data" if $main::servergroup =~ m/-/;
    $main::db = "test1";

}

###########################################################################
sub yorn ($;$)
{
    $main::changed = 0;
    my $result = $_[0];
    my $morearg = "";
    print $_[1] if defined $_[1];
    ($result) ? print " [Y/n] " : print " [y/N] ";
    $arg = <STDIN>;
    $result = 1 if ($arg =~ /^y/i);
    $result = 0 if ($arg =~ /^n/i);
    $q = 0 if ($arg =~ /q/i);
    $main::changed = 1 unless ($result == $_[0]);
    return $result;
}

###########################################################################
sub answer ($;$)
{
    $main::changed = 0;
    my $arg;
    my $default = $_[0];
    print $_[1] if defined $_[1];
    print " [$default] " if (defined ($default) && $default ne "");
    chomp ($arg = `read REPLY; echo \$REPLY`);
    if ($arg ne "")
    {
	$main::changed = 1;
	return $arg;
    }
    return $default;
}

###########################################################################
sub loadconf ($$;$)
{
    my $parameter = $_[0];
    my $default = $_[1];

    my $file = (defined ($_[2]) ? $_[2] : "$NSD_CONF");
    my $in = `egrep "^ns_param.*$parameter " $file 2> /dev/null| head -1`;
    $in =~ s/ns_param\s*$parameter //;
    $in =~ s/"//g;
    $in =~ s/ //g;
    chomp ($in);
    return $in if ($in ne "");
    return $default;
}

###########################################################################
sub add_user()
{
    my($userid, @chunks);
    $userid = `grep $main::serveruser $PASSWD`;
    @chunks = split(/:/, $userid);
    $userid = @chunks[2];
    print "Preparing to add user $main::serveruser with UID $userid ... \n";

    # Temporary until we move to Postgres 7.0
    $VERSION=`dpkg -l postgresql | grep postgresql | cut --bytes=19-32 | sed "s/ 	//"`;
    print "The version of PostgreSQL is $VERSION\n";
    if ($VERSION <= "6.5.3-18")
    {
        print "Pre 7.0 PostgreSQL.\n";
        system("su postgres -c 'createuser -d -u -i $userid $main::serveruser'");
    }
    else
    {
        system("su postgres -c 'createuser -d -a -i $userid $main::serveruser'");
    }

    print "Success.\n";
}

###########################################################################
sub add_db()
{
    my($userid, @chunks);
    print "Preparing to create the database $opt_dbname ... ";
    
    system("su postgres -c 'createdb $opt_dbname'")
        && die ("\nCouldn't create database.\n");

    print "Success.\n";
}

###########################################################################
GetOptions ("assert-perl!",
            "addadmin!",
	    "domain=s",
            "aoladmin=s",
	    "dbname=s");

$main::hostname = hostname;

exit 0 if $opt_assert_perl;

# Set correct domain configuration information
if ($opt_domain)
{
    $main::domain = $opt_domain;
    $NSD_CONF="$NSD_CONFDIR/$main::domain.tcl";
}
else
{
    $NSD_CONF="$NSD_CONFDIR/nsd.tcl";
}
load_config_files();

system ("set PGBASE=$PGBASE; export PGBASE");
system ("set PGLIB=$PGLIB; export PGLIB");
system ("set PATH=$PATH:$PGBIN; export PATH");

add_user if ($opt_addadmin);

add_db();

print "aoladddb successful completion.\n";

exit 0;


