#!/usr/bin/perl
#############################################################################
#                                                                           #
# This copyright data applies to all source files                           #
#                                                                           #
# WebShell, Copyright (c) 2001 Steven Wishart, All Rights Reserved.         #
#                                                                           #
#      Module  : dbsetup                                                    #
#        Type  : Perl Source file                                           #
# Description  : Script to set up Database for the WebShell application     #
#                                                                           #
# This software is provided `AS IS' and without any express or implied      #
# warranties, including, without limitation, the implied waranties of       #
# merchantabulity and fitness for a particular purpose.                     #
#                                                                           #
# Redistribution and use of source are permitted provided that:             #
#                                                                           #
# (1) source distributions may not be sold for profit on physical media     #
# such as disks, tapes, and CD-ROMS, without expressed written permission   #
# from the author.                                                          #
#                                                                           #
#############################################################################
# -----------> Start of Config Options
$table = "users";
$database = "webshell";
$user = "webshell";
$passwd = "webshell";
$rootuser = "root";
$rootpasswd = "";
$config_db = "mysql";
$sql_interpreter = "/usr/bin/mysql";
$sql_admin = "/usr/bin/mysqladmin";
# -----------> End of Config Options

print "\n\n\nWebShell, Copyright (c) 2002 Steven Wishart, All Rights Reserved\n\n";

print "What is the root password for MySQL? ";
system("stty -echo");
$ans = <>; chomp $ans;
print "\n";
$rootpasswd = $ans;
system("stty echo");

while( ! -x $sql_interpreter )
{
  print "Where is the mysql interpreter located? ";
  $ans = <$sql_interpreter>; chomp $ans;
  $sql_interpreter = $ans;
}

while( ! -x $sql_admin )
{
  print "Where is mysqladmin located? ";
  $ans = <$sql_admin>; chomp $ans;
  $sql_admin = $ans;
}


# Create a user for the cgi probrams to connect as
open(SQL, "|$sql_interpreter -u $rootuser -p$rootpasswd $config_db");

# first delete the user if it exists
print SQL "delete from user where user='$user';";
print SQL "delete from db where user='$user';";

print SQL "INSERT into user (Host, User, password, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv)";

print SQL "VALUES ('localhost','$user', password('$passwd'),'Y','Y','Y','Y','Y','Y','Y');";

print SQL "INSERT into db (Host, Db, User, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv)";

print SQL "VALUES ('localhost','$database','$user','Y','Y','Y','Y','Y','Y');";
close SQL;

system("$sql_admin -u $rootuser -p$rootpasswd reload");

# create the database
open(SQL, "|$sql_interpreter -u $rootuser -p$rootpasswd");

print SQL "create database $database;";

close SQL;

# create the table needed to store the information
open(SQL, "|$sql_interpreter -u $rootuser -p$rootpasswd $database");


print SQL "create table $table (";
print SQL "uid INT NOT NULL PRIMARY KEY AUTO_INCREMENT,";   # 0
print SQL "firstname varchar(20),";                         # 1
print SQL "lastname varchar(20),";                          # 2
print SQL "userid varchar(30),";                            # 3
print SQL "phone varchar(30),";                             # 4
print SQL "email varchar(50),";                             # 5
print SQL "datecreated date,";                              # 6
print SQL "password varchar(16),";                          # 7
print SQL "level INT,";                                     # 8
print SQL "homephone varchar(30),";                         # 9
print SQL "domain varchar(60),"; 	                    # 10
print SQL "firstlogin date,"; 	                            # 11
print SQL "lastlogin date,";                                # 12
print SQL "logins INT,";                                    # 13
print SQL "city varchar(30),";                              # 14
print SQL "country varchar(30),";                           # 15
print SQL "ipaddress varchar(30),";                         # 16
print SQL "homedir varchar(100),";                          # 17
print SQL "community varchar(100)";                         # 18


print SQL ");";


close SQL;

print "\n\nCreated '$database' database. Use 'mysql $database -p' to connect to database.\n\n";

print "Database Creation complete.\n\n";
