#!/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.09;

my ( $db_type, $db_name, $db_user, $db_password, $table_name, $db_handle );

my $log_file = "stdout";

my $log_id = "NLTBL";

#print @INC"\n";

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

if ($opt_h) {
    print "\nUsage: ftndbadm [-t nodelisttablename] [-T db_type] [-D db_name] [-u db_user] [-p db_password] [-v] [-h]...\n";
    print "-t                nodelisttablename = defaults to \'Nodelist\'. \n";
    print "[-T db_type]       database type;  defaults to 'SQLite'.\n\n";
    print "[-D db_name]       database name & path;  defaults to 'ftndbtst'.\n\n";
    print "[-u db_user]       database user;  defaults to an empty string.\n\n";
    print "[-p db_password]       database password;  defaults to an empty string.\n\n";
    print "-v                verbose option \n\n";
    exit;
}

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

# note that "opt_v" is the verbose variable
if ($opt_v) {
    logging($log_file, $log_id, "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) {
    $db_type = $opt_T;
    undef $opt_T;
}
else {
    $db_type = "SQLite";    # default database type is SQLite
    undef $opt_T;
}
if ($opt_v) { logging($log_file, $log_id, "Database type being used is $db_type.") };

#    Database name
if ($opt_D) {
    $db_name = $opt_D;    # this needs to be at least the filename & can also include a path
    undef $opt_D;
}
else {
    $db_name = "ftndbtst";    # default database file is in current dir
}
#    Database user
if ($opt_u) {
    $db_user = $opt_u;    # Set database user
    undef $opt_u;
}
else {
    $db_user = "";    # default user is an empty string, as default SQLite does not need it.
}
#    Database password
if ($opt_p) {
    $db_password = $opt_p;    # Set database passsward
    undef $opt_p;
}
else {
    $db_password = "";    # default database password is an empty string, as default SQLite does not need it
}

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

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

# connect to database
$db_handle = FTN::Database::open_ftndb($db_type, $db_name, $db_user, $db_password);

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

# drop the old nodelist table, if it exists.
logging($log_file, $log_id, "Dropping existing nodelist table $table_name if it already exists.");
FTN::Database::Nodelist::drop_nodelist_table($db_handle, $table_name);

# Create nodelist table
FTN::Database::Nodelist::create_nodelist_table($db_handle, $table_name);

# Creating Index
FTN::Database::Nodelist::create_ftnnode_index($db_handle, $table_name);

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

logging($log_file, $log_id, "Table $table_name created.");

exit();

