#!/usr/bin/perl ##--------------------------------------------------------------------------## ## File: ## $Id: apply-config,v 1.15 2002/10/17 03:14:31 ehood Exp $ ## Description: ## Processes all '.in' template files. ##--------------------------------------------------------------------------## ## Copyright (C) 2002 Earl Hood ## ## 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. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA ##--------------------------------------------------------------------------## package MHArc::apply_config; ##--------------------------------------------------------------------------## # BEGIN { die qq/CGI use FORBIDDEN!\n/ if (defined($ENV{'GATEWAY_INTERFACE'})); } my $Dir; BEGIN { $Dir = `dirname $0`; chomp $Dir; } use lib "$Dir/../lib"; # Add relative lib to search path # ##--------------------------------------------------------------------------## # use MHArc::Config; my $config = MHArc::Config->load("$Dir/../lib/config.sh"); # ##--------------------------------------------------------------------------## use Getopt::Long; use File::Find; use MHArc::Util qw(run_prg usage); my $clean = 0; my $distclean = 0; my $debug = 0; my $noact = 0; my $verbose = 0; my $clstatus = GetOptions( "clean!" => \$clean, "distclean!" => \$distclean, "debug!" => \$debug, "n!" => \$noact, "verbose!" => \$verbose, "help" => \$help, "man" => \$man ); usage(0) unless $clstatus; usage(1) if $help; usage(2) if $man; $verbose = 1 if $debug || $noact; $clean = 1 if $distclean; $MHArc::Util::ECHO_CMDS = 1 if ($verbose); $MHArc::Util::ECHO_ONLY = 1 if ($noact); my %done = ( ); if ($debug) { $config->dump_config(\*STDERR); } sub wanted { if ($_ =~ /config\.sh$/ || $_ =~ /config\.sh\.in$/ || $_ =~ /config\.sh\.dist$/ || $_ =~ /config\.sh\.in\.dist$/) { print qq/Skipping config file "$File::Find::name"\n/ if $debug; return; } if (-d $_ && ($_ =~ /RCS$/ || $_ =~ /CVS$/ || $_ =~ /SCCS$/)) { print qq/Pruning "$File::Find::name"\n/ if $debug; $File::Find::prune = 1; return; } if (-d $_ || !/\.in(?:\.dist)?$/o) { print qq/Ignoring "$File::Find::name"\n/ if $debug; return; } if ($done{$File::Find::name}) { print qq/Skipping "$File::Find::name", already processed.\n/ if $debug; return; } my $file = $_; print qq/Checking "$File::Find::name"...\n/ if $debug; if ($file =~ s/\.dist$//) { if (!$clean) { if (! -e $file) { run_prg('/bin/cp', $_, $file); run_prg('/bin/chmod', 'u+w', $file); } } $done{$File::Find::name} = 1; $done{"$File::Find::dir/$file"} = 1; } my $file_out = $file; $file_out =~ s/\.in$//; if ($clean) { run_prg('/bin/rm', $file_out) if (-e $file_out); run_prg('/bin/rm', $file) if ($distclean && (-e $file) && (-e "$file.dist")); return; } print qq|Processing "$File::Find::dir/$file"\n| if $verbose; if (!$noact) { local(*IN, *OUT); open(IN, $file) || die qq|ERROR: Unable to open "$File::Find::dir/$file": $!\n|; open(OUT, ">$file_out") || die qq|ERROR: Unable to create "$File::Find::dir/$file_out": $!\n|; my($line); while (defined($line = )) { $line =~ s/\@\@([^@]*)\@\@/$config->{$1}/g; print OUT $line; } close(IN); close(OUT); if (-x $file) { run_prg('/bin/chmod', 'a+x,go-w', $file_out); } } } if (!@ARGV) { my $home = $config->{'SW_ROOT'}; $home = "$Dir/.." unless $home; push(@ARGV, $home); my($dir); foreach $dir ($config->{'HTML_DIR'}, $config->{'CGI_DIR'}, $config->{'MBOX_DIR'}, $config->{'INFO_DIR'}, $config->{'MKNMZ_TMPL_DIR'}) { next unless $dir =~ /\S/; if (! -e $dir) { warn qq/Warning: "$dir" does not exist\n/; next; } $dir =~ s/\/+$//; if ($dir !~ /^\Q$home\E\//o) { push(@ARGV, $dir); } } } find(\&wanted, @ARGV); ##--------------------------------------------------------------------------## __END__ =head1 NAME apply-config - Process input template files based upon configuration settings. =head1 SYNOPSIS apply-config [options] [dir ...] =head1 DESCRIPTION This program processes input template files and expands variables referenced to values specified in Cmharc-rootE/lib/config.sh>. Template files are designated by the C<.in> filename extentions. For a given file, if C.in.dist> exists and C.in> does not, C.in.dist> will be copied to C.in> before processing. Variable references in template files are denoted as follows: @@VARIABLE_NAME@@ If the specified variable name is defined, the reference will be replaced with the empty string. =head1 OPTIONS Any non-option arguments are treated as directories to recursively scan for template files. If no directories are specified, then C<$SW_ROOT> is used as defined in Cmharc-rootE/lib/config.sh>. =over =item -clean Remove all files that have a C<.in> version. This option is useful to clean up all files generated from templates. =item -distclean Remove all files that have a C<.in> version and remove all C<.in> files that have a C<.in.dist> version. This option is useful to clean up all files for generating a distribution bundle. B Use this option with care since it will delete all customized versions of C<.in> templates. This option is mainly for use by mharc developers. =item -debug Print out alot of information on what is going on. This options prints out more information than C<-verbose>. =item -n Just echo what would be done, but do not do it. =item -verbose Echo out status on any operation that modifies files. =back =head1 FILES =over =item Cmharc-rootE/lib/config.sh> Configuration file defining variables values. =back =head1 VERSION C<$Id: apply-config,v 1.15 2002/10/17 03:14:31 ehood Exp $> =head1 AUTHOR Earl Hood, earl@earlhood.com This program is part of the mharc archiving system and comes with ABSOLUTELY NO WARRANTY and may be copied only under the terms of the GNU General Public License, which may be found in the mhArc distribution. =cut