head	1.21;
access;
symbols
	dlr:1.1.1
	TIGRIS_0_9_2_4:1.18
	TIGRIS_0_9_2_3:1.18
	HELM_PEER_PORT_BRANCH:1.1.1.1
	TURBINE_PEER:1.18
	TIGRIS_0_9_2_1:1.18
	TIGRIS_0_9_2:1.18.0.2
	TIGRIS_0_9_0:1.15.0.2
	TIGRIS_0_8_4:1.5.0.2;
locks; strict;
comment	@# @;


1.21
date	2001.02.22.04.48.45;	author edk;	state dead;
branches;
next	1.20;

1.20
date	2001.02.21.01.08.28;	author dlr;	state Exp;
branches;
next	1.19;

1.19
date	2001.02.05.22.51.23;	author ms;	state Exp;
branches;
next	1.18;

1.18
date	2001.01.11.00.54.53;	author edk;	state Exp;
branches
	1.18.4.1;
next	1.17;

1.17
date	2001.01.11.00.41.23;	author edk;	state Exp;
branches;
next	1.16;

1.16
date	2000.12.28.04.11.40;	author edk;	state Exp;
branches;
next	1.15;

1.15
date	2000.12.21.04.01.50;	author edk;	state Exp;
branches;
next	1.14;

1.14
date	2000.12.15.01.51.40;	author edk;	state Exp;
branches;
next	1.13;

1.13
date	2000.12.14.17.57.58;	author edk;	state Exp;
branches;
next	1.12;

1.12
date	2000.12.14.02.35.59;	author edk;	state Exp;
branches;
next	1.11;

1.11
date	2000.12.12.05.40.46;	author edk;	state Exp;
branches;
next	1.10;

1.10
date	2000.12.09.06.03.16;	author edk;	state Exp;
branches;
next	1.9;

1.9
date	2000.12.06.20.28.17;	author edk;	state Exp;
branches;
next	1.8;

1.8
date	2000.12.06.20.25.27;	author edk;	state Exp;
branches;
next	1.7;

1.7
date	2000.12.06.20.24.53;	author edk;	state Exp;
branches;
next	1.6;

1.6
date	2000.12.06.17.01.15;	author edk;	state Exp;
branches;
next	1.5;

1.5
date	2000.12.01.06.12.51;	author edk;	state Exp;
branches
	1.5.2.1;
next	1.4;

1.4
date	2000.11.30.12.00.57;	author edk;	state Exp;
branches;
next	1.3;

1.3
date	2000.11.29.05.42.38;	author edk;	state Exp;
branches;
next	1.2;

1.2
date	2000.11.23.06.10.31;	author edk;	state Exp;
branches;
next	1.1;

1.1
date	2000.11.23.05.51.52;	author edk;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	2001.02.21.00.36.23;	author dlr;	state Exp;
branches;
next	;

1.5.2.1
date	2000.12.09.01.16.24;	author edk;	state Exp;
branches;
next	1.5.2.2;

1.5.2.2
date	2000.12.09.06.46.37;	author edk;	state Exp;
branches;
next	1.5.2.3;

1.5.2.3
date	2000.12.15.02.41.54;	author edk;	state Exp;
branches;
next	1.5.2.4;

1.5.2.4
date	2001.01.11.00.53.34;	author edk;	state Exp;
branches;
next	;

1.18.4.1
date	2001.02.16.04.23.23;	author davidp;	state Exp;
branches;
next	1.18.4.2;

1.18.4.2
date	2001.02.20.04.37.28;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.21
log
@switch from using a generated file to using the environment
@
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);

    if ($options->{type} eq 'all' or $options->{type} eq 'eyebrowse') {
        handleEyebrowseItems($options, $eyebrowse_command_info);
    }
    if ($options->{type} eq 'all' or $options->{type} eq 'anzu') {
        handleAnzuItems($options, $anzu_command_info);
    }
}

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

sub handleAnzuItems {
    my ($options, $anzu_command) = @@_;
    sleep 2;
    print STDERR "running anzu Harness; if this hangs, please kill it,\n";
    print STDERR "restart your sandbox, and try again.\n";

    system(@@$anzu_command);
}

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

#   this is now (effectively) done in anzu:
#    system("rm -fr $eyebrowse_dir");
    mkdir $eyebrowse_dir, 0755;

    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";
            system(@@eyebrowse_command_base, '--command','deletelist',
                    '--listname', $list_address);
        }
        print STDERR "adding list $list_address to eyebrowse database\n";
        system(@@eyebrowse_command_base, '--command', 'addlist', '--listname',
                $list_address, '--descr', $desc, '--listid', $listID);
        print STDERR "adding list file to eyebrowse database\n";
    
        system('mkdir', '-p', $list_file_dir);
        system('touch', $list_file);
        system(@@eyebrowse_command_base, '--command', 'addlistfile',
                '--listname', $list_address, '--listfile', $list_file);

	print STDERR "adding search-index $list_file_dir/index\n";
	mkdir ("$list_file_dir/index", 0755);
        system(@@eyebrowse_command_base, '--command', 'addindex', '--listname',
		+$list_address, '--indexdir', "$list_file_dir/index");
    }
    my $eyebrowse_dir ="__SANDBOX__/data/eyebrowse";
    if ($> == 0) {
        print STDERR "Setting ownership of the eyebrowse data directory\n";
        system("chown","-R", "tigrisq.tigris", $eyebrowse_dir);
    }
}

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

sub constructCommandInfo {
    my ($dbh) = @@_;
    my $java = findJava();
   
    my @@anzu_command = (
            $java,
            "-Dorg.tigris.anzu.secret=__SANDBOX__/data/anzu/secret",
            "-Dorg.tigris.anzu.RMIURL=rmi://localhost:__RMI_PORT__/".
                "org.tigris.anzu.mail",
            "org.tigris.anzu.client.Harness"
    );
    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_command, 'delete_domain', $domain);
        push(@@anzu_command, '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_command, '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->{LOGIN_NAME};
            $ownerAddress .= "\n" if $ownerAddress;
            $ownerAddress .= "$ownerLoginID\@@$domain";
        }
        push(@@anzu_command, 'alias', $domain, $project, 'create',
                'owner', $ownerAddress);
    }

    $select = getUsers($main::dbh);
    while(my $userRecord = $select->fetchrow_hashref) {
        my $user = $userRecord->{LOGIN_NAME};
        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_command, 'alias', $domain, 'www', 'create', $user,
                        $email);
            }
            next;
        }
        push(@@anzu_command, '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->{MAILINGLIST_ID};
        my $desc     = $mlRecord->{DESCRIPTION};
        push(@@anzu_command, 'ml',$domain,$project,'create',$listName,$type);
        if ('__MAIL_PREFIX__' ne '') {
            push (@@anzu_command, 'ml', $domain, $project, 'update_string',
                    $listName, 'mail_prefix', '__MAIL_PREFIX__');
        }
        my $archiver_address = "__MAIL_PREFIX__archive=mail-$project=".
            "$listName=$domain\@@__MAIL_ARCHIVE_HOST__";
        my $list_address     = "__MAIL_PREFIX__$listName\@@$project.$domain";
        my $list_file_dir    =
            "__SANDBOX__/data/eyebrowse/$domain/$project/$listName";

        push(@@anzu_command, 'ml', $domain, $project, 'subscribe', $listName, '',
                $archiver_address, '');
        push @@eyebrowse_lists, [$list_address,$listID,$desc,$list_file_dir];
    }
    return (\@@anzu_command, \@@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.LOGIN_NAME ".
            "FROM HELM_USER u, HELM_USER_ROLE_PROJECT urp, HELM_ROLE r ".
            "WHERE u.USER_ID = urp.USER_ID and urp.PROJECT_ID = $projectID and ".
                "urp.ROLE_ID = r.ROLE_ID 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 LOGIN_NAME, EMAIL, HELM_DOMAIN.NAME as DOMAIN ".
            "FROM HELM_USER, HELM_DOMAIN ".
            "WHERE HELM_USER.DOMAIN_ID = 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, MAILINGLIST_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 = '__DATABASE_NAME__';
    my $db_host = '__DATABASE_HOST__';
    my $db_port = '__DATABASE_PORT__';
    my $db_user = '__DATABASE_USER__';
    my $db_pass = '__DATABASE_PASSWORD__';
    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 = "__SANDBOX__/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  ('__SANDBOX__/site/scripts/eyebrowse/eyebrowse');
}

@


1.20
log
@This is the highest revision from the HELM_PEER_PORT_BRANCH.  The previous import caused too many conflicts to resolve manually
@
text
@@


1.19
log
@now anzu_initialize will create the index and add it to eyebrowse when run with
--type eyebrowse || all
@
text
@d70 1
a70 1
		$list_address, '--indexdir', "$list_file_dir/index");
d97 1
a97 1
        my $domain = $domainRecord->{domain};
d108 1
a108 1
        my $domain  = $projectRecord->{domain};
d115 1
a115 1
            my $ownerLoginID = $ownerRecord->{loginID};
d125 3
a127 3
        my $user = $userRecord->{loginID};
        my $email = $userRecord->{email};
        my $domain = $userRecord->{domain};
d145 1
a145 1
        my $listID   = $mlRecord->{MAILING_LIST_ID};
d191 1
a191 1
            "SELECT HELM_DOMAIN.NAME as domain ".
d201 1
a201 1
            "SELECT HELM_PROJECT.NAME as project, HELM_DOMAIN.NAME as domain, ".
d214 4
a217 4
            "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\"");
d226 3
a228 3
            "SELECT loginID, email, HELM_DOMAIN.NAME as domain ".
            "FROM User, HELM_DOMAIN ".
            "WHERE User.domainID = HELM_DOMAIN.DOMAIN_ID");
d238 1
a238 1
            "Domain.NAME as DOMAIN_NAME, MAILING_LIST_ID, ml.DESCRIPTION ".
@


1.18
log
@switch the default : now you must explicitly provide --clean if you want
to remove existing data.  perhaps the default should be based on
IN_PRODUCTION?
@
text
@d66 5
@


1.18.4.1
log
@Changed the names that were previously for Joist tables to be HELM_* tables
instead.
@
text
@d92 1
a92 1
        my $domain = $domainRecord->{DOMAIN};
d103 1
a103 1
        my $domain  = $projectRecord->{DOMAIN};
d110 1
a110 1
            my $ownerLoginID = $ownerRecord->{LOGIN_NAME};
d120 3
a122 3
        my $user = $userRecord->{LOGIN_NAME};
        my $email = $userRecord->{EMAIL};
        my $domain = $userRecord->{DOMAIN};
d140 1
a140 1
        my $listID   = $mlRecord->{MAILINGLIST_ID};
d186 1
a186 1
            "SELECT HELM_DOMAIN.NAME as DOMAIN ".
d196 1
a196 1
            "SELECT HELM_PROJECT.NAME as project, HELM_DOMAIN.NAME as DOMAIN, ".
d209 4
a212 4
            "SELECT u.LOGIN_NAME ".
            "FROM HELM_USER u, HELM_USER_ROLE_PROJECT urp, HELM_ROLE r ".
            "WHERE u.USER_ID = urp.USER_ID and urp.PROJECT_ID = $projectID and ".
                "urp.ROLE_ID = r.ROLE_ID and r.NAME=\"Project Owner\"");
d221 3
a223 3
            "SELECT LOGIN_NAME, EMAIL, HELM_DOMAIN.NAME as DOMAIN ".
            "FROM HELM_USER, HELM_DOMAIN ".
            "WHERE HELM_USER.DOMAIN_ID = HELM_DOMAIN.DOMAIN_ID");
d233 1
a233 1
            "Domain.NAME as DOMAIN_NAME, MAILINGLIST_ID, ml.DESCRIPTION ".
@


1.18.4.2
log
@Merging in changes from the HEAD.
@
text
@a65 5

	print STDERR "adding search-index $list_file_dir/index\n";
	mkdir ("$list_file_dir/index", 0755);
        system(@@eyebrowse_command_base, '--command', 'addindex', '--listname',
		+$list_address, '--indexdir', "$list_file_dir/index");
@


1.17
log
@allow this to be used for only anzu or only eyebrowse; and provide
an option (unused so far) to preserve existing data
@
text
@d260 1
a260 1
    if (!GetOptions(\%options, "type=s", "preserve", "help") 
d275 3
a277 3
Usage: anzu_initialize [--type={type}] [--preserve]
        type          One of 'all', 'eyebrowse', 'anzu'
        preserve      A flag which indicates the data should not be deleted
@


1.16
log
@handle root (which is now associated w/ **HOST, not the domain) reasonably
@
text
@d3 2
d9 1
d18 6
a23 2
    handleEyebrowseItems($eyebrowse_command_info);
    handleAnzuItems($anzu_command_info);
d31 1
a31 1
    my ($anzu_command) = @@_;
d40 1
a40 1
    my ($eyebrowse_lists) = @@_;
d256 24
@


1.15
log
@handle EB items before doing the anzu stuff -- because anzu expects
that the EB directory exists
@
text
@d82 1
d90 1
d117 7
@


1.14
log
@use hotspot when it's available
@
text
@d15 1
a16 2
    handleEyebrowseItems($eyebrowse_command_info);

d35 4
a60 3
#   this is now done in anzu:
#    system("rm -fr $eyebrowse_dir");
    mkdir $eyebrowse_dir, 0755;
@


1.13
log
@ug ... we should delete the eb list and re-add it, otherwise we'll have old
messages in it
@
text
@d73 1
a73 1
    my $java = getJava();
d147 5
a151 1
sub getJava {
@


1.12
log
@use delete_domain rather than remove_domain now
@
text
@d35 3
d41 5
a45 3
        if (grep /^$list_address$/, @@existing_lists) {
            print STDERR "$list_address is already set up\n";
            next;
a46 1
        my @@eyebrowse_command_base = getEyebrowseCommandBase();
d58 2
a59 1
    system("rm -fr $eyebrowse_dir");
a72 1
    my ($eyebrowse_lists_ref) = getEyebrowseLists();
d139 1
a139 5
        if (grep /^$list_address$/, @@$eyebrowse_lists_ref) {
            print STDERR "$list_address already exists in eyebrowse db\n";
        } else {
            push @@eyebrowse_lists, [$list_address,$listID,$desc,$list_file_dir];
        }
@


1.11
log
@use package name to qualify system properties
@
text
@d83 1
a83 1
        push(@@anzu_command, 'remove_domain', $domain);
@


1.10
log
@use MAIL_PREFIX so lists will work in developer sandboxes; and
set up owner email aliases
@
text
@d73 4
a76 3
            "-Danzu.secret=__SANDBOX__/data/anzu/secret",
            "-DRMIURL=rmi://localhost:__RMI_PORT__/mail",
           "org.tigris.anzu.client.Harness"
@


1.9
log
@fix syntax error and improve status message for eyebrowse lists
@
text
@d92 10
a112 1

d122 4
d167 1
a167 1
    $ret     = $select->execute();
d175 2
a176 1
            "SELECT HELM_PROJECT.NAME as project, HELM_DOMAIN.NAME as domain ".
d180 1
a180 1
    $ret     = $select->execute();
d185 12
d203 1
a203 1
    $ret    = $select->execute();
d216 1
a216 1
    $ret    = $select->execute();
@


1.8
log
@nuke the one remaining instance of __DOMAIN__
PR:
Obtained from:
Submitted by:
Reviewed by:
@
text
@d34 1
a34 1
    my $eyebrowse_lists = @@_;
d43 1
a43 1
        print STDERR "adding list to eyebrowse database\n";
@


1.7
log
@lots of reorganization to make this cleaner; also, use HELM_DOMAIN
in interests of supporting multiple domains.
@
text
@d117 1
a117 1
            "__SANDBOX__/data/eyebrowse/__DOMAIN__/$project/$listName";
@


1.6
log
@* call anzu's Harness class only once (but with lots of arguments) -- this
  will usually avoid the problems with the JVM hanging, and it's much
  faster anyway
* don't call the eyebrowse wrapper script for lists which are already set
  up
* some reorganization into functions.
@
text
@d3 1
a3 1
my $type = shift @@ARGV;
d5 9
a13 59
setupEnvironment();
my $java = getJava();
my $dbh = getDatabaseConnection();
my ($select, $ret);

my @@anzu_command = (
        $java,
        "-Danzu.secret=__SANDBOX__/data/anzu/secret",
        "-DRMIURL=rmi://localhost:__RMI_PORT__/mail",
       "org.tigris.anzu.client.Harness"
);

my @@eyebrowse_command_base = (
        '__SANDBOX__/site/scripts/eyebrowse/eyebrowse'
);

push(@@anzu_command, 'remove_domain', '__DOMAIN__');
push(@@anzu_command, 'add_domain', '__DOMAIN__');

$select = getProjects($dbh);
my $projectRecord;
while ($projectRecord = $select->fetchrow_hashref) {
    my $project = $projectRecord->{project};
    my $domain  = $projectRecord->{domain};
    next if ($project=~ m/\*/);
    push(@@anzu_command, 'add_project', $domain, $project);
}

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


$select = getMailingLists($dbh);
my $mlRecord;
while ($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_command, 'ml', $domain, $project, 'create', $listName, $type);
    my $archiver_address = "__MAIL_PREFIX__archive=mail-$project=$listName=".
        "$domain\@@__MAIL_ARCHIVE_HOST__";
    my $list_address     = "__MAIL_PREFIX__$listName\@@$project.$domain";
    my $list_file_dir    =
        "__SANDBOX__/data/eyebrowse/__DOMAIN__/$project/$listName";

    push(@@anzu_command, 'ml', $domain, $project, 'subscribe', $listName, '',
            $archiver_address, '');
    push @@eyebrowse_lists, [$list_address, $listID, $desc, $list_file_dir];
}
d15 2
d18 1
a18 2
&handleAnzuItems();
&handleEyebrowseItems();
d20 3
d25 2
d30 1
a30 1
    system(@@anzu_command);
d34 2
a35 10
    # let's avoid nasty errors.  if these lits are already in the database,
    # just ignore them.
    open (IN, "-|") or exec @@eyebrowse_command_base, '--command','getlists';
    while (<IN>) {
        chomp;
        push @@existing_lists, $_;
    }
    close(IN);
    
    foreach $item (@@eyebrowse_lists) {
d42 1
d63 68
d149 10
d162 1
a162 1
            "SELECT HELM_PROJECT.NAME as project, HELM_DOMAIN.NAME as domain " .
d164 2
a165 1
            "WHERE HELM_PROJECT.DOMAIN_ID = HELM_DOMAIN.DOMAIN_ID");
d196 1
a196 1
END { $dbh->disconnect if $dbh }
d215 16
@


1.5
log
@use archive=mail- as the archive prefix -- so as to avoid name
space collisions w/ projects
@
text
@d5 4
a8 1
my $anzu_jar = "__SANDBOX__/site/lib/anzu.jar";
d10 1
a10 29
$ENV{CLASSPATH} = "$ENV{CLASSPATH}:$anzu_jar";

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;
}

my $db_base = 'mysql';
use DBI;  
my $db_name = '__DATABASE_NAME__';
my $db_host = '__DATABASE_HOST__';
my $db_port = '__DATABASE_PORT__';
my $db_user = '__DATABASE_USER__';
my $db_pass = '__DATABASE_PASSWORD__';
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";
my $select;
my $ret;

my @@command_base = (
d21 4
a24 15
if (not $type or $type eq "install") {
    system(@@command_base, 'remove_domain', '__DOMAIN__');
} else {
    print STDERR "*** since this is not an install, the following commands\n",
        "may produce error messages.  please ignore them if so. ***\n";
    sleep 2;
}
system(@@command_base, 'add_domain', '__DOMAIN__');

$select  = $dbh->prepare(
       "SELECT HELM_PROJECT.NAME as project, HELM_DOMAIN.NAME as domain " .
       "FROM HELM_PROJECT, HELM_DOMAIN ".
       "WHERE HELM_PROJECT.DOMAIN_ID = HELM_DOMAIN.DOMAIN_ID");
$ret     = $select->execute();
warn "error selecting projects" if not $ret;
d30 1
a30 2
    print STDERR "adding project $project to domain $domain\n";
    system(@@command_base, 'add_project', $domain, $project);
d33 1
a33 4
$select = $dbh->prepare("SELECT loginID, email, HELM_DOMAIN.NAME as domain "
        ."FROM User, HELM_DOMAIN where User.domainID = HELM_DOMAIN.DOMAIN_ID");
$ret    = $select->execute();
warn "error selecting users" if not $ret;
d40 1
a40 2
    print STDERR "adding user $user w/ email $email to domain $domain\n";
    system(@@command_base, 'alias', $domain, 'www', 'create', $user, $email);
d44 1
a44 8
$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");
$ret    = $select->execute();
warn "error selecting mailing lists" if not $ret;
d53 1
a53 2
    print STDERR "adding ml $listName type $type to $project in $domain\n";
    system(@@command_base, 'ml', $domain, $project, 'create', $listName, $type);
a58 1
    my $list_file = "$list_file_dir/0mbox";
d60 51
a110 12
    print STDERR "subscribing archiver address $archiver_address\n";
    system(@@command_base, 'ml', $domain, $project, 'subscribe', $listName, '',
            $archiver_address);
    print STDERR "adding list to eyebrowse database\n";
    system(@@eyebrowse_command_base, '--command', 'addlist', '--listname',
            $list_address, '--descr', $desc, '--listid', $listID);
    print STDERR "adding list file to eyebrowse database\n";

    system('mkdir', '-p', $list_file_dir);
    system('touch', $list_file);
    system(@@eyebrowse_command_base, '--command', 'addlistfile', '--listname',
            $list_address, '--listfile', $list_file);
d113 55
d170 16
a185 6
my $eyebrowse_dir ="__SANDBOX__/data/eyebrowse";
system("rm -fr $eyebrowse_dir");
mkdir $eyebrowse_dir, 0755;
if ($> == 0) {
    print STDERR "Setting ownership of the eyebrowse data directory\n";
    system("chown","-R", "tigrisq.tigris", $eyebrowse_dir);
@


1.5.2.1
log
@use new Harness interface, for better reliability
@
text
@d3 1
a3 1
main();
d5 1
a5 4
sub main {
    setupEnvironment();
    $main::dbh = getDatabaseConnection();
    my ($select, $ret);
d7 1
a7 4
    # 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);
d9 6
a14 2
    handleAnzuItems($anzu_command_info);
    handleEyebrowseItems($eyebrowse_command_info);
d16 4
d22 29
a50 6
#################################
### handle any required actions
#################################

sub handleAnzuItems {
    my ($anzu_command) = @@_;
a51 33
    print STDERR "running anzu Harness; if this hangs, please kill it,\n";
    print STDERR "restart your sandbox, and try again.\n";

    system(@@$anzu_command);
}

sub handleEyebrowseItems {
    my ($eyebrowse_lists) = @@_;
    foreach $item (@@$eyebrowse_lists) {
        my ($list_address, $listID, $desc, $list_file_dir) = @@$item;
        my $list_file = "$list_file_dir/0mbox";
        if (grep /^$list_address$/, @@existing_lists) {
            print STDERR "$list_address is already set up\n";
            next;
        }
        my @@eyebrowse_command_base = getEyebrowseCommandBase();
        print STDERR "adding list $list_address to eyebrowse database\n";
        system(@@eyebrowse_command_base, '--command', 'addlist', '--listname',
                $list_address, '--descr', $desc, '--listid', $listID);
        print STDERR "adding list file to eyebrowse database\n";
    
        system('mkdir', '-p', $list_file_dir);
        system('touch', $list_file);
        system(@@eyebrowse_command_base, '--command', 'addlistfile',
                '--listname', $list_address, '--listfile', $list_file);
    }
    my $eyebrowse_dir ="__SANDBOX__/data/eyebrowse";
    system("rm -fr $eyebrowse_dir");
    mkdir $eyebrowse_dir, 0755;
    if ($> == 0) {
        print STDERR "Setting ownership of the eyebrowse data directory\n";
        system("chown","-R", "tigrisq.tigris", $eyebrowse_dir);
    }
d53 1
d55 27
a81 66
#################################
### process information according to business logic
#################################

sub constructCommandInfo {
    my ($dbh) = @@_;
    my ($eyebrowse_lists_ref) = getEyebrowseLists();
    my $java = getJava();
   
    my @@anzu_command = (
            $java,
            "-Danzu.secret=__SANDBOX__/data/anzu/secret",
            "-DRMIURL=rmi://localhost:__RMI_PORT__/mail",
           "org.tigris.anzu.client.Harness"
    );
    $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_command, 'remove_domain', $domain);
        push(@@anzu_command, 'add_domain', $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_command, 'add_project', $domain, $project);
    }

    $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 ?
        push(@@anzu_command, '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_command, 'ml',$domain,$project,'create',$listName,$type);
        my $archiver_address = "__MAIL_PREFIX__archive=mail-$project=".
            "$listName=$domain\@@__MAIL_ARCHIVE_HOST__";
        my $list_address     = "__MAIL_PREFIX__$listName\@@$project.$domain";
        my $list_file_dir    =
            "__SANDBOX__/data/eyebrowse/$domain/$project/$listName";

        push(@@anzu_command, 'ml', $domain, $project, 'subscribe', $listName, '',
                $archiver_address, '');
        if (grep /^$list_address$/, @@$eyebrowse_lists_ref) {
            print STDERR "$list_address already exists in eyebrowse db\n";
        } else {
            push @@eyebrowse_lists, [$list_address,$listID,$desc,$list_file_dir];
        }
    }
    return (\@@anzu_command, \@@eyebrowse_lists);
a83 18
#################################
###  preparation (getting information, db connections, etc.)
#################################
sub getJava {
    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;
}
d85 1
a85 36
sub getDomains {
    my ($dbh) = @@_;
    my $select  = $dbh->prepare(
            "SELECT HELM_DOMAIN.NAME as domain ".
            "FROM HELM_DOMAIN");
    $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 ".
            "FROM HELM_PROJECT, HELM_DOMAIN ".
            "WHERE HELM_PROJECT.DOMAIN_ID = HELM_DOMAIN.DOMAIN_ID AND " .
                "HELM_PROJECT.TYPE!='host' AND HELM_PROJECT.TYPE != 'domain'");
    $ret     = $select->execute();
    warn "error selecting projects" 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");
    $ret    = $select->execute();
    warn "error selecting users" if not $ret;
    return $select;
}

sub getMailingLists {
    my ($dbh) = @@_;
    my $select = $dbh->prepare(
d91 41
a131 3
    $ret    = $select->execute();
    warn "error selecting mailing lists" if not $ret;
    return $select;
a132 37

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

sub getDatabaseConnection {
    my $db_base = 'mysql';
    my $db_name = '__DATABASE_NAME__';
    my $db_host = '__DATABASE_HOST__';
    my $db_port = '__DATABASE_PORT__';
    my $db_user = '__DATABASE_USER__';
    my $db_pass = '__DATABASE_PASSWORD__';
    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 setupEnvironment {
    my $anzu_jar = "__SANDBOX__/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  ('__SANDBOX__/site/scripts/eyebrowse/eyebrowse');
}

@


1.5.2.2
log
@changes to support owner email aliases.  tested in head, tested in
branch, and ok'ed by jr.
@
text
@a91 10
        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_command, 'alias', $domain, $project, 'create',
                'owner', $ownerAddress);
d103 1
a112 4
        if ('__MAIL_PREFIX__' ne '') {
            push (@@anzu_command, 'ml', $domain, $project, 'update_string',
                    $listName, 'mail_prefix', '__MAIL_PREFIX__');
        }
d154 1
a154 1
    my $ret  = $select->execute();
d162 1
a162 2
            "SELECT HELM_PROJECT.NAME as project, HELM_DOMAIN.NAME as domain, ".
                "HELM_PROJECT.PROJECT_ID as projectID ".
d166 1
a166 1
    my $ret  = $select->execute();
a170 12
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;
}

d177 1
a177 1
    my $ret = $select->execute();
d190 1
a190 1
    my $ret = $select->execute();
@


1.5.2.3
log
@use hotspot if available
@
text
@a146 4
   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';
    }                                                
@


1.5.2.4
log
@port changes in the head back to the branch, to allow this to be used
to put the db into a valid state.  the --clean option doesn't really
work as it should yet, but fixing that would require larger changes.
@
text
@a2 2
use Getopt::Long;

a6 1
    my $options = getOptions();
d15 3
a17 6
    if ($options->{type} eq 'all' or $options->{type} eq 'anzu') {
        handleAnzuItems($options, $anzu_command_info);
    }
    if ($options->{type} eq 'all' or $options->{type} eq 'eyebrowse') {
        handleEyebrowseItems($options, $eyebrowse_command_info);
    }
d25 1
a25 1
    my ($options, $anzu_command) = @@_;
d34 1
a34 1
    my ($options, $eyebrowse_lists) = @@_;
d54 1
a54 3
    if ($options{clean}) {
        system("rm -fr $eyebrowse_dir");
    }
a238 24
}

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;
@


1.4
log
@handle eyebrowse archives as well when setting up
@
text
@d103 2
a104 2
    my $archiver_address = "__MAIL_PREFIX__archive-$project=$listName=$domain".
        "\@@__MAIL_ARCHIVE_HOST__";
@


1.3
log
@anzu_initialize.sh is deprecated : now it tries to exec anzu_initialize
anzu_initialize now creates mailing lists from whatever is in
HELM_MAILINGLIST
@
text
@d42 4
d87 1
a87 1
            "Domain.NAME as DOMAIN_NAME ".
d99 2
d105 5
d113 9
@


1.2
log
@be more lenient w/ the upgrade process -- this should allow it to work
even if the data directory has nothing in it.
@
text
@d55 1
a55 1
$ret     = $select->execute;
d59 2
a60 2
    my $project = $projectRecord->{'project'};
    my $domain  = $projectRecord->{'domain'};
d66 1
a66 1
my $select = $dbh->prepare("SELECT loginID, email, HELM_DOMAIN.NAME as domain "
d68 1
a68 1
my $ret    = $select->execute;
d72 3
a74 3
    my $user = $userRecord->{'loginID'};
    my $email = $userRecord->{'email'};
    my $domain = $userRecord->{'domain'};
d78 24
@


1.1
log
@a more sophisticated initalization script for anzu : this sets up
projects & usernames as required.  it also has an 'upgrade' mode
which avoids blowing away all the data associated w/ a domain, though
this may also produce error messages.
@
text
@a43 1
    system(@@command_base, 'add_domain', '__DOMAIN__');
d49 1
@


1.1.1.1
log
@Replacing the HEAD with the HELM_PEER_PORT_BRANCH.
@
text
@d3 1
a3 1
use Getopt::Long;
d5 1
a5 1
main();
d7 1
a7 10
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);
d9 4
a12 2
    if ($options->{type} eq 'all' or $options->{type} eq 'eyebrowse') {
        handleEyebrowseItems($options, $eyebrowse_command_info);
a13 142
    if ($options->{type} eq 'all' or $options->{type} eq 'anzu') {
        handleAnzuItems($options, $anzu_command_info);
    }
}

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

sub handleAnzuItems {
    my ($options, $anzu_command) = @@_;
    sleep 2;
    print STDERR "running anzu Harness; if this hangs, please kill it,\n";
    print STDERR "restart your sandbox, and try again.\n";

    system(@@$anzu_command);
}

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

#   this is now (effectively) done in anzu:
#    system("rm -fr $eyebrowse_dir");
    mkdir $eyebrowse_dir, 0755;

    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";
            system(@@eyebrowse_command_base, '--command','deletelist',
                    '--listname', $list_address);
        }
        print STDERR "adding list $list_address to eyebrowse database\n";
        system(@@eyebrowse_command_base, '--command', 'addlist', '--listname',
                $list_address, '--descr', $desc, '--listid', $listID);
        print STDERR "adding list file to eyebrowse database\n";
    
        system('mkdir', '-p', $list_file_dir);
        system('touch', $list_file);
        system(@@eyebrowse_command_base, '--command', 'addlistfile',
                '--listname', $list_address, '--listfile', $list_file);

	print STDERR "adding search-index $list_file_dir/index\n";
	mkdir ("$list_file_dir/index", 0755);
        system(@@eyebrowse_command_base, '--command', 'addindex', '--listname',
		+$list_address, '--indexdir', "$list_file_dir/index");
    }
    my $eyebrowse_dir ="__SANDBOX__/data/eyebrowse";
    if ($> == 0) {
        print STDERR "Setting ownership of the eyebrowse data directory\n";
        system("chown","-R", "tigrisq.tigris", $eyebrowse_dir);
    }
}

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

sub constructCommandInfo {
    my ($dbh) = @@_;
    my $java = findJava();
   
    my @@anzu_command = (
            $java,
            "-Dorg.tigris.anzu.secret=__SANDBOX__/data/anzu/secret",
            "-Dorg.tigris.anzu.RMIURL=rmi://localhost:__RMI_PORT__/".
                "org.tigris.anzu.mail",
            "org.tigris.anzu.client.Harness"
    );
    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_command, 'delete_domain', $domain);
        push(@@anzu_command, '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_command, '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->{LOGIN_NAME};
            $ownerAddress .= "\n" if $ownerAddress;
            $ownerAddress .= "$ownerLoginID\@@$domain";
        }
        push(@@anzu_command, 'alias', $domain, $project, 'create',
                'owner', $ownerAddress);
    }

    $select = getUsers($main::dbh);
    while(my $userRecord = $select->fetchrow_hashref) {
        my $user = $userRecord->{LOGIN_NAME};
        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_command, 'alias', $domain, 'www', 'create', $user,
                        $email);
            }
            next;
        }
        push(@@anzu_command, '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->{MAILINGLIST_ID};
        my $desc     = $mlRecord->{DESCRIPTION};
        push(@@anzu_command, 'ml',$domain,$project,'create',$listName,$type);
        if ('__MAIL_PREFIX__' ne '') {
            push (@@anzu_command, 'ml', $domain, $project, 'update_string',
                    $listName, 'mail_prefix', '__MAIL_PREFIX__');
        }
        my $archiver_address = "__MAIL_PREFIX__archive=mail-$project=".
            "$listName=$domain\@@__MAIL_ARCHIVE_HOST__";
        my $list_address     = "__MAIL_PREFIX__$listName\@@$project.$domain";
        my $list_file_dir    =
            "__SANDBOX__/data/eyebrowse/$domain/$project/$listName";

        push(@@anzu_command, 'ml', $domain, $project, 'subscribe', $listName, '',
                $archiver_address, '');
        push @@eyebrowse_lists, [$list_address,$listID,$desc,$list_file_dir];
    }
    return (\@@anzu_command, \@@eyebrowse_lists);
d16 4
a19 80
#################################
###  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.LOGIN_NAME ".
            "FROM HELM_USER u, HELM_USER_ROLE_PROJECT urp, HELM_ROLE r ".
            "WHERE u.USER_ID = urp.USER_ID and urp.PROJECT_ID = $projectID and ".
                "urp.ROLE_ID = r.ROLE_ID 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 LOGIN_NAME, EMAIL, HELM_DOMAIN.NAME as DOMAIN ".
            "FROM HELM_USER, HELM_DOMAIN ".
            "WHERE HELM_USER.DOMAIN_ID = 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, MAILINGLIST_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;
d22 1
d24 25
a48 13
END { $main::dbh->disconnect if $main::dbh }

sub getDatabaseConnection {
    my $db_base = 'mysql';
    my $db_name = '__DATABASE_NAME__';
    my $db_host = '__DATABASE_HOST__';
    my $db_port = '__DATABASE_PORT__';
    my $db_user = '__DATABASE_USER__';
    my $db_pass = '__DATABASE_PASSWORD__';
    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;
d51 37
a87 27
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 = "__SANDBOX__/site/lib/anzu.jar";
    $ENV{CLASSPATH} = "$ENV{CLASSPATH}:$anzu_jar";
a88 16

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  ('__SANDBOX__/site/scripts/eyebrowse/eyebrowse');
}

@


