| 1 | #!/usr/bin/perl |
| 2 | ##--------------------------------------------------------------------------## |
| 3 | ## File: |
| 4 | ## $Id: apply-config,v 1.15 2002/10/17 03:14:31 ehood Exp $ |
| 5 | ## Description: |
| 6 | ## Processes all '.in' template files. |
| 7 | ##--------------------------------------------------------------------------## |
| 8 | ## Copyright (C) 2002 Earl Hood <earl@earlhood.com> |
| 9 | ## |
| 10 | ## This program is free software; you can redistribute it and/or modify |
| 11 | ## it under the terms of the GNU General Public License as published by |
| 12 | ## the Free Software Foundation; either version 2 of the License, or |
| 13 | ## (at your option) any later version. |
| 14 | ## |
| 15 | ## This program is distributed in the hope that it will be useful, |
| 16 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | ## GNU General Public License for more details. |
| 19 | ## |
| 20 | ## You should have received a copy of the GNU General Public License |
| 21 | ## along with this program; if not, write to the Free Software |
| 22 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 23 | ## 02111-1307, USA |
| 24 | ##--------------------------------------------------------------------------## |
| 25 | |
| 26 | package MHArc::apply_config; |
| 27 | |
| 28 | ##--------------------------------------------------------------------------## |
| 29 | # <x-boot-strap> |
| 30 | BEGIN { die qq/CGI use FORBIDDEN!\n/ if (defined($ENV{'GATEWAY_INTERFACE'})); } |
| 31 | my $Dir; BEGIN { $Dir = `dirname $0`; chomp $Dir; } |
| 32 | use lib "$Dir/../lib"; # Add relative lib to search path |
| 33 | # </x-boot-strap> |
| 34 | ##--------------------------------------------------------------------------## |
| 35 | # <x-config> |
| 36 | use MHArc::Config; |
| 37 | my $config = MHArc::Config->load("$Dir/../lib/config.sh"); |
| 38 | # </x-config> |
| 39 | ##--------------------------------------------------------------------------## |
| 40 | |
| 41 | use Getopt::Long; |
| 42 | use File::Find; |
| 43 | use MHArc::Util qw(run_prg usage); |
| 44 | |
| 45 | my $clean = 0; |
| 46 | my $distclean = 0; |
| 47 | my $debug = 0; |
| 48 | my $noact = 0; |
| 49 | my $verbose = 0; |
| 50 | |
| 51 | my $clstatus = GetOptions( |
| 52 | "clean!" => \$clean, |
| 53 | "distclean!" => \$distclean, |
| 54 | "debug!" => \$debug, |
| 55 | "n!" => \$noact, |
| 56 | "verbose!" => \$verbose, |
| 57 | |
| 58 | "help" => \$help, |
| 59 | "man" => \$man |
| 60 | ); |
| 61 | usage(0) unless $clstatus; |
| 62 | usage(1) if $help; |
| 63 | usage(2) if $man; |
| 64 | |
| 65 | $verbose = 1 if $debug || $noact; |
| 66 | $clean = 1 if $distclean; |
| 67 | $MHArc::Util::ECHO_CMDS = 1 if ($verbose); |
| 68 | $MHArc::Util::ECHO_ONLY = 1 if ($noact); |
| 69 | |
| 70 | my %done = ( ); |
| 71 | |
| 72 | if ($debug) { |
| 73 | $config->dump_config(\*STDERR); |
| 74 | } |
| 75 | |
| 76 | sub wanted { |
| 77 | if ($_ =~ /config\.sh$/ || |
| 78 | $_ =~ /config\.sh\.in$/ || |
| 79 | $_ =~ /config\.sh\.dist$/ || |
| 80 | $_ =~ /config\.sh\.in\.dist$/) { |
| 81 | print qq/Skipping config file "$File::Find::name"\n/ if $debug; |
| 82 | return; |
| 83 | } |
| 84 | if (-d $_ && ($_ =~ /RCS$/ || $_ =~ /CVS$/ || $_ =~ /SCCS$/)) { |
| 85 | print qq/Pruning "$File::Find::name"\n/ if $debug; |
| 86 | $File::Find::prune = 1; |
| 87 | return; |
| 88 | } |
| 89 | if (-d $_ || !/\.in(?:\.dist)?$/o) { |
| 90 | print qq/Ignoring "$File::Find::name"\n/ if $debug; |
| 91 | return; |
| 92 | } |
| 93 | if ($done{$File::Find::name}) { |
| 94 | print qq/Skipping "$File::Find::name", already processed.\n/ |
| 95 | if $debug; |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | my $file = $_; |
| 100 | print qq/Checking "$File::Find::name"...\n/ if $debug; |
| 101 | if ($file =~ s/\.dist$//) { |
| 102 | if (!$clean) { |
| 103 | if (! -e $file) { |
| 104 | run_prg('/bin/cp', $_, $file); |
| 105 | run_prg('/bin/chmod', 'u+w', $file); |
| 106 | } |
| 107 | } |
| 108 | $done{$File::Find::name} = 1; |
| 109 | $done{"$File::Find::dir/$file"} = 1; |
| 110 | } |
| 111 | |
| 112 | my $file_out = $file; |
| 113 | $file_out =~ s/\.in$//; |
| 114 | |
| 115 | if ($clean) { |
| 116 | run_prg('/bin/rm', $file_out) if (-e $file_out); |
| 117 | run_prg('/bin/rm', $file) if ($distclean && (-e $file) && |
| 118 | (-e "$file.dist")); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | print qq|Processing "$File::Find::dir/$file"\n| if $verbose; |
| 123 | if (!$noact) { |
| 124 | local(*IN, *OUT); |
| 125 | open(IN, $file) || |
| 126 | die qq|ERROR: Unable to open "$File::Find::dir/$file": $!\n|; |
| 127 | open(OUT, ">$file_out") || |
| 128 | die qq|ERROR: Unable to create "$File::Find::dir/$file_out": $!\n|; |
| 129 | |
| 130 | my($line); |
| 131 | while (defined($line = <IN>)) { |
| 132 | $line =~ s/\@\@([^@]*)\@\@/$config->{$1}/g; |
| 133 | print OUT $line; |
| 134 | } |
| 135 | close(IN); |
| 136 | close(OUT); |
| 137 | if (-x $file) { |
| 138 | run_prg('/bin/chmod', 'a+x,go-w', $file_out); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (!@ARGV) { |
| 144 | my $home = $config->{'SW_ROOT'}; |
| 145 | $home = "$Dir/.." unless $home; |
| 146 | push(@ARGV, $home); |
| 147 | |
| 148 | my($dir); |
| 149 | foreach $dir ($config->{'HTML_DIR'}, |
| 150 | $config->{'CGI_DIR'}, |
| 151 | $config->{'MBOX_DIR'}, |
| 152 | $config->{'INFO_DIR'}, |
| 153 | $config->{'MKNMZ_TMPL_DIR'}) { |
| 154 | |
| 155 | next unless $dir =~ /\S/; |
| 156 | if (! -e $dir) { |
| 157 | warn qq/Warning: "$dir" does not exist\n/; |
| 158 | next; |
| 159 | } |
| 160 | $dir =~ s/\/+$//; |
| 161 | if ($dir !~ /^\Q$home\E\//o) { |
| 162 | push(@ARGV, $dir); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | find(\&wanted, @ARGV); |
| 168 | |
| 169 | ##--------------------------------------------------------------------------## |
| 170 | __END__ |
| 171 | |
| 172 | =head1 NAME |
| 173 | |
| 174 | apply-config - Process input template files based upon configuration settings. |
| 175 | |
| 176 | =head1 SYNOPSIS |
| 177 | |
| 178 | apply-config [options] [dir ...] |
| 179 | |
| 180 | =head1 DESCRIPTION |
| 181 | |
| 182 | This program processes input template files and expands variables |
| 183 | referenced to values specified in C<E<lt>mharc-rootE<gt>/lib/config.sh>. |
| 184 | |
| 185 | Template files are designated by the C<.in> filename extentions. For a |
| 186 | given file, if C<I<file>.in.dist> exists and C<I<file>.in> does not, |
| 187 | C<I<file>.in.dist> will be copied to C<I<file>.in> before processing. |
| 188 | |
| 189 | Variable references in template files are denoted as follows: |
| 190 | |
| 191 | @@VARIABLE_NAME@@ |
| 192 | |
| 193 | If the specified variable name is defined, the reference will be |
| 194 | replaced with the empty string. |
| 195 | |
| 196 | =head1 OPTIONS |
| 197 | |
| 198 | Any non-option arguments are treated as directories to recursively |
| 199 | scan for template files. If no directories are specified, then |
| 200 | C<$SW_ROOT> is used as defined in C<E<lt>mharc-rootE<gt>/lib/config.sh>. |
| 201 | |
| 202 | =over |
| 203 | |
| 204 | =item -clean |
| 205 | |
| 206 | Remove all files that have a C<.in> version. This option is useful |
| 207 | to clean up all files generated from templates. |
| 208 | |
| 209 | =item -distclean |
| 210 | |
| 211 | Remove all files that have a C<.in> version and remove all C<.in> |
| 212 | files that have a C<.in.dist> version. This option is useful to |
| 213 | clean up all files for generating a distribution bundle. |
| 214 | |
| 215 | B<WARNING:> Use this option with care since it will delete all |
| 216 | customized versions of C<.in> templates. This option is mainly |
| 217 | for use by mharc developers. |
| 218 | |
| 219 | =item -debug |
| 220 | |
| 221 | Print out alot of information on what is going on. This options |
| 222 | prints out more information than C<-verbose>. |
| 223 | |
| 224 | =item -n |
| 225 | |
| 226 | Just echo what would be done, but do not do it. |
| 227 | |
| 228 | =item -verbose |
| 229 | |
| 230 | Echo out status on any operation that modifies files. |
| 231 | |
| 232 | =back |
| 233 | |
| 234 | =head1 FILES |
| 235 | |
| 236 | =over |
| 237 | |
| 238 | =item C<E<lt>mharc-rootE<gt>/lib/config.sh> |
| 239 | |
| 240 | Configuration file defining variables values. |
| 241 | |
| 242 | =back |
| 243 | |
| 244 | =head1 VERSION |
| 245 | |
| 246 | C<$Id: apply-config,v 1.15 2002/10/17 03:14:31 ehood Exp $> |
| 247 | |
| 248 | =head1 AUTHOR |
| 249 | |
| 250 | Earl Hood, earl@earlhood.com |
| 251 | |
| 252 | This program is part of the mharc archiving system and comes with |
| 253 | ABSOLUTELY NO WARRANTY and may be copied only under the terms of |
| 254 | the GNU General Public License, which may be found in the mhArc |
| 255 | distribution. |
| 256 | |
| 257 | =cut |
| 258 | |