#!/usr/bin/perl
#
# Copyright (C) 2001 Caldera International, Inc.  All Rights Reserved.
#
# domaindelete
#
# delete a domain and all of its users and aliases
#

$DEBUG	= 0;				# must be first statement

BEGIN {
	use File::Basename();
	$newdir = File::Basename::dirname($0);
	unshift (@INC, "$newdir");
}

use 		msglib;
use 		msgcyrus;

sub Usage {
	print STDERR msgi18nmsg(1); # usage message
	exit(1);
}

#
# main
#

msgi18ninit(17); # set 17 is msgdomaindelete

$argc = @ARGV;
if ($argc != 1) {
	Usage();
}
$optresult = GetOptions ("domain=s" => \$vdomain);
if(!$optresult) {
	Usage();
}

argcheck("domain", $vdomain);

# Get defaults for LDAP
parse_conf;

# confirm domain exists
$ldap   = Net::LDAP->new($ldaphost) || die msgi18nmsg(2) . "$@";
$result = $ldap->bind("$msgadmindn", password => "$msgadminpw", version => 3);

if ($result->code) {
	#
	# if we've got an error... record it
	#
	print getLDAPerrorString("bind",$result) . "\n";
	exit(1);
}

$mesg = $ldap->search(
		base	=> $ldapsuffix,
		scope	=> "one",
		filter	=> fbuild("(&(objectClass=msgDomain)(ou=%1))", $vdomain),
		attrs	=> ['dn'],
);

$entry = $mesg->entry(0);
if (!$entry) {
	print STDERR msgi18nmsg(5, $vdomain); # --domain=$vdomain does not exist
	$ldap->unbind;
	exit(1);
}

# now check if it is the primary domain.
$masterdomain = getmasterdomain();

if ($vdomain eq $masterdomain) {
	print STDERR msgi18nmsg(6); # Cannot delete the master domain
	$ldap->unbind;
	exit(1);
}

$ret = do_rootread("/etc/postfix/transport", \@lines);
if ($ret == 0) {
	print STDERR msgi18nmsg(8); # Cannot read /etc/postfix/transport
	$ldap->unbind;
	exit(1);
}

# output array
@olines = ();
$oline = 0;

foreach(@lines) {
	if (! (/^$vdomain\s.*$/) ) {
                $olines[$oline++] = "$_";
	}
}
do_rootwrite("/etc/postfix/transport", \@olines);

$cmd = "|$msgbin/msgroot --run=postmap --args=/etc/postfix/transport";
$ret = do_cmd($cmd);
if ($ret == 0) {
        # service action $service $action failed
  	die msgi18nsmsg(23, 1, "postmap", "/etc/postfix/transport");
}

# now fixup the mydestination parameter

$ret = do_rootread("/etc/postfix/main.cf", \@lines);
if ($ret == 0) {
	print STDERR msgi18nmsg(7); # Cannot read /etc/postfix/main.cf
	$ldap->unbind;
	exit(1);
}

# output array
@olines = ();
$oline = 0;

$prev = "";
foreach (@lines) {
	if (/^mydestination\s=\s*/) {
		# four cases first, middle, last, only,
		# we assume middle and last are only possible ones
		if (/, $vdomain\n$/) {
			$_ =~ s/, $vdomain\n/\n/;
		} elsif (/, $vdomain, /) {
			$_ =~ s/, $vdomain, /, /;
		}
	}
	$olines[$oline++] = $_;
}

do_rootwrite("/etc/postfix/main.cf", \@olines);

do_rootserviceaction("postfix", "reload");

# do the users
$mesg = $ldap->search(
		base	=> "ou=$vdomain,$ldapsuffix",
		scope	=> "sub",
		filter	=> fbuild("(objectClass=msgUser)"),
		attrs	=> ['uid'], 
);

if ($mesg->code) {
	print getLDAPerrorString("search",$mesg) . "\n";
	$ldap->unbind;
	exit(1);
}

cyrinit();

foreach $entry ($mesg->entries) {
	$uid = $entry->get_value("uid");
	cyrdeletembox($uid, "localhost", "inbox");
	$delmesg = $ldap->delete($entry);
	if ($delmesg->code) {
		print getLDAPerrorString("delete",$delmesg) . "\n";
		$ldap->unbind;
		exit(1);
	}
}

cyrdone();

# do the groups
$mesg = $ldap->search(
		base	=> "ou=$vdomain,$ldapsuffix",
		scope	=> "sub",
		filter	=> fbuild("(objectClass=msgGroup)"),
		attrs	=> ['uid'], 
);

if ($mesg->code) {
	print getLDAPerrorString("search",$mesg) . "\n";
	$ldap->unbind;
	exit(1);
}

foreach $entry ($mesg->entries) {
	$delmesg = $ldap->delete($entry);
	if ($delmesg->code) {
		print getLDAPerrorString("delete",$delmesg) . "\n";
		$ldap->unbind;
		exit(1);
	}
}

# delete the MSG Users and MSG Groups container nodes
$mesg = $ldap->delete ("ou=MSG Users,ou=$vdomain,$ldapsuffix");

if( $mesg->code ) {
	print getLDAPerrorString("delete",$mesg) . "\n";
	exit(1);
}

$mesg = $ldap->delete ("ou=MSG Groups,ou=$vdomain,$ldapsuffix");

if( $mesg->code ) {
	print getLDAPerrorString("delete",$mesg) . "\n";
	exit(1);
}

# delete the domain node itself
$mesg = $ldap->delete ("ou=$vdomain,$ldapsuffix");

if( $mesg->code ) {
	print getLDAPerrorString("delete",$mesg) . "\n";
	exit(1);
}

$ldap->unbind;

do_rootbackupdb();

exit(0);
