#!/usr/bin/env perl
#
# Script to remove obsolete futil files from /usr/local/bin
#
# This script can be run manually via
#
#	/path/to/clean_usr_local.pl {check|clean}
# or
#	ups check futil [-v]	# check the files, do NOT delete
#	ups clean futil [-v]	# actually DELETE the files
#
################################################################################
#
#


$string = $ARGV[0];
$VERBOSE = 1 if ( $ENV{UPS_VERBOSE} );

if ( $string eq "clean" ) {
	$verb = "clean";
	$Verb = "Clean";
	$doDelete = 1;
} elsif ( $string eq "check" ) {
	$verb = "check";
	$Verb = "Check";
	$doDelete = 0;
	$VERBOSE = 1;
} else {
	print STDOUT ("Usage:\n\t$0 {clean|check}\n");
	exit 1;
}

$verbing = $verb . "ing";
$verbup  = $verb . "up";
$Verbing = $Verb . "ing";
$Verbup  = $Verb . "up";

print STDOUT ("Now $verbing obsolete futil files...\n");

init_file_list();


%FOUND    = {};
%DELETED  = {};
%FAILED   = {};
$anyFailures = 0;

foreach $dir ( @DIRS ) {
    print STDOUT ("$Verbing $dir... ");

    $FOUND{"$dir"} = [];
    $DELETED{"$dir"} = [];
    $FAILED{"$dir"} = [];

    foreach $file ( @{$FILES{"$dir"}} ) {
	$fileSpec = $dir . "/" . "$file";
	if ( -e $fileSpec ) {
	    push(@{$FOUND{"$dir"}}, $fileSpec);
	    if ( $doDelete ) {
		if ( unlink($fileSpec) ) {
		    push(@{$DELETED{"$dir"}}, $fileSpec);
		    print STDOUT ("\n\tremoved $fileSpec") if ( $VERBOSE );
		} else {
		    push(@{$FAILED{"$dir"}}, $fileSpec);
		    $anyFailures = 1;
		    print STDOUT ("\n\tFAILED to remove $fileSpec") if ( $VERBOSE );
		}
	    } else {
		print STDOUT ("\n\t would have removed $fileSpec") if ( $VERBOSE );
	    }
	}
    }

#
#   Tally the results:
#
    $nFound   = $#{@{$FOUND{"$dir"}}} + 1;		# array offset
    $nDeleted = $#{@{$DELETED{"$dir"}}} + 1;		# array offset
    $nFailed  = $#{@{$FAILED{"$dir"}}} + 1;		# array offset
#
#   Sanity check: they should add up (if we really removed files)
#
    if ( $doDelete && (($nDeleted + $nFailed) != $nFound) ) {
	print STDERR ("Sanity check failed! nFound = $nFound, nDeleted = $nDeleted, nFailed = $nFailed.\n");
    }

    print STDOUT ("\n") if ( $VERBOSE );
    print STDOUT ("Found $nFound files, successfully removed $nDeleted files.\n") if ( $doDelete );
}

#
# Give the list of files removed if we wanted verbosity.
#

if ( $VERBOSE && $doDelete ) {
    print STDOUT ("\nThe following files have been removed from your system:\n");
    foreach $dir ( @DIRS ) {
	foreach $fileSpec ( @{$DELETED{"$dir"}} ) {
	    print STDOUT ("\t$fileSpec\n");
	}
    }
}

#
# Give the list of failures in any case.
#
if ( $anyFailures ) {
    print STDOUT ("\nThe following files WERE NOT SUCCESSFULLY REMOVED from your system, please handle manually!\n");
    foreach $dir ( @DIRS ) {
	foreach $fileSpec( @{$FAILED{"$dir"}} ) {
	    print STDOUT ("\t$fileSpec\n");
	}
    }
}


print STDOUT ("futil $verbup complete on this node.\n");



################################################################################
#
# Initialize @DIRS (list of directories in which we'll be removing files)
# and %FILES (hash of arrays of files-per-directory we want to remove).
#
sub init_file_list() {

#
#   More complicated than it needs to be, for future potential expansion
#   (and also because I'm cloning from an already-written and debugged
#   script!)
#
    @DIRS = (
	"/usr/local/bin"
	);

    %FILES = {};

    $FILES{"/usr/local/bin"} = [
#
#	from the futil product:
#
	"Info",
	"findcpp",
	"flpr",
	"getlock",
	"gzip",
	"lesskey",
	"obtain",
	"subm",
	"telephone",
	"zcat",
	"zforce",
	"znew",
	"a2ps",
	"flpk",
	"fpasswd",
	"gunzip",
	"less",
	"lsof",
	"psnup",
	"subm.cron",
	"vt100",
	"zcmp",
	"zgrep",
	"cwi",
	"flpq",
	"getdate",
	"gzexe",
	"less.help",
	"namefix",
	"stock",
	"tele",
	"zap",
	"zdiff",
	"zmore",
#
#	from the funkern product:
#
	"addpath",
	"dropit",
	"grpmembers",
	"grpnumber",
	"logdata",
	"loggid",
	"loguid",
	"delpath",
	"funame",
	"grpname",
	"logage",
	"logdir",
	"logshell",
	"loguser"
	];

}

