#! /usr/bin/perl


###############################################################################
#
# Remove the 'gnats' userid from the password file
#
$passwdfile = "/etc/passwd";	# provided by 'init'

sub rmuser_die {
	print "- Could not remove user 'gnats' from password file: $!\n";
	return 0;
}

#
# Slurp the entire password file
#
if (open(PASSWD,"<$passwdfile") || rmuser_die) {
	$delim = $/; undef $/;
	$passwd = <PASSWD>;
	close(PASSWD);
	$/ = $delim;
	@passwd = split(/\n/,$passwd);

#
# Remove any entry for 'gnats'
#
	@passwd = grep(!/^gnats:/,@passwd);

#
# Write out the changed password file
#
	$passwd = join("\n",@passwd);
	$passwd.= "\n";
	if (rename("$passwdfile","$passwdfile.dpkg-old") || rmuser_die) {
		if (open(PASSWD,">$passwdfile") || rmuser_die) {
			print PASSWD $passwd;
			close(PASSWD);
			chmod 0644, "$passwdfile";
		} else {
			rename("$passwdfile.dpkg-old","$passwdfile");
		}
	}
}
