head	1.1;
access;
symbols
	TIGRIS_1_1_0RC2:1.1
	TIGRIS_1_1_0RC1:1.1
	TIGRIS_1_1:1.1.0.4
	TIGRIS_1_0_8:1.1
	TIGRIS_1_0_8RC3:1.1
	TIGRIS_1_0_8RC2:1.1
	TIGRIS_1_0_8RC1:1.1
	TIGRIS_1_0_7:1.1
	TIGRIS_1_0_7RC3:1.1
	TIGRIS_1_0_7RC2:1.1
	TIGRIS_1_0_7RC1:1.1
	TIGRIS_1_0_6:1.1
	TIGRIS_1_0_6RC5:1.1
	TIGRIS_1_0_6RC4:1.1
	TIGRIS_1_0_6RC3:1.1
	TIGRIS_1_0_6RC2:1.1
	TIGRIS_1_0_6RC1:1.1
	TIGRIS_1_0_5:1.1
	TIGRIS_1_0_5RC6:1.1
	TIGRIS_1_0_5RC5:1.1
	TIGRIS_1_0_5RC4:1.1
	TIGRIS_1_0_5RC3:1.1
	TIGRIS_1_0_5RC2:1.1
	TIGRIS_1_0_5RC1:1.1
	TIGRIS_1_0_4:1.1
	TIGRIS_1_0_3:1.1
	TIGRIS_1_0_2:1.1
	TIGRIS_1_0_1:1.1
	TIGRIS_1_0:1.1.0.6
	TIGRIS_1_0_0:1.1
	TIGRIS_1_0_0_RC1:1.1.0.2;
locks; strict;
comment	@# @;


1.1
date	2001.03.27.18.24.21;	author edk;	state Exp;
branches;
next	;


desc
@@


1.1
log
@a useful script for old sites, this is designed to provide a means to
make the filesystem match the db.  a bit more work would make it produce
fewer error messages (the messages are mostly harmless, but could disguise
a real error condition).
@
text
@#!/usr/bin/perl

use Getopt::Long;

main();

sub main {
    setupEnvironment();
    my $options = getOptions();
    $main::dbh = getDatabaseConnection();
    my ($select, $ret);

    # currently, $anzu_command_info is a command, and eyebrowse_command_info
    # is a list of arrays, one for each mailing list to be created.
    my ($anzu_command_info, $eyebrowse_command_info) =
        constructCommandInfo($dbh);
    renderAnzuCommandInfo($options, $anzu_command_info);
    renderEyebrowseCommandInfo($options, $eyebrowse_command_info);
}

#################################
### handle any required actions
#################################

sub renderAnzuCommandInfo {
    my ($options, $anzu_command_info) = @@_;
    my $command_base = $anzu_command_info->{command_base};
    foreach my $command_ref (@@{$anzu_command_info->{commands}}) {
        print (join(" ", @@$command_base), " '", join("' '", @@$command_ref), "'\n");
    }
}

sub renderEyebrowseCommandInfo {
    my ($options, $eyebrowse_lists) = @@_;

    my $eyebrowse_dir ="/usr/local/tigris/data/eyebrowse";
    print "mkdir $eyebrowse_dir\n";
    print "chmod $eyebrowse_dir 0755\n";

    my ($existing_lists) = getEyebrowseLists();

    foreach $item (@@$eyebrowse_lists) {
        my ($list_address, $listID, $desc, $list_file_dir) = @@$item;
        my $list_file = "$list_file_dir/0mbox";
        my @@eyebrowse_command_base = getEyebrowseCommandBase();
        if (grep /^$list_address$/, @@$existing_lists) {
            print STDERR "removing $list_address since it was set up\n";
            print join (" ", @@eyebrowse_command_base, '--command','deletelist',
                    '--listname', $list_address, "\n");
        }
        print STDERR "adding list $list_address to eyebrowse database\n";
        print join (" ", @@eyebrowse_command_base, '--command', 'addlist',
                '--listname', $list_address, '--descr', $desc, '--listid',
                $listID, "\n");
        print STDERR "adding list file to eyebrowse database\n";
    
        print join(" ", 'mkdir', '-p', $list_file_dir, "\n");
        print join(" ", 'touch', $list_file, "\n");
        print join(" ", @@eyebrowse_command_base, '--command', 'addlistfile',
                '--listname', $list_address, '--listfile', $list_file, "\n");
    }

    print STDERR "Setting ownership of the eyebrowse data directory\n";
    print join(" ", "chown","-R", "tigrisq.tigris", $eyebrowse_dir, "\n");
}

#################################
### process information according to business logic
#################################

sub constructCommandInfo {
    my ($dbh) = @@_;
    my $java = findJava();
   
    my @@anzu_command_base = (
            $java,
            "-Dorg.tigris.anzu.secret=/usr/local/tigris/data/anzu/secret",
            "-Dorg.tigris.anzu.RMIURL=rmi://localhost:1099/".
                "org.tigris.anzu.mail",
            "org.tigris.anzu.client.Harness"
    );
    my $anzu_commands = [];
    my @@domains = ();
    $select = getDomains($main::dbh);
    while (my $domainRecord = $select->fetchrow_hashref) {
        my $domain = $domainRecord->{domain};
        next if ($domain =~ m/\*/);
        # if there's stuff there, clean it out
        push(@@$anzu_commands, ['add_domain', $domain]);
        push @@domains, $domain;
    }

    $select = getProjects($main::dbh);
    while (my $projectRecord = $select->fetchrow_hashref) {
        my $project = $projectRecord->{project};
        my $domain  = $projectRecord->{domain};
        next if ($project=~ m/\*/);
        push(@@$anzu_commands, ['add_project', $domain, $project]);
        my $projectID = $projectRecord->{projectID};
        my $subselect = getProjectOwners($main::dbh, $projectID);
        my $ownerAddress = '';
        while (my $ownerRecord = $subselect->fetchrow_hashref) {
            my $ownerLoginID = $ownerRecord->{loginID};
            $ownerAddress .= "\n" if $ownerAddress;
            $ownerAddress .= "$ownerLoginID\@@$domain";
        }
        push(@@$anzu_commands, ['alias', $domain, $project, 'create',
                'owner', $ownerAddress]);
    }

    $select = getUsers($main::dbh);
    while(my $userRecord = $select->fetchrow_hashref) {
        my $user = $userRecord->{loginID};
        my $email = $userRecord->{email};
        my $domain = $userRecord->{domain};
        next if ($user =~m/\*/); # TODO : better weeding ?
        if ($domain =~ m/\*/) {  # TODO : better handling of wildcards ?
            foreach my $domain (@@domains) {
                push (@@$anzu_commands,
                        ['alias', $domain, 'www', 'create', $user, $email]);
            }
            next;
        }
        push(@@$anzu_commands, 
                ['alias', $domain, 'www', 'create', $user, $email]);
    }

    $select = getMailingLists($main::dbh);
    while (my $mlRecord = $select->fetchrow_hashref) {
        my $listName = $mlRecord->{LIST_NAME};
        my $type     = $mlRecord->{TYPE};
        my $domain   = $mlRecord->{DOMAIN_NAME};
        my $project  = $mlRecord->{PROJECT_NAME};
        my $listID   = $mlRecord->{MAILING_LIST_ID};
        my $desc     = $mlRecord->{DESCRIPTION};
        push(@@$anzu_commands, 
                ['ml',$domain,$project,'create',$listName,$type]);
        if ('' ne '') {
            push (@@$anzu_commands, ['ml', $domain, $project, 'update_string',
                    $listName, 'mail_prefix', '']);
        }
        my $archiver_address = "archive=mail-". lc($project) . "=".
            lc($listName) . "=" . lc($domain)."\@@otncast.otnxchange.oracle.com";
        my $list_address     = "$listName\@@$project.$domain";
        my $list_file_dir    =
            "/usr/local/tigris/data/eyebrowse/$domain/$project/$listName";

        push(@@$anzu_commands, 
                ['ml', $domain, $project, 'subscribe', $listName, '',
                $archiver_address, '']);
        push @@eyebrowse_lists, [$list_address,$listID,$desc,$list_file_dir];
    }
    my $anzu_command_info = {
        "command_base" => \@@anzu_command_base,
        "commands" => $anzu_commands,
    };
    return ($anzu_command_info, \@@eyebrowse_lists);
}

#################################
###  preparation (getting information, db connections, etc.)
#################################
sub findJava {
    if (-d "/usr/java/jdk1.3" && -x "/usr/java/jdk1.3/bin/java") {
        $ENV{JAVA_HOME}='/usr/java/jdk1.3';
        return '/usr/java/jdk1.3/bin/java';
    }
    my $java = `which java 2> /dev/null`;
    if ( ! -x "$java" ) {
        if ($ENV{JAVA_HOME}) {
           $java = "$ENV{JAVA_HOME}/bin/java";
        }
    }

    if ( ! -x $java )  {
        print STDERR "Please add the bin dir from your JDK to your path or ",
                "set JAVA_HOME\n";
        exit 1;
    }
    return $java;
}

sub getDomains {
    my ($dbh) = @@_;
    my $select  = $dbh->prepare(
            "SELECT HELM_DOMAIN.NAME as domain ".
            "FROM HELM_DOMAIN");
    my $ret  = $select->execute();
    warn "error selecting domains" if not $ret;
    return $select;
}

sub getProjects {
    my ($dbh) = @@_;
    my $select  = $dbh->prepare(
            "SELECT HELM_PROJECT.NAME as project, HELM_DOMAIN.NAME as domain, ".
                "HELM_PROJECT.PROJECT_ID as projectID ".
            "FROM HELM_PROJECT, HELM_DOMAIN ".
            "WHERE HELM_PROJECT.DOMAIN_ID = HELM_DOMAIN.DOMAIN_ID AND " .
                "HELM_PROJECT.TYPE!='host' AND HELM_PROJECT.TYPE != 'domain'");
    my $ret  = $select->execute();
    warn "error selecting projects" if not $ret;
    return $select;
}

sub getProjectOwners {
    my ($dbh, $projectID) = @@_;
    my $select = $dbh->prepare(
            "SELECT u.loginID ".
            "FROM User u, HELM_USER_ROLE_PROJECT urp, Role r ".
            "WHERE u.userID = urp.USER_ID and urp.PROJECT_ID = $projectID and ".
                "urp.ROLE_ID = r.roleID and r.name=\"Project Owner\"");
    my $ret    = $select->execute();
    warn "error selecting project owners" if not $ret;
    return $select;
}

sub getUsers {
    my ($dbh) = @@_;
    my $select = $dbh->prepare(
            "SELECT loginID, email, HELM_DOMAIN.NAME as domain ".
            "FROM User, HELM_DOMAIN ".
            "WHERE User.domainID = HELM_DOMAIN.DOMAIN_ID");
    my $ret = $select->execute();
    warn "error selecting users" if not $ret;
    return $select;
}

sub getMailingLists {
    my ($dbh) = @@_;
    my $select = $dbh->prepare(
        "SELECT LIST_NAME, ml.TYPE, Project.NAME as PROJECT_NAME, " .
            "Domain.NAME as DOMAIN_NAME, MAILING_LIST_ID, ml.DESCRIPTION ".
        "FROM HELM_MAILINGLIST ml, HELM_PROJECT Project, HELM_DOMAIN Domain ".
        "WHERE ml.PROJECT_ID = Project.PROJECT_ID AND ".
            "Project.DOMAIN_ID = Domain.DOMAIN_ID");
    my $ret = $select->execute();
    warn "error selecting mailing lists" if not $ret;
    return $select;
}

use DBI;  
END { $main::dbh->disconnect if $main::dbh }

sub getDatabaseConnection {
    my $db_base = 'mysql';
    my $db_name = 'tigris';
    my $db_host = 'localhost';
    my $db_port = '3306';
    my $db_user = 'tigris';
    my $db_pass = 'euphrates';
    my $connectstring = "dbi:$db_base:$db_name:host=$db_host:port=$db_port";
    my $dbh = DBI->connect($connectstring, $db_user, $db_pass)
        or die "Can't connect to the table '$connectstring'.\n";
    return $dbh;
}

sub getOptions {
    my %options;
    if (!GetOptions(\%options, "type=s", "clean", "help") 
            or $options{help}) {
        showUsage();
    }
    $options{type}    || ($options{type}    = "all");
    die "invalid type $options{type}\n" 
        if ($options{type} ne "anzu" and $options{type} ne "eyebrowse"
                and $options{type} ne "all");

    return \%options;
}

sub showUsage {
    my ($message) = @@_;
    print <<EOM;
Usage: anzu_initialize [--type={type}] [--clean]
        type       One of 'all', 'eyebrowse', 'anzu'
        clean      A flag which indicates the data should not be deleted
EOM
    exit;
}

sub setupEnvironment {
    my $anzu_jar = "/usr/local/tigris/site/lib/anzu.jar";
    $ENV{CLASSPATH} = "$ENV{CLASSPATH}:$anzu_jar";
}

sub getEyebrowseLists {
    my @@eyebrowse_command_base = getEyebrowseCommandBase();
    open (IN, "-|") or exec @@eyebrowse_command_base, '--command','getlists';
    while (<IN>) {
        chomp;
        push @@existing_lists, $_;
    }
    close(IN);
    return \@@existing_lists;
}
    
sub getEyebrowseCommandBase {
    return  ('/usr/local/tigris/site/scripts/eyebrowse/eyebrowse');
}

@
