#!/usr/bin/perl
#
# Program to check to see if users exist
# Version 1.00 by Ken Hollis
#
# THIS PROGRAM MUST BE RUN AS SUPERUSER IF YOU WANT IT TO DELETE THE
# DIRECTORIES FOR YOU AUTOMATICALLY FROM THE USER HOME DIRECTORY!
#
# The search method used in this program is very slow.  I will devise
# a different way to get this working, but for the time being, it works.
#
# Update log:
#	01/25/95: Started the program
#	01/26/95: Fixed a bug in directory checking, tuned things up

### Change this if it's different on your system
$HOME_DIR = "/home/users";

### This was "ls.old" on our machine, it may be "ls" on yours.
### This was because of ANSI color graphics in the listing otherwise.
$LS_COMMAND = "/bin/ls -al";

### Cut from byte-order 56 to the end of line
$CUT_PARSE = "cut -b56-";

### Display mode (V = Verbose (for debugging), S = Standard, Q = Quiet)
$DISP_MODE = "V";

### Auto-delete flag (TRUE = Auto-delete non-matched directories, or
### FALSE = Just list them, no deletion)
$AUTO_DELETE = "TRUE";

sub check_options {
    if ($HOME_DIR eq "") {
	print "Sorry, cannot have a null home directory.  If you intended\n";
	print "for this to be root, please put a forward slash (\"/\") in\n";
	print "the \$HOME_DIR field.\n\n";
	die "Configuration Error in HOME_DIR variable\n";
    } elsif ($LS_COMMAND eq "") {
	print "Sorry, there is no directory listing command.  Since there\n";
	print "is no default, the program will break here.\n\n";
	die "Configuration Error in LS_COMMAND variable\n";
    } elsif ($CUT_PARSE eq "") {
	print "Sorry, there is no cut-parsing command.  Make sure your\n";
	print "computer system can handle a parse command like \"cut\".\n";
	print "If it cannot, do NOT use this program!\n\n";
	die "Configuration Error in CUT_PARSE variable\n";
    } elsif ($DISP_MODE eq "") {
	print "Sorry, there is a display mode configuration error.  Since\n";
	print "there is no default value if this is unfilled, you must edit\n";
	print "this variable, as was given in the documentation.\n\n";
	die "Configuration Error in DISP_MODE variable\n";
    } elsif ($AUTO_DELETE eq "") {
	print "Sorry, there is no AUTO_DELETE variable set, or you have not\n";
	print "configured it correctly.  Please go back and check this\n";
	print "variable, as per documentation.\n\n";
	die "Configuration Error in AUTO_DELETE variable\n";
    } else {
	if ($DISP_MODE eq "V") {
	    print "check_options passed\n";
	}
    }
}

sub initialize_dir {
    if ($DISP_MODE eq "V") {
	print "Initializing directory display with ${LS_COMMAND} | ${CUT_PARSE}\n";
    }

    `${LS_COMMAND} ${HOME_DIR} | ${CUT_PARSE} > ./dir_file`;

    if ($DISP_MODE eq "V") {
	print "Done initializing directory file (file is /dir_file)\n";
    } elsif ($DISP_MODE eq "S") {
	print "Done.\n";
    }
}

sub check_loop {
    $f_user = 0;
    $n_user = 0;

    open(DIN, "./dir_file") || die "Cannot open ./dir_file: $!\n";
    while(<DIN>) {
	$umatch = $_;
	chop($umatch);

	### For some reason, umatch gets lost, so we save it here.
	$username = $umatch;

	if (($DISP_MODE eq "V") || ($DISP_MODE eq "S")) {
	    if (($username ne "") && ($username ne ".") && ($username ne "..") &&
		($username ne "lost+found") && ($username ne "news")) {
		print "User $umatch ";
	    }
	}

	$found = "FALSE";

	open(PIN, "/etc/passwd") || die "Cannot find /etc/passwd: $!\n";
	while(<PIN>) {
	    ($uname, $upass, $uuid, $ugid, $ufullname, $udir, $ushell) = split(/:/);

	    $udir =~ s/^${HOME_DIR}\///;

	    if (($umatch eq "") || ($umatch eq ".") || ($umatch eq "..") ||
		($umatch eq "lost+found") || ($umatch eq "news")) {
		last;
	    }

	    if ($udir eq $umatch) {
		$found = "TRUE";
		last;
	    } else {
		$found = "FALSE";
	    }
	}
	close(PIN);

	if (($username ne "") && ($username ne ".") && ($username ne "..") &&
	    ($username ne "lost+found") && ($username ne "news")) {
	    if ($found eq "TRUE") {
		$f_user++;

		if ($DISP_MODE ne "Q") {
		    print "found ";
		}

		if ($DISP_MODE eq "V") {
		    print "[$uuid | $ugid]";
		}

		if ($DISP_MODE ne "Q") {
		    print "\n";
		}
	    } else {
		$n_user++;

		if ($DISP_MODE ne "Q") {
		    print "not found ... ";
		}

		if ($AUTO_DELETE eq "TRUE") {
		    if ($DISP_MODE eq "V") {
			print "rm -rf ${HOME_DIR}/${username}\n";
		    }
		    system ("rm -rf ${HOME_DIR}/${username}");
		    if ($DISP_MODE ne "Q") {
			print "directory removed.";
		    }
		}

		if ($DISP_MODE ne "Q") {
		    print "\n";
		}
	    }
	}
    }
    close(DIR_IN);

    if ($DISP_MODE eq "V") {
	print "Found users: ${f_user}\nNot found: ${n_user}\n";
    }
}

sub main_part {
    &check_options;
    &initialize_dir;
    &check_loop;
}

&main_part;
