#!/usr/bin/perl

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);

=head1 NAME

ftndbadm - Administration of an SQL database for Fidonet/FTN processing.

=head1 VERSION

Version 0.10

=cut

our $VERSION = 0.10;

=head1 DESCRIPTION

Administration of a database for Fidonet/FTN related processing. The SQL
database engine is one for which a DBD module exists, defaulting to SQLite.

=head1 SYNOPSIS

C<ftndbadm [-t nodelisttablename] [-T db_type] [-D db_name] [-u db_user] [-p db_password] [-v] [-h]>

=cut

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

my $log_file = "stdout";

my $log_id = "NLTBL";

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";
    print "[-x]		          = debug option \n\n";
    print "[-h]		          = display this help message \n\n";
    exit;
}

=head1 OPTIONS

=over

=item -x

Debug option.

=cut

if ($opt_x) {
    logging($log_file, $log_id, "Debug flag is set");
}

=item -v

Verbose option.

=cut

if ($opt_v) {
    logging($log_file, $log_id, "Verbose flag is set");
}

=item -T

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.

=cut

if ($opt_T) {
    $db_type = $opt_T;
    undef $opt_T;
}
else {
    $db_type = "SQLite";
    undef $opt_T;
}
if ($opt_v) { logging($log_file, $log_id, "Database type being used is $db_type.") };

=item -D

Database name.
For an SQLite databse; this needs to be at least the filename and can
also include a path, and defaults to the file name ftndbttst.

=cut

if ($opt_D) {
    $db_name = $opt_D; 
    undef $opt_D;
}
else {
    $db_name = "ftndbtst";
}

=item -u

Database user.
For an SQLite database, this defaults to an empty string as it does not need it.

=cut

if ($opt_u) {
    $db_user = $opt_u;
    undef $opt_u;
}
else {
    $db_user = "";
}

=item -p

Database password.
For an SQLite database, this defaults to an empty string as it does not need it.

=cut

if ($opt_p) {
    $db_password = $opt_p;
    undef $opt_p;
}
else {
    $db_password = ""; 
}

=item -t

The nodelist table name to be used.
Note that if there is a period in the name, that period will be changed
to an underscore.

=back

=cut

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();

=head1 EXAMPLE

Given that $DBFILE is a file like F</opt/ftndb/FTN-Database.db>, the following
command line can be used to create an FTN Nodelist table in that database file:

C<ftndbadm -T SQLite -D $DBFILE -t Nodelist -v>

=head1 AUTHOR

Robert James Clay, C<< <jame at rocasa.us> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-ftn-database at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FTN-Database>.  I will
be notified, and then you'll automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc listftndb


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FTN-Database>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/FTN-Database>

=item * Search CPAN

L<http://search.cpan.org/dist/FTN-Database>

=back

=head1 SEE ALSO

 L<FTN::Database>, L<FTN::Database::Nodelist>, L<listftndb>,
 L<ftndbadm>, and L<nl2ftndb>


=head1 COPYRIGHT & LICENSE

Copyright 2010 Robert James Clay, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

