+#!/usr/bin/perl
+
+#$sharedir= '/usr/local/share/userv/udptunnelconf';
+$sharedir= '/home/ian/things/userv-utils/udptunnelconf';
+$confdir= '/etc/udptunnel';
+
+sub badusage () { die "usage: udptunnel-reconf [<directory>]\n"; }
+$debug=0;
+
+while ($ARGV[0] =~ m/^-/) {
+ $_= shift @ARGV;
+ last if m/^--$/;
+ if (m/^-d$/) { $debug++; next; }
+ badusage();
+}
+
+if (@ARGV) { $confdir= shift @ARGV; }
+#badusage() if @ARGV;
+
+chdir $confdir or die "$0: $confdir: $!\n";
+
+sub run_m4 ($$$) {
+ my ($wanted, $site, $variable) = @_;
+ $x= "m4 -P -DWANTED=$wanted -DWHVARIABLE=V_$variable -DV_global=global ".
+ "-DV_site=$site ".
+ "-DV_defaults=$sharedir/defaults $sharedir/config.m4";
+ print STDERR $x,"\n" if $debug>=2;
+ open X, "$x |"
+ or die $!;
+ undef $/;
+ $m4out= <X>;
+ $/= "\n";
+ close X; $? and exit -1;
+ $m4out =~ s/^\s+//;
+ $m4out =~ s/\n+/\n/g;
+ $m4out =~ s/\s+$//;
+ print STDERR "$wanted/$variable($site) -> \`$m4out'\n" if $debug>=1;
+ return $m4out;
+}
+
+sub check_junk ($$) {
+ my ($emsg, $site) = @_;
+ $j= run_m4('JUNK',$site,'');
+ $j =~ s/^\# //g;
+ die "$0: $emsg: $j\n" if length $j;
+}
+
+check_junk("error in config",'');
+
+@actives= split /\s+/, run_m4('ACTIVES','','');
+@passives= split /\s+/, run_m4('PASSIVES','','');
+
+foreach $site (@actives, @passives) {
+ check_junk("error in config for site $site",$site);
+}
+
+$_= run_m4('VARIABLE','greenend','command');
+s/\\\n/ /g;
+s/\s+/\n/g;
+print $_,"\n" or die $!;