#!/usr/bin/perl
#
# updatelists.pl - Update the list files kept in the same
# 		   directory as the syssumm.format file
#
# Synopsis:
#
#	updatelists.pl [ -d ]
#
# On the main page output from sysquery.pl, there are 4 lists.
# Rather than hard-code these lists, we'll read them from 4
# list files kept in the same directory as the format file.
# 
# The purpose of this Perl script is to initially build the 
# list files.  Then these files will be maintained by incoming.pl
# as new profiles are added to the system.  You can also use 
# this script at any time to rebuild the lists.
# 
# This script, of necessity, shares alot of code with install.pl.
# 
# ==============================================================
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 
# 2 of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be 
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
# 
# Original source:
#	Copyright (c) 1999, 2000 SAIC (Bruce W. Mohler)
# 
# Contributed source:
#	Copyright (c) 1999, 2000 <author>
# 
# ==============================================================
# 
# $Header: /export/home/mohlerb/syssumm/web.server/RCS/updatelists.pl,v 1.4 2000/04/04 03:30:41 mohlerb Exp mohlerb $
# 
# $Revision: 1.4 $
#
# $Log: updatelists.pl,v $
# Revision 1.4  2000/04/04 03:30:41  mohlerb
# changes from Frank Crawford including:
# Patch to add the current path to modules lookup and
# change of names for LoadProfile/UnloadProfile to
# LoadSummary/UnloadSummary
#
# Revision 1.3  2000/03/16 23:01:10  mohlerb
# changes from Bruce Mohler including:
# changes to support re-write LoadConfigurationFile function;
# note that the CreateConfigurationFile function no longer
# exists; a single argument to LoadConfigurationFile function
# (boolean) which states whether to try to fix problems if
# one or more configuration tags are missing; this is only
# true in install.pl, false for all other scripts; therefore
# if tags are added to the configuration file in later versions,
# it will automatically prompt for new values and add them to
# the file
#
# Revision 1.2  1999/08/06 21:54:49  mohlerb
# changes from Paul Farrall including:
# changed default path to Perl to /usr/bin
# removed line $FilePermissions = 0600;. All calls to
# $FilePermissions in this script happen inside subroutines
# in the common.pm.  This line isn't doing anything
#
# Revision 1.1  1999/07/02 16:06:51  mohlerb
# Initial revision
#

require 5.003;

use Getopt::Std;
use FileHandle;
BEGIN {
	unless (($DirName = $0) =~ s:/[^/]*$::) {
		$DirName = '.';
	}
}
use lib $DirName;
use common;

###########################################################################
# Global Variables
###########################################################################

# switches set on the command line
$Debug = 0;
$RunAnyway = 0;

# 
# At run-time, certain global variables are defined from a
# persistent configuration file by the function LoadConfigurationFile
# in common.pm.  Refer to the source in that Perl module to 
# determine which variables are defined.
# 
# In addition, certain variables are exported into the calling
# scripts main:: name space including the syssumm home directory,
# uid, and gid.
# 


###########################################################################
# Subroutines
###########################################################################

sub ProcessCommandLine
{
	if ( getopts('df') ) {
		if ( $opt_d ) { $Debug = 1; }
		if ( $opt_f ) { $RunAnyway = 1; }
	}
	else {
		die "There were problems processing the command line.";
	}

	return;
}


#
# Make sure we're running as root
# 

sub RunAsRoot
{
	if ( $< != 0 ) {		# running as uid 0 (zero)?
		print STDERR "You're not running as root ";
		if ( $RunAnyway ) {
			print STDERR "but I guess you know what you're doing...\n\n";
		}
		else {
			print STDERR "so I'm going to terminate...\n";
			print STDERR "If you're really want to run this script ";
			print STDERR "as someone other than root,";
			print STDERR "use the \'-f\' command line option\n";
			exit(1);
		}
	}

	# let's set the path just in case...
	$ENV{PATH} = "/bin:/usr/bin";

	return;
}


# 
# This function will rebuild the OS and vendor lists used
# in the form output by sysquery.pl.  It really doesn't
# care whether the lists already exist but will overwrite
# whatever exists with what is in the current set of profiles.
# 
# Note that when we arrive in the $SyssummDirectory, there
# will be 'n' data files (including the format file) and
# 1 sub-directory for each existing profile.  We only care
# about the sub-directories.
# 

sub RebuildLists
{	my $PathToSyssummDirectory = "$DocrootDirectory/$SyssummDirectory";

	# get list of "files" and list directories
	opendir SYSSUMMDIR, "$PathToSyssummDirectory" or
		die "Couldn't read directory $PathToSyssummDirectory: $!";
	my @AllFiles = readdir SYSSUMMDIR;
	closedir(SYSSUMMDIR);

	foreach $Directory (sort @AllFiles) {
		# bypass "." and ".."
		next if $Directory =~ /^\./;
		next if $Directory eq "lost+found";
		my $PathToProfileDirectory = 
			"$PathToSyssummDirectory/$Directory";
		if ( -d "$PathToProfileDirectory" ) {
			LoadSummary($Directory);
			UpdateLists();
			UnloadSummary();
		}
	}

	return;
}


###########################################################################
# Mainline
###########################################################################

# strip path off of script name
$0 = (@discard = split(/\//, $0))[$#discard];

STDOUT->autoflush(1);			# from Perl Cookbook section 7.12

ProcessCommandLine();
RunAsRoot();
LoadConfigurationFile(0);
RebuildLists();

###########################################################################
# We're done!
###########################################################################
exit(0);

