#!/usr/bin/perl
# $Id: cfmhrlis,v 1.3 2000/05/30 14:53:34 dmetz Exp $
# $crtd:  by  Derald Metzger  on  990910 $
# $cmnt:  Output cfm host-relative config file list
#  This list is created by filtering the filenames in the /etc dirtree,
#  adding a filtered list of the config files identified by rpm (rpm -qac),
#  and outputing a unique sort of the result. You may add to the 
#  filters in /ect/cfm/cfm.cf.
#           Copyright (c) 1998 1999  Derald Metzger 
#  This file is part of the cfm pkg (GPL).
# $

################ 000401-dam. Needs reviewed for cfm-1

use File::Find;

# Read in the config file and build the REs for excluding files
$data_flg = 0;      # 0 for header, 1 for /etc, 2 between, 3 rpm idd files
$re_etc = 'm;^$';  # Start the str, empty line reject; semicolon is delimiter
$re_rpm = 'm;^$';  # same
open(CFMCF, "/etc/cfm/cfm.cf") || die "### Failed: open /etc/cfm/cfm.cf";
while(<CFMCF>) {
    /^#- section/ && do { $data_flg++; next; };
	/^#|^$/ && next;
	($data_flg == 1) && do { $re_etc = "$re_etc|$_"; next; };
	($data_flg == 2) && do { $re_rpm = "$re_rpm|$_"; next; };
}
close CFMCF;
$re_etc = "$re_etc;ox";
$re_rpm = "$re_rpm;ox";

# Get and filter the /etc dirtree list.
find(\&wanted,"/etc");
sub wanted { 
###########   Beg linux internal exclude for /etc dir tree  ############
#   Should use full filename but I don't have the File::Find doc.
	m;                # begin RE
#  Assorted backup files
	~$                # emacs
	| \.OLD$          # ?
	| \.rpmsave$      # rpm
	| \.rpmorig$      # rpm
	| ^group-$        # system processing
#  Miscellaneous
	| \.db$           # database files, control the soruce instead
	| ^ioctl\.save$   # console settings 
	| ^issue$         # dynamic, created by rc.local
	| ^issue\.net     # dynamic, created by rc.local
	| \.lock$         # lock files are dynamic
	| ^ld\.so\.cache$ # control the .conf
	| ^mtab$          # dynamic, mount table
	| ^passwd-$       # system processing
	| ^shadow-$       # system processing
	;ox && next;      # close RE
    eval $re_etc && next;  # extensions from /etc/cfm/cfm.cf
###########   End linux internal exclude for /etc dir tree  ############
	( -l || -f ) && push @fil_lis, "$File::Find::name\n";
}

# Get and filter the rpm identified config files list
open(RPMQAC, "rpm -qac |") or die "### \`rpm -qac failed'";
while(<RPMQAC>) {
	eval $re_rpm && next;      # extensions from /etc/cfm/cfm.cf
	push @fil_lis, $_;
}
close RPMQAC;

# Output sorted unique list
$tmp = ";";
for (sort @fil_lis) { /$tmp/ || print; $tmp = $_; }
