#!/usr/bin/perl

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

use FTN::Database qw(&open_ftndb &close_ftndb);

=head1 NAME

listftndb - List information from an Fidonet/FTN SQL database

=head1 VERSION

Version 0.10

=cut

our $VERSION = 0.10;

=head1 DESCRIPTION

List information from a Fidonet/FTN St. Louis Format Nodelist to a text file.
One such listing can be from a specific net from a specific zone of a nodelist table.

=head1 SYNOPSIS

C<listftndb [-t nodelisttablename] [-T db_type] [-D db_name] [-u db_user] [-p db_password] [-z zone] [-n net] [-o outfile] [-v] [-h]>

=cut

my ( $db_type, $db_name, $db_user, $db_password, $table_name, $db_handle, $sql_statement, $zone_number, $net_number, $list_file );

getopts('n:o:t:T:D:u:p:hvxz:');

if ($opt_h) {
    print "\nUsage: listftndb [-t nodelisttablename] [-T db_type] [-D db_name] [-u db_user] [-p db_password] [-z zone] [-n net] [-o outfile] [-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 "[-z zone_number]       = zone_number number = defaults to 1. \n";
    print "[-n net_number]        = Net number; defaults to 1. \n";
    print "[-o outfile]           = Output file (plus path) = defaults to outfile.txt in current directory.\n";
    print "[-v]                   = Verbose option \n";
    print "[-x]                   = Debug option \n\n";
    print "[-h]                   = Print this help message \n\n";
    exit;
}

=head1 OPTIONS

=over

=item -x

Debug option.

=cut

if ($opt_x) {
    print "Debug flag is set\n";
}

=item -v

Verbose option.

=cut

if ($opt_v) {
    print "Verbose flag is set\n";
}

=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";    # default database type is SQLite
    undef $opt_T;
}
if ($opt_v) { print "Database type being used is $db_type.\n" };


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

=cut

if ($opt_t) {
    if ( $opt_t =~ /\./ ) {    # period in proposed table name?
        print "sqlite does not allow periods in table names.";
        $opt_t=~ tr/\./_/;    # change period to underscore
        $table_name = $opt_t;     #
        print "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
}

=item -z

If set;  is the FTN Zone number, with 1 being the default.

=cut

if ($opt_z) {
    $zone_number = $opt_z;    # Set zone number 
    undef $opt_z;
}
else {
    $zone_number = 1;    # default zone number
}

=item -n

If set;  is the FTN Net number, with 1 being the default.

=cut

if ($opt_n) {
    $net_number = $opt_n;
    undef $opt_n;
}
else {
    $net_number = 1;
}

=item -o

Output file.
This needs to be at least the filename and can also include a path.
The default output file is outfile.txt in the current directory.

=back

=cut

if ($opt_o) {
    $list_file = $opt_o;    # 
    undef $opt_o;
}
else {
    $list_file = "outfile.txt"; 
}


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

# build Select query sql statement
$sql_statement = "SELECT * FROM $table_name WHERE zone = $zone_number and net = $net_number ";
$sql_statement .= "ORDER by node ASC";

# execute query
my $query_handle = $db_handle->prepare($sql_statement);
$query_handle->execute();

$query_handle->bind_columns(\my($id, $type, $zone, $net, $node, $point, $region, $name, $location, $sysop, $phone, $baud, $flags, $domain, $source, $updated));

local(*F);

open(F, ">$list_file") or die "Cannot open $list_file\n"; 

print F "Listing Zone $zone_number Net $net_number from FTN database:\n\n";

while($query_handle->fetch()) {
   print F "$zone:$net/$node, $name, $location, $sysop, $phone, $baud, $flags";
}

close(F);

# finish query
$query_handle->finish;
undef $query_handle;

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

exit();

=head1 EXAMPLE

Given that $DBFILE is an SQLite database file and that $LISTFILE is a
file where the information is to be listed, the following command line
can be used to list the nodes in Net 102 of Zone 1 in that file:

C<listftndb -D $DBFILE -z 1 -n 102 -o $LISTFILE -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<ftndbadm>,
 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

