#!/usr/local/bin/perl -w

use strict;

# our ad-hoc lock module
use hm_lock;

## upgrade_archive
##
## quick and dirty script for upgrading the archives from one version
## to another.

## Author: Jose Kahan (W3C)
## History:
## 22/Dec/1999 : Written from scratch (JK)
##

## configuration

# mailing list path
my $list_path = "/home/lists"; 
my $update_archive = "$list_path/w3t-test/update-archive";

## global variables

my $lock_status = 0;
my $lock_file = "";

#######################################################

END {
    if ($lock_status && $lock_file ne "") {
	lock_clear ("$lock_file");
	$lock_status = 0;
    }
}

sub change_symlink {
    my ($list_dir) = @_;

    unlink ("$list_dir/rc.local.s20");
    unless (symlink ("../.etc/rc.local.s20-mime", "$list_dir/rc.local.s20")) {
	die "couldn't create rc.local.s20-mime symlink in $list_dir: #!\n";
    }

} # change_sylink

sub upgrade_list {
    my ($list_name) = @_;
    my ($list_dir);

    $list_dir = "$list_path/$list_name";

    if (!-d $list_dir) {
	die "$list_dir doesn't exist?\n";
    }
    
    if (!-d "$list_dir/ArchiveStatus") {
	die "$list_name isn't a hypermail list!\n";
    }

    $lock_file = "$list_dir/rc.lock";
    lock_set ($lock_file);
    $lock_status = 1;

    system $update_archive, "-list", $list_name, "-no_lock";
    change_symlink ($list_dir);

    lock_clear ($lock_file);
    $lock_file = "";
    $lock_status = 0;

} # upgrade_list

sub read_list_file {
    my ($list_file) = @_;
    my (@lists);

    open (IN, "<$list_file") 
	or die "Couldn't open $list_file: $!\n";

    while (<IN>) {
	chomp;
	push @lists, $_;
    }

    close (IN);
    
    return (@lists);
}
## main

{
    my (@lists);
    my ($list_file);
    my ($list);

    ($list_file) = @ARGV;

    if (!defined ($list_file)) {
	die "usage: upgrade listfile\n";
    }
    
    @lists = read_list_file ($list_file);

    foreach $list (@lists) {
	print "#####\n"
	      . "\t upgrading $list\n"
	      . "#####\n";

	upgrade_list ("$list");
    }

    print "\n##### finished!\n";

} ## main
