#!/usr/bin/perl

print `clear`;
print ("************************************************************\n");
print ("** LibData OS Rel. 1.0 mySQL Database Loader - 10.06.2003 **\n");
print ("************************************************************\n");
print ("** WARNING!  Do NOT run this script if you have a current **\n");
print ("** LibData installation.  It will purge required libdata  **\n");
print ("** databases and libdata users from the mysql.user and    **\n");
print ("** mysql.db tables.                                       **\n");
print ("************************************************************\n");
print ("** NOTE: This script works only against mySQL 3.x.        **\n");
print ("** Since the number of columns in the mysql.user table    **\n");
print ("** and potential security issues differ between 3.x and   **\n");
print ("** 4.x, some modifications to the create.sql script are   **\n");
print ("** necessary.                                             **\n");
print ("************************************************************\n\n");
print ("This script performs the following installation steps:\n");
print ("(1) Prompt for a mySQL account (typically root) and password\n");
print ("    capable of creating/dropping databases, modifying the\n");
print ("    grants tables, and reloading/refreshing them.\n");
print ("(2) It will then confirm your decision and DESTROY any\n");
print ("    currently existing mySQL databases named libdata, libstats,\n");
print ("    or libsession.\n");
print ("(3) The libdata and libsession users in the mysql.user table\n");
print ("    will be purged, as well as references to these databases\n");
print ("    in the mysql.db table.\n");
print ("(4) New libdata, libstats, and libsession databases will be\n");
print ("    created.  They'll be populated with minimal data sets.\n");
print ("(5) New entries for libdata and libsession will be added with\n");
print ("    minimal required SQL rights to the mysql.user and mysql.db\n");
print ("    grants tables.\n\n");

print ("************************************************************\n");
print ("** Do NOT run this script if there is data in libdata,    **\n");
print ("** libstats, or libsession databases that you wish to     **\n");
print ("** keep!                                                  **\n");
print ("************************************************************\n\n"); 

# collect inputs


# This part commented out for now - not a good idea to offer this capability?
# print ("DNS name or IP of the mySQL host upon which to install lidata:  ");
# my $mysql_host;
# $mysql_host = <STDIN>;
# chomp($mysql_host);

print ("mySQL user name capable of adding/dropping databases:  ");
my $mysql_user;
$mysql_user = <STDIN>;
chomp($mysql_user);

print ("mySQL password associated with the specified user:  ");
use Term::ReadKey;
ReadMode('noecho');
$mysql_pwd = ReadLine(0);
chomp($mysql_pwd);
ReadMode('normal');

# confirm for delete, display settings
print ("\n\nYou entered:\n");
print ("mySQL server name: $mysql_host\n");
print ("mySQL user name: $mysql_user\n");

print ("Continue with install?  WARNING: This will DROP any databases \n");
print ("named libdata, libstats, and session on the specified mySQL\n");
print ("server.  Type YES (caps) if you wish to continue.\n\n");

print ("Load fresh LibData installation? ");
my $load_flag;
$load_flag = <STDIN>;
chomp($load_flag);

if ($load_flag eq "YES") {

	# dump existing libdata, libstats, session
	print ( "Dropping libdata, libstats, and session databases!\n" );
	`cat drop.sql | mysql -u$mysql_user -p$mysql_pwd`;

	# create new databases
	print ("Creating new libdata, libstats and libsessions databases.\n");
        `cat create.sql | mysql -u$mysql_user -p$mysql_pwd`;

	print ("Populating libdata tables & data.\n");
        `cat libdata.sql | mysql -u$mysql_user -p$mysql_pwd libdata`;

        print ("Populating libstats tables.\n");
        `cat libstats.sql | mysql -u$mysql_user -p$mysql_pwd libstats`;

        print ("Populating libsession tables.\n");
        `cat libsession.sql | mysql -u$mysql_user -p$mysql_pwd libsession`;

	# refresh/reload grants tables
	print("Reloading and refreshing the mySQL grants tables.\n");
	`mysqladmin -u$mysql_user -p$mysql_pwd refresh`;
        `mysqladmin -u$mysql_user -p$mysql_pwd reload`;

	# The "done" message
	print ("\nInstallation steps finished.  Script terminated.\n");

}

else {

print ("Installation cancelled.  No changes made.\n\n");

}

