#!/usr/bin/eperl
###
#
# Copyright (c) Ensim Corporation 2000, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# The information in this software is subject to change without notice
# and  should  not be  construed  as  a commitment by Ensim Corporation.
# Ensim assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Ensim.
#
# Exit codes (on failure error message goes to stderr):
#  0 - success
#  1 - failure
# 
# Boolean arguments on the command line are given as 0 or 1. All the
# command line arguments are encoded to avoid problems with escapes.
#
# All functions defined here either return an error message if an
# error occured and "" if everything went well or allways return a
# valid value, but exit (with code 1) printing an error message if an
# error occurs. This second type of functions have _e appended to
# their name. Functions are allowed to print results onto stdout, but
# errors are printed only in the main program.
#
# --------------------------------------------------------------------------
# $Id: remove_mlist,v 1.1.1.1 2008-03-01 00:21:36 aarefyev Exp $
# $Name: not supported by cvs2svn $
# --------------------------------------------------------------------------
# remove_mlist : It removes a maling list from the server
# Usage: remove_mlist <mailing-list name>
# -------------------------------------------------------

push @INC, ($ENV{'OCW_SVCPATH'} || "/usr/lib/opcenter")."/cmdline_common";
push @INC, ($ENV{'OCW_SVCPATH'} || "/usr/lib/opcenter")."/sendmail";


require '/usr/lib/opcenter/cmdline_common/CmdLineCoder.pm';
require '/usr/lib/opcenter/majordomo/mymajordomo-lib.pl';
require '/usr/lib/opcenter/majordomo/majordomo.pm';

local $in;
local %in;

# Decode the string argument before performing any vodoo on it
&CmdLineCoder::decode_args();
$name = $ARGV[0];

# find aliases and files
$conf = &get_config();
$aliases_files = [$config{'aliases_file'}];


# this gets list of all the aliases from /etc/aliases 
@aliases = &majordomo::sendmail_call("list_aliases", $aliases_files);
foreach $a (@aliases) {
}

@aliases = grep { $_->{'name'} =~ /^$name$/i ||
		  $_->{'name'} =~ /^$name-/i ||
		  $_->{'name'} =~ /-$name$/i } @aliases;

foreach $a (@aliases) {
#  &majordomo::mydbg(" after $a = ", &majordomo::dbgDict($a));
}

#$ldir = &perl_var_replace(&find_value("listdir", $conf), $conf);
$myos = &getMyOS();
if ($myos eq "Linux") {
$ldir = '/var/lib/majordomo/lists';
}
else {
$ldir = '/usr/local/majordomo/lists';
}

opendir(LDIR, $ldir);
while($f = readdir(LDIR)) {
	if ($f eq $name || $f =~ /^$name\./) {
		push(@files, "$ldir/$f");
		}
	}
closedir(LDIR);

# do the deletion
unlink(@files);
@aliases = sort { $b->{'line'} <=> $a->{'line'} } @aliases;

$whereami = `hostname -f`;
chomp $whereami;
open(EXCLIST, "> /etc/mail/majordomo-virtusertable");
local @mlist = &majordomo::list_lists($conf);
foreach $l (@mlist) {
  print EXCLIST "$l-list\@$whereami error:nouser User unknown\n";
  print EXCLIST "$l-outgoing\@$whereami error:nouser User unknown\n";
}
close(EXCLIST);
if (-f "/etc/mail/virtusertable") { 
  system("cat /etc/mail/virtusertable /etc/mail/majordomo-virtusertable | /usr/bin/makemap hash /etc/mail/virtusertable.db");
} else {
  system("/usr/bin/makemap hash /etc/mail/virtusertable.db < /etc/mail/majordomo-virtusertable");
}

foreach $a (@aliases) {
  &majordomo::sendmail_call("delete_alias", $a, $aliases_file);
}

