#!/usr/bin/perl
# ftndbadm - Administration of a database for Fidonet/FTN related
# processing. The SQL database engine is one for which a DBD module
# exists, defaulting to SQLite.
#
# Copyright (c) 2010 Robert James Clay.  All Rights Reserved.
# This is free software;  you can redistribute it and/or
# modify it under the same terms as Perl itself.

use warnings;
use strict;
use Getopt::Std;
use vars qw/ $opt_t $opt_T $opt_D $opt_u $opt_p $opt_h $opt_v $opt_x /;

use FTN::Database qw(&open_ftndb &close_ftndb);
use FTN::Database::Nodelist qw(&create_nodelist_table &drop_nodelist_table &create_ftnnode_index &drop_ftnnode_index);
use FTN::Log qw(&logging);

our $VERSION = 0.3;

my ( $dbtype, $dbname, $dbuser, $dbpass, $tblname, $dbh, $sql_stmt );

my $Logfile = "stdout";

my $progid = "NLTBL";

#print @INC"\n";

getopts('t:T:D:u:p:hvx');

if ($opt_h) {
    print "\nUsage: ftndbadm.pl [-t nodelisttablename] [-T dbtype] [-D dbname] [-u dbuser] [-p dbpass] [-v] [-h]...\n";
    print "-t                nodelisttablename = defaults to \'Nodelist\'. \n";
    print "[-T dbtype]       database type;  defaults to 'SQLite'.\n\n";
    print "[-D dbname]       database name & path;  defaults to 'ftndbtst'.\n\n";
    print "[-u dbuser]       database user;  defaults to 'sysop'.\n\n";
    print "[-p dbpass]       database password;  defaults to 'ftntstpw'.\n\n";
    print "-v                verbose option \n\n";
    exit;
}

# note that "opt_x" is the debug variable
if ($opt_x) {
    logging($Logfile, $progid, "Debug flag is set");
}

# note that "opt_v" is the verbose variable
if ($opt_v) {
    logging($Logfile, $progid, "Verbose flag is set");
}

#    Database type
#    This needs to be a database type for which a DBD module exists,
#    the type being the name as used in the DBD module.  The default
#    type is SQLite.
if ($opt_T) {
    $dbtype = $opt_T;
    undef $opt_T;
}
else {
    $dbtype = "SQLite";    # default database type is SQLite
    undef $opt_T;
}
if ($opt_v) { logging($Logfile, $progid, "Database type being used is $dbtype.") };

#    Database name
if ($opt_D) {
    $dbname = $opt_D;    # this needs to be at least the filename & can also include a path
    undef $opt_D;
}
else {
    $dbname = "ftndbtst";    # default database file is in current dir
}
#    Database user
if ($opt_u) {
    $dbuser = $opt_u;    # Set database user
    undef $opt_u;
}
else {
    $dbuser = "sysop";    # default user is sysop
}
#    Database password
if ($opt_p) {
    $dbpass = $opt_p;    # Set database password
    undef $opt_p;
}
else {
    $dbpass = "ftntstpw";    # default database password
}

#  nodelist table name
if ($opt_t) {
    if ( $opt_t =~ /\./ ) {    # period in proposed table name?
        logging($Logfile, $progid, "sqlite does not allow periods in table names.");
        $opt_t =~ tr/\./_/;    # change period to underscore
        $tblname = $opt_t;     #
        logging($Logfile, $progid, "Changed table name to $tblname.");
    }
    else {                     # no period in name
        $tblname = $opt_t;     #  just assign to variable
    }

}
else {
    $tblname = "Nodelist";     # default table name
}

# connect to database
$dbh = FTN::Database::open_ftndb($dbtype, $dbname, $dbuser, $dbpass);

# drop the old nodelist table index, if it exists.
FTN::Database::Nodelist::drop_ftnnode_index($dbh);

# drop the old nodelist table, if it exists.
logging($Logfile, $progid, "Dropping existing nodelist table $tblname if it already exists.");
FTN::Database::Nodelist::drop_nodelist_table($dbh, $tblname);

# Create nodelist table
FTN::Database::Nodelist::create_nodelist_table($dbh, $tblname);

# Creating Index
FTN::Database::Nodelist::create_ftnnode_index($dbh, $tblname);

# disconnect from database
FTN::Database::close_ftndb($dbh);

logging($Logfile, $progid, "Table $tblname created.");

exit();

