#! /usr/bin/perl


###############################################################################
#
# Add the 'gnats' userid into the password file
#
$gnatsid    = 41;														# provided by 'init'
$gnatsgid   = 41;														# provided by 'init'
$passwdfile = "/etc/passwd";											# provided by 'init'
$gnatshome  = "/var/lib/gnats/gnats-db";								# provided by 'init'
$gnatsentry = "gnats:*:$gnatsid:$gnatsgid:gnats:$gnatshome:/bin/sh";	# provided by 'init'


sub adduser_die {
	die "Cannot add user 'gnats' to password file: $!\n";
}

#
# 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 "* Multiple listings of the 'gnats' userid were found in '$passwdfile'\n";
	$waitcr = 1;
}
if ($pwfound > 0) {
	if (!grep(/^gnats.*:\Q$gnatshome\E:/,@passwd)) {
		print "* A 'gnats' user with incorrect home directory was found.  The entry should\n";
		print "  look like: $gnatsentry\n";
		$waitcr = 1;
	}
	if (!grep(/^gnats:[^:]*:$gnatsid:$gnatsgid:/,@passwd)) {
		print "* A 'gnats' user with incorrect uid/gid was found.  The entry should look\n";
		print "  like: $gnatsentry\n";
		$waitcr = 1;
	}
}
if ($pwfound == 0) {
	print "* Your password file does not contain a userid for the 'gnats' user.\n";
	print "  You must either upgrade your 'base-passwd' package or add the following\n";
	print "  line to your /etc/passwd file:\n";
	print "  $gnatsentry\n";
	$waitcr = 1;
}

#
# Notify the user of what must be done
#
if (!$pwfound || $pwgstar) {
	print "- The 'gnats' userid has no password and thus will not allow login.\n";
	print "  As 'root' you must edit the password file and change 'gnats:*:'\n";
	print "  to 'gnats::'.  Only then can you login as 'gnats' and set a password.\n";
}
