#!/usr/bin/perl

# I got this script from Stéphane Bortzmeyer <bortz@pasteur.fr>
# You need to launch it for each list
# perl ./tulp2sympa list-name

# Ce script m'a été fourni par Stéphane Bortzmeyer <bortz@pasteur.fr>
# Il faut le lancer à la main pour chaque liste :
# perl ./tulp2sympa nom_de_la_liste

$tulp_dir = "/home/listserv/expl";
$sympa_dir = "/var/spool/sympa/expl";

$list = shift (@ARGV);

if (! $list) {
	die "Usage: $0 list-name"
}

if (! -e "$tulp_dir/$list.u") {
	die "I cannot find list $list in $tulp_dir";
}

open (OLD, "< $tulp_dir/$list.u") || die "Cannot open old config: $!";
mkdir ("$sympa_dir/$list", 0700) || die "Cannot mkdir in $sympa_dir: $!";
open (STATS, "> $sympa_dir/$list/stats");
open (ABONNES, "> $sympa_dir/$list/abonnes");
if (-e "$tulp_dir/$list.w") {
	open (DOTW, "< $tulp_dir/$list.w");
	open (BIENVENUE, "> $sympa_dir/$list/bienvenue");
	while (<DOTW>) {
		print BIENVENUE;
	}	
	close (DOTW);
	close (BIENVENUE);
}
open (CONFIG, "> $sympa_dir/$list/config");
open (ABONNES, "> $sympa_dir/$list/abonnes");
$in_abonnes = 0;
while (<OLD>) {
	if (/\s*#/) {
		/^\s*#\s*([a-zA-Z]+)\s*=\s*(.*)$/;
		$keyword = $1;
		$value = $2;
		if ($keyword =~ /^owner$/i) {
			print CONFIG "owner \n";
			print CONFIG "email $value\n\n";
		} else {
			print CONFIG "$keyword $value\n";
		}
	} elsif (/^\s*$/) {
		# Ignore empty lines
	} else {
		/^\s*([a-zA-Z0-9\.\-_@%<>\"]+)\s*(\(([^\)]*)\)|)\s*$/;
		$email = $1;
		$gecos = $2;
		if (! $email) {
			die "Strange line $_";
		}
		print ABONNES "date \n";
		print ABONNES "email $email\n";
		print ABONNES "gecos $gecos\n\n";
	}
}
close (OLD);
close (CONFIG);
close (ABONNES);

system "chown -R sympa.sympa $sympa_dir/$list";
