#!/usr/bin/perl
#
# Copyright (C) 2001 Caldera International, Inc.  All Rights Reserved.
#

# this script is not i18n because it has to be stand alone.
$msgpath='lsb-sco.com/msg';
$SAVEDIR_VAR="/var/opt/$msgpath/save";
$SAVEDIR_ETC="/etc/opt/$msgpath/save";

# check for OpenLinux/UnitedLinux
$tmp=`rpm -q OpenLinux > /dev/null 2>&1`;
if ( ! $? ) {
        # Open Linux Found
        $OPENLINUX = 1;
        $UNITEDLINUX = 0;
} else {
        $OPENLINUX = 0;
        $UNITEDLINUX = 1;
}


if ($UNITEDLINUX) {
	$INITDIR = "/etc/rc.d";
	$LDAPDIR = "/etc/openldap";
} else {
	$INITDIR = "/etc/rc.d/init.d";
	$LDAPDIR = "/etc/ldap";
}

sub myprompt {

	my ($prompt) = @_;

	my $result;
	my $value;

	my $ok = 0;
	while ($ok == 0) {
		print("$prompt (Y/N) [Y] ");
		$result = <STDIN>;
		chomp($result);
		if ($result eq "") {
			$ok = 1;
			$value = 1;
			next;
		}
		if ($result =~ /^y/i) {
			$ok = 1;
			$value = 1;
			next;
		}
		if ($result =~ /^n/i) {
			$ok = 1;
			$value = 0;
			next;
		}
		print("Invalid entry, answer Y or N\n");
	}
	return($value);
}

sub update_cyrusconf {

	my $cyrusconf = "$INITDIR/cyrus";

	open(IFD, "$cyrusconf") || return;
	@lines = <IFD>;
	close(IFD);

	open(OFD, ">$cyrusconf.lck") || return;
	$got_group = 0;
	foreach (@lines) {
		if (/# up the file/) {
			$got_group = 1;
		}
		if ($got_group > 0) {
			$got_group = $got_group + 1;
			if ($got_group == 11) {
				$got_group = 0;
			}
			next;
		}
		if (/\/usr\/cyrus\/bin\/master/) {
			$_ =~ s/-l 256 //;
		}
		print OFD "$_";
	}
	close(OFD);
	rename("$cyrusconf.lck", "$cyrusconf");
	chmod(0700, "$cyrusconf");
}

sub update_ldapconf {

	if ($UNITEDLINUX) {
		return;
	}

	my $ldapconf = "/etc/pam_ldap/auth_ldap.conf";

	open(IFD, "$ldapconf") || return;
	@lines = <IFD>;
	close(IFD);

	open(OFD, ">$ldapconf.lck") || return;
	$got_group = 0;
	foreach (@lines) {
		if (/^bind_timelimit/) {
			next;
		}
		print OFD "$_";
	}
	close(OFD);
	rename("$ldapconf.lck", "$ldapconf");
}

sub update_cron() {
	my $cronconf = "/tmp/cron$$";

	# if this fails no crontab exists for root, no need to remove anything
	$ret = system("/usr/bin/crontab -u root -l > $cronconf 2>&1");
	if ($ret != 0) {
		return;
	}

	open(IFD, "$cronconf") || return;
	@lines = <IFD>;
	close(IFD);

	open(OFD, ">$cronconf.new") || return;
	$got_group = 0;
	foreach (@lines) {
		# get rid of those spawning comments so they don't replicate
		if (/^# DO NOT/) {
			next;
		}
		if (/^# \(\/tmp/) {
			next;
		}
		if (/^# \(Cron/) {
			next;
		}
		if (/sievebuild/) {
			next;
		}
		print OFD "$_";
	}
	close(OFD);
	rename("$cronconf.new", "$cronconf");
	if (-z $cronconf) {
		system("/usr/bin/crontab -u root -r");
	} else {
		system("/usr/bin/crontab -u root $cronconf");
	}
	system("/bin/rm -f $cronconf");
}

sub update_apache()
{
	$apacheconf = "/etc/httpd/httpd.conf";

	open(IFD, "$apacheconf") || return;
	@lines = <IFD>;
	close(IFD);

	open(OFD, ">$apacheconf.new") || return;
	$inconf = 0;
	foreach (@lines) {
		if ($inconf) {
			next;
		}
		if (/START SCOoffice Mail Server CONFIGURATION/) {
			$inconf = 1;
			next;
		}
		if (/END SCOoffice Mail Server CONFIGURATION/) {
			$inconf = 0;
			next;
		}
		print OFD "$_";
	}
	close(OFD);
	rename("$apacheconf.new", "$apacheconf");
}

print("WARNING: msguninstall removes the SCOoffice Mail Server from your system.\n");

$result = myprompt("Would you like to restore the LDAP configuration to its old state?");

if ($result == 0) {
	print("LDAP configuration will not be restored\n");
	$ldaprestore = 0;
} else {
	print("LDAP configuration will be cleared\n");
	print("LDAP database will be cleared\n");
	$ldaprestore = 1;
	$ldapremove = 1;
}

if (!$ldapremove) {
	$result = myprompt("Would you like to clear the LDAP database?");

	if ($result == 0) {
		print("LDAP database will not be cleared\n");
		$ldapremove = 0;
	} else {
		print("LDAP database will be cleared\n");
		$ldapremove = 1;
	}
}

$result = myprompt("Are you sure you want to proceed ?");

if ($result == 0) {
	print("Messaging Server removal aborted.\n");
	exit(1);
} 

# Stop postfix and Cyrus
system("$INITDIR/postfix stop");
system("$INITDIR/cyrus stop");

# undo install config changes
update_cyrusconf();
update_ldapconf();

# Remove the RPMS
if ($UNITEDLINUX) {
	@rpms = qw(
		scomsg-mpack
		scomsg
		scomsg-doc
		perl-IMAP-Admin
		perl-ldap
		perl-Convert-ASN1
		perl-Text-Iconv
		perl-Msgcat
		perl-Mcrypt
		ispell-english
		ispell-french
		ispell-german
		ispell-italian
		ispell-spanish
		ispell
		drac
		scomsg-turba
		scomsg-imp
		scomsg-horde
		cyrus-imapd
		scomsg-libtool
	);
} else {
	@rpms = qw(
		scomsg-mpack
		scomsg
		scomsg-doc
		perl-IMAP-Admin
		perl-ldap
		perl-Convert-ASN1
		perl-Text-Iconv
		perl-Msgcat
		ispell-english
		ispell-french
		ispell-german
		ispell-italian
		ispell-spanish
		drac
		imp
		postfix 
		cyrus-imapd
		cyrus-sasl2
		gq
	);
}

# RPMS not removed include:
# horde

foreach (@rpms) {
	$rpmexec = "/bin/rpm -e --allmatches " . "$_";
	system($rpmexec);
}
system ("/bin/rm -rf /var/tmp/scomsg"); 
system ("/bin/rm -rf /var/tmp/scomsg-doc"); 

if ($ldapremove) {
	system("$INITDIR/ldap stop");
	sleep 5;

	# restore the original database files
	if ($UNITEDLINUX) {
		$dir="/var/lib/ldap";
	} else {
		$dir="/var/ldap/openldap-ldbm";
	}
	$d=`basename $dir`; chomp $d;
	system("/bin/rm -rf $dir/*");
	if ( -d "$SAVEDIR_VAR/$d" ) {
		system("cd $SAVEDIR_VAR/$d; find . | cpio -dump $dir"); 
		system("/bin/rm -rf $SAVEDIR_VAR/$d");
	}
	undef($dir); undef($d);

	if ($ldaprestore) {
		system("/bin/cp $SAVEDIR_ETC/ldap.conf $LDAPDIR > /dev/null 2>&1");
		system("/bin/cp $SAVEDIR_ETC/slapd.conf $LDAPDIR > /dev/null 2>&1");
		system("/bin/cp $SAVEDIR_ETC/auth_ldap.conf /etc/pam_ldap > /dev/null 2>&1");
		system("/bin/cp $SAVEDIR_ETC/ldap /etc/sysconfig/daemons > /dev/null 2>&1");
	}

	if (! $UNITEDLINUX) {
		$ret = system('/usr/bin/grep ONBOOT=\"yes\" /etc/sysconfig/daemons/ldap > /dev/null');
	} else {
		$ret = 0;
	}
	if ($ret == 0) {
		system("$INITDIR/ldap start");
	}
}

# remove the vscan user and its home directory.
system("rm -fr ~vscan > /dev/null 2>&1");
system("/usr/sbin/userdel vscan > /dev/null 2>&1");

update_cron();

update_apache();

# remove any last vestiges of the msg server
$tmppath=`dirname $msgpath`;
system("/bin/rm -fr /opt/$msgpath");
system("/bin/rmdir /opt/$tmppath >/dev/null 2>&1") if ($tmppath != '.');

system("/bin/rm -fr /etc/opt/$msgpath");
system("/bin/rmdir /etc/opt/$tmppath >/dev/null 2>&1") if ($tmppath != '.');

# we want to leave /var/opt/$msgpath/save
# if it has anything in it
$filecount = `ls /tmp 2>/dev/null | wc -l`;
if ($filecount == 0) {
	# remove everything
	system("/bin/rm -fr /var/opt/$msgpath");
	system("/bin/rmdir /var/opt/$tmppath >/dev/null 2>&1") if ($tmppath != '.');
} else {
	# remove directories other than save
	system("/bin/rm -fr /var/opt/$msgpath/tmp");
}

if (! $UNITEDLINUX) {
	system("/bin/rm -fr /etc/postfix");
	system("/bin/rm -fr /var/spool/postfix");
}

exit(0);
