#!/usr/bin/perl

use strict;
use DBI;


my $OPALS_CONF_DIR  = '/etc/opals/conf';

my ($code, $config, $dbh, $sth_dir, $sth_update, $dir, $path);

END {
    if ($dbh) {
        $dbh->disconnect();
    }
}


my @values;
open SITECODE, "ls $OPALS_CONF_DIR | ";
while (<SITECODE>) {
    $code = $_;
    chomp $code;
    $config = loadConfig("$OPALS_CONF_DIR/$code");

    $dbh = makeConnection($config);
    print "add table opl_delUser to  $code ....";
    
    
# change homeroom field in opl_user to varchar(100)

    my $sth=$dbh->prepare("create table opl_delUser like opl_user");
        $sth->execute();



    $sth->finish;
    print " done.\n";

    if ($dbh) {
        $dbh->disconnect();
    }
}
close SITECODE;


exit 0;
################################################################################


sub makeConnection {
    my ($config) = @_;
    if (!$config) {
        return;
    }
    my ($db_driver, $db_name, $db_host, $db_port, $db_user, $db_password);

    $db_driver   = $config->{'db_driver'} || 'mysql';
    $db_name     = $config->{'db_name'};
    $db_host     = $config->{'db_host'};
    $db_port     = $config->{'db_port'}   || '3306';
    $db_user     = $config->{'db_user'};
    $db_password = $config->{'db_password'};

    my $dsn = "dbi:$db_driver:$db_name:$db_host:$db_port";

    return DBI->connect($dsn, $db_user, $db_password);
}
############################################################


sub loadConfig {
    my ($configFile) = @_;
#    print "Enter the config filename of Opals: ";
#    $configFile = <STDIN>;
    my $config = {};

    open CONF, $configFile || die "Cannot open file $configFile";
    while (<CONF>) {
        chomp;
        s/#.*//;                # remove comments
        next if /^\s*$/;        # ignore blank lines

        if (/^\s*(\w+)\s*=\s*(.*?)\s*$/) {
            $config->{$1} = $2;
        }
    }
    close CONF;
    
    return $config;
}
############################################################
