#! /usr/bin/perl


###############################################################################
#
# Add the 'gnats' userid into the password file
#
$gnatsid    = 16;													# provided by 'init'
$passwdfile = "/etc/passwd";										# provided by 'init'
$gnatshomere= "\/var\/gnats-db";									# provided by 'init'
$gnatsentry = "gnats:*:$gnatsid:65534:gnats:/var/gnats-db:/bin/sh";	# provided by 'init'

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

#
# Remove any old entry for 'gnats'
#
$pwfound = grep(/^gnats:/,@passwd);
$pwgstar = grep(/^gnats:\*:/,@passwd);
if ($pwfound > 1) {
	print "- WARNING: Multiple listings of the 'gnats' userid were found in 'passwd'\n";
}
if ($pwfound > 0) {
	if (!grep(/^gnats.*:$gnatshomere:/,@passwd)) {
		print "- A 'gnats' userid with incorrect home directory was found\n";
	}
}
if ($pwfound == 0) {
	print "- Your password file does not contain a userid for the 'gnats' user\n";
	print "- You must either upgrade your 'base' package or add the following\n";
	print "  line to your /etc/passwd file:\n\n";
	print "  $gnatsentry\n\n";
}
