#! /usr/bin/perl


###############################################################################
#
# Remove the 'gnats' system from mail
#
$mailfile   = "/etc/aliases";	# provided by 'init'

sub rmmail_die {
	print "- Could not remove 'gnats' system from mail: $!\n";
	return 0;
}

#
# Slurp the entire aliases file
#
if (open(MAIL,"<$mailfile") || rmmail_die) {
	$delim = $/; undef $/;
	$mail = <MAIL>;
	close(MAIL);
	$/ = $delim;
	@mail = split(/\n/,$mail);

#
# Deactivate any entries for 'gnats'
#
for ($index=0; $index <= $#mail; $index++) {
	if ($mail[$index] =~ m/gnats/) {
		$mail[$index] = "#gnats#$mail[$index]";
	}
}

#
# Write out the changed aliases file
#
	$mail = join("\n",@mail);
	$mail.= "\n";
	if (rename("$mailfile","$mailfile.dpkg-old") || rmmail_die) {
		if (open(MAIL,">$mailfile") || rmmail_die) {
			print MAIL $mail;
			close(MAIL);
			chmod 0644, "$mailfile";
		} else {
			rename("$mailfile.dpkg-old","$mailfile");
		}
	}
}
