#!/usr/bin/perl

use CGI;
use DBI;

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

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

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

# Connect to the database.
$dbh=$drh->connect("$database:$address","$username","$password") || cgidie("Failed to connect to mysql server\n");

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



print <<EOF
Please select a table to work with:<br>
<form method=post action='use_table.pl'>
<input name=address value='$address' type=hidden>
<input name=database value='$database' type=hidden>
<input name=username value='$username' type=hidden>
<input name=password value='$password' type=hidden>
<select size=6 name=table>
EOF
;
while ($table = shift @list) {
  print "<option value='$table'> $table\n";
}
print <<EOF
</select><BR>
<input type=submit value='Use Table'>
</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;
}
