#!/usr/bin/perl

# eg_db_config.pl -- install/configure the eg database/db settings
# Author: Kevin Beswick <kevinbeswick00@gmail.com>

use strict; use warnings;
use XML::LibXML;

my ($dbhost, $dbport, $dbname, $dbuser, $dbpw);
my $config_file = '';
my $build_db_sh = '';
my $cschema = 0;
my $uconfig = 0;


sub update_config {

# Puts command line specified settings into xml file

my $parser = XML::LibXML->new();
my $opensrf_config = $parser->parse_file($config_file);

my @query;
$query[0] = "//database/host/text()";
$query[1] = "//database/port/text()";
$query[2] = "//database/db/text()";
$query[3] = "//database/user/text()";
$query[4] = "//database/pw/text()";
my @params = @_;

for(my $count = 0 ; $count < 5 ; $count++){
	if ($params[$count]){
		my(@node) = $opensrf_config->findnodes($query[$count]);
		foreach (@node) {
			$_->setData($params[$count]);
		}
	}
}

unless ($opensrf_config->toFile($config_file)){
	die "ERROR: There has been a problem updating the config file";
}


}

sub create_schema {

# Extracts the info from opensrf.xml and builds the db by calling build-db.sh

my @result;
$result[0] = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/host/text()";
$result[1] = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/port/text()";
$result[2] = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/db/text()";
$result[3] = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/user/text()";
$result[4] = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/pw/text()";

my $parser = XML::LibXML->new();
my $opensrf_config = $parser->parse_file($config_file);

system($build_db_sh." ".$opensrf_config->findnodes($result[0])." ".$opensrf_config->findnodes($result[1])." ".$opensrf_config->findnodes($result[2])." ".$opensrf_config->findnodes($result[3])." ".$opensrf_config->findnodes($result[4]));

}


while (scalar(@ARGV)) {

	my $arg = shift(@ARGV);

	if ($arg eq "--create-schema"){ $cschema = 1; }
	elsif ($arg eq "--update-config"){ $uconfig = 1; }
	elsif ($arg eq "--configfile"){ $config_file = shift(@ARGV); }
	elsif ($arg eq "--builddbfile"){ $build_db_sh = shift(@ARGV); }
	elsif ($arg eq "--user"){ $dbuser = shift(@ARGV); }
	elsif ($arg eq "--password"){ $dbpw = shift(@ARGV); }
	elsif ($arg eq "--dbname"){ $dbname = shift(@ARGV); }
	elsif ($arg eq "--host"){ $dbhost = shift(@ARGV); }
	elsif ($arg eq "--port"){ $dbport = shift(@ARGV); }
	else { die "unknown argument '$arg'"; }

}

if ($config_file eq '') { $config_file = "/openils/conf/opensrf.xml"; }
if ($build_db_sh eq '') { $build_db_sh = "../sql/Pg/build-db.sh"; }
unless (-e $build_db_sh) { die "Error: ".$build_db_sh." does not exist. \n"; }
unless (-e $config_file) { die "Error: ".$config_file." does not exist. \n"; }
if ($uconfig == 1) { &update_config($dbhost,$dbport,$dbname,$dbuser,$dbpw); }
if ($cschema == 1) { &create_schema; }

if (!$cschema == 1 && !$uconfig == 1){
	print "USAGE - eg_db_config.pl [--configfile /foo/bar/opensrf.xml] [--builddbfile /foo/bar/build-db.sh] [--create-schema] [--update-config --host dbhost --port dbport --dbname name --user dbuser --password dbpass] \n";
}
