#!/usr/bin/perl
# $Id: cfmd,v 1.5 2000/09/04 19:37:02 dmetz Exp $
# $crtd:  by  Derald Metzger  on  990625 $
# $cmnt:  cfm database reference full scan
#           Copyright (c) 1998 1999  Derald Metzger
#   This file is part of the cfm pkg (GPL).
# $

#  Load perl modules
use File::Basename;

# Id/set vars
$base = basename($0);
$dst_fil = "";         # Output destination file
($hnam) = (`hostname` =~ /(\w+).*/);
@optsndirs = "-v1";    # Cfm options and additional dirs
$sdate = `date +%y%m%d`; chop $sdate;  # short date yymmdd

$usage = "
NAME
  cfmd  - cfm database relative full scan
SYNOPSIS
  cfmd [-f dst_fil] { . | [cfm_options] [add_dirs] }
DESCR
  This cmd is an options wrapper for cfm.
  cfmd tests files on the executing host that are identified in the
  cfm database using recusive decent thru the cfm database dirs. In
  addition to mismatches the output tells you which database files are
  missing on the executing host. It will not check existing files on
  your host which have not been committed to the cfm database. See
  \`cfmh' for that. Tested dir trees will not include /mnt+ and /export+
  trees by default. You may add such dirs on the cmdline using their 
  mapped names.
OPTIONS
  dst_file  - destination file, eg: /cm/cfm/status/\`hostname`_d
  .         - execute with default opts vice display usage info
  cfm_options  - see cfm, default option is -Q
  add_dirs     - additional dir trees, eg /home/dmetz /app/<app1>
                 cfm will only check files on the host servering them
ALSO
  See also cfm(8), cfmd(8), cfmds, cfmh(8), cfmhs, cfmdb(8), cfmsr(8),
  cfmv(8), cfmvs, rcsintro(1), doc/cfm*/*
AUTHORS
           Copyright (c) 1998 1999  Derald Metzger 
  This file is part of the cfm pkg (GPL).
";

# Get the $dst_fil opt.
if( $ARGV[0] =~ /^-f/ ) {
    $_ = shift;
    /^-f(?:$|(\S+$))/ && do {
        ($1 && ($_ = $1)) || ($_ = shift);
        $dst_fil = $_;
        };
}

# Arg or output usage
@ARGV || die "\n### $0: insufficient args $usage";

# Get cfm opts & additional dirs
!($ARGV[0] eq "\.") && (@optsndirs = (@optsndirs, @ARGV));

# Open destination file if we need it
$dst_fil && (open(CFMDOUT, ">$dst_fil") || die "### Can't open $dst_fil");

# Run cfm with recursion from root down
open(CFMD, "nice cfm -r @optsndirs / 2>&1 |") || die "### Can't open cfm";
while(<CFMD>) {
    /^## $sdate $hnam/ && s/Summary Report/cfmd report/o;
    if(!$dst_fil) { print; }
    else { print CFMDOUT; }
}
close CFMD;
close CFMDOUT;
