#!/usr/bin/perl -w

# MySQL database upgrade script
# Copyright 2000 Jrme Marant <jerome@debian.org>
#

use Getopt::Std;
use DBI;

getopt('a:d:h:p:o:');

$adminpass = $opt_a;
$database = $opt_d;
$host = $opt_h;
$port = $opt_p;
$options = $opt_o;

my $dsn = "DBI:mysql:database=$database";

if ($host eq "") {
    $host = 'localhost';
}

# Set the hostname
if ($host ne 'localhost') {
    $dsn .= ";host=$host";

    # Set the port in case of a TCP connection.
    if ($port eq '') {
	$port = "3306";
    }

    $dsn .= ";port=$port";
}

if ($options ne "") {
    $dsn .= ";$options";
}

# Connect to mysql
my $dbh = DBI->connect($dsn, "root", "$adminpass",
			{"RaiseError" => 1});

# Alter the database
eval {$dbh->do("ALTER TABLE user_table CHANGE password_user password_user varchar (40)")};

eval {$dbh->do("ALTER TABLE subscriber_table ADD comment_subscriber varchar (150)")};

eval {$dbh->do("ALTER TABLE subscriber_table ADD INDEX (user_subscriber,list_subscriber)")};

$dbh->disconnect();

sub usage {
    die "Usage: upgrade-mysql-db.pl -a <adminpass> -d <database> [-h <hostname>] [-p <port>]\n";
}
