#!/usr/bin/perl

use warnings;
use strict;
use Getopt::Std;
use vars qw/ $opt_t $opt_T $opt_D $opt_u $opt_p $opt_l $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);

$Getopt::Std::STANDARD_HELP_VERSION = 1;

=head1 NAME

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

=head1 VERSION

Version 0.13

=cut

our $VERSION = 0.13;

=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 tablename] [-T db_type] [-D db_name] [-u db_user] [-p db_password] [-l log_file] [-v]>

C<ftndbadm [-h | --help]>

=cut

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

my $log_id = "DBADM";

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

if ($opt_h) {
    HELP_MESSAGE(*STDOUT);
    exit(0);
}

=head1 OPTIONS

=over

=item -l

This would be the filename and path of a log file, with the default
being ftnpldb.log in the current directory.

=cut

if ($opt_l) {
    $log_file = $opt_l;
    undef $opt_l;
}
else {
    $log_file = "ftnpldb.log";
}

=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
}

#  Create an FTN nodelist table
create_ftn_nodelist_table();

exit();

########################################################################
# subroutines
########################################################################
# Display Help message
########################################################################
sub HELP_MESSAGE {

    my $fh = shift;
    
    print $fh "\n\tUsage:\nftndbadm [-t tablename] [-T db_type] [-D db_name] [-u db_user] [-p db_password] [-l log_file] [-v] [-h | --help] [--version]\n";
    print $fh "\t[-t tablename]\t\t= Nodelist table name;  defaults to \'Nodelist\'. \n";
    print $fh "\t[-T db_type]\t\t= Database type;  defaults to 'SQLite'.\n";
    print $fh "\t[-D db_name]\t\t= Database name & path;  defaults to 'ftndbtst'.\n";
    print $fh "\t[-u db_user]\t\t= Database user;  defaults to an empty string.\n";
    print $fh "\t[-p db_password]\t= Database password;  defaults to an empty string.\n";
    print $fh "\t[-l log_file]\t\t= Log filename; defaults to ftnpldb.log in current dir.\n";
    print $fh "\t[-v]\t\t\t= Verbose option.\n";
    print $fh "\t[-x]\t\t\t= Debug option.\n";
    print $fh "\t[-h | --help]\t\t= Display this help message.\n";
    print $fh "\t[--version]\t\t= Display version message.\n\n";

    return();

}

########################################################################
# Create an FTN Nodelist table
########################################################################
sub create_ftn_nodelist_table {

    # 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.");

    return();
}

########################################################################
# Display Version message
########################################################################
sub VERSION_MESSAGE {

    my $fh = shift;
    
    print $fh "ftndbadm version $VERSION\n";

    return();

}

########################################################################

=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 ftndbadm


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

