#!/usr/bin/perl

use CGI;
use DBI;

$query=new CGI;
$address = $query->param('address') || 'dewey';
$username = $query->param('username') || 'bastille';
$password = $query->param('password') || '';
if ($password eq '') { $pwstring = ''; } else { $pwstring = '-p=$password' };

print $query->header;
print "<title>Database Engine Administration</title>\n";
print "<body>\n";
print "<h1>Database Engine Administration</h1>\n";

$drh=DBI->install_driver('mysql') || cgidie("Could not load mysql driver\n");

# Connect to the database engine.  Do to funky things I don't understand
# with Mysql, we need to connect to a database, not just the engine.
# I hope the database 'mysql' is installed everywhere...
$dbh=$drh->connect("mysql:$address","$username","$password") || cgidie("Failed to connect to mysql server\n");

# Find out what databases are available.
$cursor=$dbh->prepare("show databases") || cgidie("show databases failed: ".$cursor->errstr);
$cursor->execute;
while (@databases = $cursor->fetchrow) {
  push @list, $databases[0];
}

print <<EOF
Please select a database to work with:<br>
<form method=post action='use_database.pl'>
<input name=address value='$address' type=hidden>
<input name=username value='$username' type=hidden>
<input name=password value='$password' type=hidden>
<select size=6 name=database>
EOF
;
while ($database = shift @list) {
  print "<option value='$database'> $database\n";
}
print <<EOF
</select><BR>
<input type=submit value='Use Database'>
</form>
<hr>
<address>
Orignial Code by: <a href='mailto:tps\@users.buoy.com'>Tim Sailer</a><BR>
Modified by: <a href='mailto:hightide\@iname.com'>Sean Bastille</a>
</address>
</body>
</html>
EOF
;

sub cgidie {
  ($die_str) = @_;
  print <<EOF
CGI_Error:<p><pre>
$die_str
</pre>
</body>
</HTML>
EOF
;
  die $dir_str;
}
