#!/usr/bin/perl

$slash = "/";
$mailroot = $ENV{"MAIL_ROOT"};


foreach $ii (0 .. $#ARGV) {
	if ($ARGV[$ii] eq "--mail-root") {
		++$ii;
		if ($ii <= $#ARGV) {
			$mailroot = $ARGV[$ii];
		}
	} elsif ($ARGV[$ii] eq "--ecartis") {
		++$ii;
		if ($ii <= $#ARGV) {
			$ecartis = $ARGV[$ii];
		}
	} elsif ($ARGV[$ii] eq "--list-name") {
		++$ii;
		if ($ii <= $#ARGV) {
			$listname = $ARGV[$ii];
		}
	} elsif ($ARGV[$ii] eq "--domain") {
		++$ii;
		if ($ii <= $#ARGV) {
			$domain = $ARGV[$ii];
		}
	} elsif ($ARGV[$ii] eq "--admin") {
		++$ii;
		if ($ii <= $#ARGV) {
			$admin = $ARGV[$ii];
		}
	} elsif ($ARGV[$ii] eq "--purge") {
		$purge = 1;
	}
}

if (!defined($mailroot) || $mailroot eq "") {
	print STDERR "MAIL_ROOT not defined: use --mail-root MAILROOT\n";
	exit 1;
}

if (!defined($listname)) {
	print STDERR "listname not defined: use --list-name LISTNAME\n";
	exit 2;
}

if (!defined($domain)) {
	print STDERR "domain not defined: use --domain DOMAIN\n";
	exit 3;
}

$cmdalias = $mailroot . $slash . "cmdaliases" . $slash . $domain . $slash;


if (defined($purge)) {
	$fname = $cmdalias . $listname . "-owner.tab";
	if (!unlink($fname)) {
		print STDERR "unable to unlink file: $fname\n";
		exit 4;
	}

	$fname = $cmdalias . $listname . ".tab";
	if (!unlink($fname)) {
		print STDERR "unable to unlink file: $fname\n";
		exit 5;
	}

	$fname = $cmdalias . $listname . "-request.tab";
	if (!unlink($fname)) {
		print STDERR "unable to unlink file: $fname\n";
		exit 6;
	}

	$fname = $cmdalias . $listname . "-repost.tab";
	if (!unlink($fname)) {
		print STDERR "unable to unlink file: $fname\n";
		exit 7;
	}

	$fname = $cmdalias . $listname . "-admins.tab";
	if (!unlink($fname)) {
		print STDERR "unable to unlink file: $fname\n";
		exit 8;
	}

	$fname = $cmdalias . $listname . "-moderators.tab";
	if (!unlink($fname)) {
		print STDERR "unable to unlink file: $fname\n";
		exit 9;
	}

	$fname = $cmdalias . $listname . "-bounce.tab";
	if (!unlink($fname)) {
		print STDERR "unable to unlink file: $fname\n";
		exit 10;
	}

	exit 0;
}


if (!defined($admin)) {
	print STDERR "admin not defined: use --admin ADMIN-EMAIL\n";
	exit 11;
}

if (!defined($ecartis)) {
	print STDERR "lt-run pathname not defined: use --ecartis PATHNAME\n";
	exit 12;
}

if (! -d $cmdalias && !mkdir($cmdalias, 0700)) {
	print STDERR "unable to create directory: $cmdalias\n";
	exit 14;
}


$fname = $cmdalias . $listname . "-owner.tab";

if (!open(OFIL, ">$fname")) {
	print STDERR "unable to create file: $fname\n";
	exit 15;
}

print OFIL "\"redirect\"\t\"$admin\"\n";

close(OFIL);


$fname = $cmdalias . $listname . ".tab";

if (!open(OFIL, ">$fname")) {
	print STDERR "unable to create file: $fname\n";
	exit 16;
}

print OFIL "\"external\"\t\"0\"\t\"0\"\t\"$ecartis\"\t\"\@\@TMPFILE\"\t\"-s\"\t\"$listname\"\n";

close(OFIL);



$fname = $cmdalias . $listname . "-request.tab";

if (!open(OFIL, ">$fname")) {
	print STDERR "unable to create file: $fname\n";
	exit 17;
}

print OFIL "\"external\"\t\"0\"\t\"0\"\t\"$ecartis\"\t\"\@\@TMPFILE\"\t\"-r\"\t\"$listname\"\n";

close(OFIL);



$fname = $cmdalias . $listname . "-repost.tab";

if (!open(OFIL, ">$fname")) {
	print STDERR "unable to create file: $fname\n";
	exit 18;
}

print OFIL "\"external\"\t\"0\"\t\"0\"\t\"$ecartis\"\t\"\@\@TMPFILE\"\t\"-a\"\t\"$listname\"\n";

close(OFIL);



$fname = $cmdalias . $listname . "-admins.tab";

if (!open(OFIL, ">$fname")) {
	print STDERR "unable to create file: $fname\n";
	exit 19;
}

print OFIL "\"external\"\t\"0\"\t\"0\"\t\"$ecartis\"\t\"\@\@TMPFILE\"\t\"-admins\"\t\"$listname\"\n";

close(OFIL);



$fname = $cmdalias . $listname . "-moderators.tab";

if (!open(OFIL, ">$fname")) {
	print STDERR "unable to create file: $fname\n";
	exit 20;
}

print OFIL "\"external\"\t\"0\"\t\"0\"\t\"$ecartis\"\t\"\@\@TMPFILE\"\t\"-moderators\"\t\"$listname\"\n";

close(OFIL);



$fname = $cmdalias . $listname . "-bounce.tab";

if (!open(OFIL, ">$fname")) {
	print STDERR "unable to create file: $fname\n";
	exit 21;
}

print OFIL "\"external\"\t\"0\"\t\"0\"\t\"$ecartis\"\t\"\@\@TMPFILE\"\t\"-bounce\"\t\"$listname\"\n";

close(OFIL);

