#!/usr/bin/perl -w

use strict;
use warnings;

use Getopt::Long;
use Pod::Usage;
use Regexp::Common;
use Stanford::Schema::WebApps::SharedEmail;

my ($hostname, $port, $database, $username, $passfile, $help, $man);
my %Config;

# Get options, and handle requests for help
GetOptions (
    'host|h=s'     => \$hostname,
    'port|p:i'     => \$port,
    'database|d=s' => \$database,
    'username|u=s' => \$username,
    'password|f=s' => \$passfile,
    'help|?'       => \$help,
    'man|m'        => \$man,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-verbose => 2) if $man;

# Validate hostname
if (!defined($hostname)) {
    pod2usage('No hostname specified');
}
unless (   ($hostname =~ m/$RE{net}{domain}{-nospace}/i)
        || ($hostname =~ m/$RE{net}{IPv4}/i)
        || ($hostname =~ m/$RE{net}{IPv6}/i)
) {
    die "Host $hostname is invalid.\n";
}
$Config{'shared_email_db_host'} = $hostname;

# Validate port
if (!defined($port)) {
    $port = '';
} else {
    if ($port > 65534) {
        die "Port number $port is too high.\n";
    }
    if ($port < 2) {
        die "Port number $port is too low.\n";
    }
    $Config{'shared_email_db_port'} = $port;
}

# Validate database
if (!defined($database)) {
    pod2usage('No database specified');
}
unless ($database =~ m/^[a-z0-9_-]+$/i) {
    die "Database $database is an invalid name.";
}
$Config{'shared_email_db_name'} = $database;

# Check for a username
if (!defined($username)) {
    pod2usage('No username specified');
}
$Config{'shared_email_db_user'} = $username;

# Read in the password
if (!defined($passfile)) {
    pod2usage('No password file specified');
}
#unless (-r $passfile) {
#    die "Unable to read $passfile\n";
#}
$Config{'shared_email_db_passwd_file'} = $passfile;

# Make the DSN, and deploy the database
print "Connecting to database\n";
my $schema = Stanford::Schema::WebApps::SharedEmail->connect(\%Config);
print "Deploying schema\n";
$schema->deploy;
exit 0;


=pod

=head1 NAME

sharedemail-db - Create the database for the Shared Email web app

=head1 SYNOPSIS

sharedemail-db -h mysql-server -u sharedemail1 -d sharedemail
               -f /tmp/sharedemail1_password

=head1 OPTIONS

=over 4

=item B<--host> B<-h>

The name (or IP address) of the database host.  If a hostname is not provided,
I<localhost> and a local UNIX socket will be assumed.

=item B<--port> B<-p>

The (optional) port for the database host.  Defaults to I<3306>.

=item B<--username> B<-u>

The username to use when connecting to the database host.

=item B<--password> B<-f>

The path to a file which contains the password to use when connecting to the
database host.  The file must be readable; only the first line will be read.

=item B<--database> B<-D>

The name of the database to use.  This program assumes an empty database.

=item B<--help> B<-?>

Print the SYNOPSIS and OPTIONS sections.

=item B<--man> B<-m>

Print the entire manual.

=back

=head1 DESCRIPTION

This program creates all of the tables and relationships necessary for the
Shared Email web application.  The program must be given an empty database,
and it must also be given an account that has full permissions on that
database (including CREATE TABLE, etc.).

=head1 AUTHOR

A. Karl Kornel - akkornel@stanford.edu
