toolbox/update-all-config.pl: quiet means quiet
[girocco.git] / make-lighttpd-conf.sh
blob26c261a898ca9413dd47a6399acc70e4839fd866
1 #!/bin/sh
3 # This script uses the current values of Girocco::Config to
4 # convert lighttpd.conf.in into lighttpd.conf.
6 # It is run automatically by "make" or "make lighttpd.conf" but
7 # may be run separately if desired.
9 set -e
11 . ./shlib.sh
13 trap "rm -f 'lighttpd.conf.$$'" EXIT
14 trap 'exit 130' INT
15 trap 'exit 143' TERM
17 # Include custom configuration, if any
18 [ ! -e config.sh ] || [ ! -f config.sh ] || [ ! -r config.sh ] || . ./config.sh
20 __girocco_conf="$GIROCCO_CONF"
21 [ -n "$__girocco_conf" ] || __girocco_conf="Girocco::Config"
22 perl -I"$PWD" -M"$__girocco_conf" -MGirocco::Validator -- - lighttpd.conf.in >lighttpd.conf.$$ <<'EOT'
23 #line 24 "make-lighttpd-conf.sh"
24 use strict;
25 use warnings;
27 BEGIN { # hack to make var_online_cpus available now
28 $INC{'Girocco/Config.pm'} = 1;
29 require Girocco::Util;
30 my $online_cpus = Girocco::Util::online_cpus();
31 defined($online_cpus) && $online_cpus >= 1 or $online_cpus = 1;
32 eval '$Girocco::Config::var_online_cpus = '.$online_cpus.';';
35 my $frombegin = '# ---- BEGIN LINES TO DUPLICATE ----';
36 my $fromend = '# ---- END LINES TO DUPLICATE ----';
37 my $tobegin = '# ---- BEGIN DUPLICATE LINES ----';
38 my $toend = '# ---- END DUPLICATE LINES ----';
40 open IN, '<', $ARGV[0] or die "could not open $ARGV[0] for reading: $!\n";
41 my $input;
43 local $/;
44 $input = <IN>;
46 close IN;
47 $input =~ s/^##.*(?:\n|$)//gm;
48 my $replifat = sub {
49 my ($first, $not, $var, $lines, $last) = @_;
50 no warnings;
51 my $test = $var =~ /^\d+$/ ? 0+$var : eval('$Girocco::Config::'.$var);
52 $not and $test = !$test;
53 !$test and $lines =~ s/^/#/gm;
54 return '#'.$first.$lines.'#'.$last;
56 my $resv = sub {
57 my ($first, $extra, $eol) = @_;
58 my $prefix = "";
59 $prefix = $first if $first =~ /^[ \t]*(?:[#].*)?$/;
60 $eol = $extra."\n" if $eol ne "";
61 return $first.join($extra."\n".$prefix,map('(?<!\.'.$_.')',
62 sort(keys(%Girocco::Config::reserved_suffixes)))).$eol;
64 my $cmteol = qr/[ \t]*(?:[#][^\n]*)?\n/;
65 1 while $input =~ s/^([ \t]*\@\@if\((!?)([a-zA-Z][a-zA-Z0-9_]*|\d+)\)\@\@(?>$cmteol)?)(.*?)
66 ((?<=\n)[ \t]*\@\@endif(?:\(\2\3\))?\@\@(?>$cmteol)?)/&$replifat($1, $2, $3, $4, $5)/gsmex;
68 no warnings;
69 $input =~ s/(\@\@([a-zA-Z][a-zA-Z0-9_]*)\@\@)/eval
70 "\$Girocco::Config::$2 ne ''?\$Girocco::Config::$2:\$1"/gsex;
72 $input =~ s/^(.*)\@\@reserved\(([^)]*)\)\@\@((?>$cmteol)?)/&$resv($1, $2, $3)/gme;
73 if ($input =~ /(?:^|\n)$frombegin[^\n]*\n(.*)(?<=\n)$fromend/s) {
74 my $dupelines = $1;
75 if ($input =~ /^((?:.+\n)?$tobegin[^\n]*\n).*((?<=\n)$toend.*)$/s) {
76 $input = $1 . $dupelines . $2;
79 printf "%s", $input;
80 EOT
81 mv -f lighttpd.conf.$$ lighttpd.conf