#!/usr/bin/perl 
## destroy -- drop all the tables of a message database
## Copyright (c) 2000 Christopher League; see LICENSE for details

## I require these packages:
##  - DBI-1.11 (Generic database interface)
use DBI;
use Getopt::Long;
use strict;

## Descriptive text
my $version = "0.1";

my $version_text =
"destroy (Meba) $version
Copyright (c) 2000 Christopher League
Meba comes with ABSOLUTELY NO WARRANTY.
You may redistribute and/or modify Meba under the terms of the 
GNU General Public License;  see the file LICENSE for details.
";

my $usage_text = 
"Usage: destroy [OPTIONS] CONF-FILE
Drop all the tables of a message database.
Requires the name of a configuration file created by the `setup' script.

  -n, --dry-run        output the queries, but do not execute them
  -v, --verbose        describe what is happening in gross detail
      --help           display this help and exit
      --version        output version information and exit

Report bugs to meba-bugs\@contrapunctus.net.
";

## Options from configuration file or command line.
my %options;

## Command-line specs are very simple here.
my @cmd_line_specs = 
    (\%options, 
     "dry-run|n", "verbose|v",
     "help", "version" );

## Parse the command line into `options'.
my $getopt_result = GetOptions (@cmd_line_specs);

## If the user wanted the usage or version blurbs, then we can 
## output them and stop now.
print STDERR $usage_text and exit 1
    if $options{help};

print STDERR $version_text and exit 2
    if $options{version};

## If there was an error in the command line options, we can
## also stop now.
unless ($getopt_result) {
    print STDERR "Try `$0 --help' for more information.\n";
    exit 3;
}
unless ($ARGV[0]) {
    print STDERR "You must give the name of a configuration file.\n";
    print STDERR "Try `$0 --help' for more information.\n";
    exit 4;
}

## Attempt to parse the config file
my $conf_file = $ARGV[0];
open (CONF, $conf_file) or die "Could not open $ARGV[0]";

print "Reading $conf_file...\n" if $options{verbose};
while(<CONF>) {                # read each line
    chop;
    if (/\s*\#.*/) { }         # ignore comments
    elsif (/\s*([\w\.]+)\s*\=\s*(.*)/) { # varname = value
        $options{$1} = $2;
    }
    elsif (/\s*/) { }           # ignore blank lines
    else {
        print STDERR "$conf_file:$.: bad configuration command\n";
        exit 5;
    }
}
close CONF;

## Determine arguments to connect()
my $data_source = "DBI:mysql:$options{database}";
if ($options{host}) {
    $data_source .= ":$options{host}";
    $data_source .= ":$options{port}" if $options{port};
}
print "Connect ($data_source, $options{user}, $options{password})\n"
    if $options{verbose};

## Go ahead and connect, unless this is a dry run
my $db;
unless ($options{"dry-run"}) {
    $db = DBI->connect ($data_source, $options{user}, $options{password})
        || exit 6;
}

## Tables to be dropped
my @tables = qw(message address reference tag);

print "I am about to irrevocably destroy the following tables:\n";
map { print "  $options{prefix}$_\n" } @tables;

unless ($options{"dry-run"}) {
    print "\"You must be VERY sure...\"\n";
    print "Proceed [n]: ";
    my $r;
    chop($r = <STDIN>);

    unless (substr($r,0,1) eq "y") {
        print "Nothing destroyed.\n";
        exit 0;
    }
}

sub drop {
    my ($t) = @_;
    my $q = "DROP TABLE $options{prefix}$t\n";
    print $q if $options{"dry-run"} or $options{verbose};
    unless ($options{"dry-run"}) {
        my $s = $db->prepare($q);
        $s->execute or exit 8;
    }
}

map &drop($_), @tables;

if ($options{"dry-run"}) {
    print "(worry not, that was a dry run...)\n";
}

## All done -- disconnect and exit
unless ($options{"dry-run"}) {
    $db->disconnect || die "Error disconnecting: " . $db->errstr;
}
exit 0;

