update
[redirector-ad.git] / redirector
index fd8920c..f72d82d 100755 (executable)
@@ -1,9 +1,45 @@
-#! /bin/sh
+#! /usr/bin/perl
 #
 # $Id$
 
-DIR_AD=/usr/local/squid/etc/ad
+use strict;
+use warnings;
 
-exec /usr/bin/perl -e '$|=1; while (<>) {
-'"`    (cd "$DIR_AD";m4 -P main)|awk '/./{print "s@^http://"$0".*$@http://localhost/cgi-bin/redirector-ad.cgi?"$0"@;"}' `"'
-print; }'
+use re 'eval';
+
+use constant DIR_AD => "/usr/local/squid/etc/ad";
+
+
+chdir DIR_AD or die "chdir ".DIR_AD.": $!";
+select STDOUT;
+$|=1;
+my @got;
+
+RELOAD: {
+       my $patt="";
+       open(M4,"-|","m4 --prefix-builtins --synclines main") or die "m4 run: $!";
+       my %files;
+       while (<M4>) {
+               chomp;
+               next if !$_;
+               do { $files{$1}=undef; next; } if /^#\s*line\s+\d+\s+"(.*)"$/;
+               $patt.="|" if $patt;
+               $patt.=$_."(?{ '";
+               s/'/'."'".'/g;
+               $patt.="$_'; })";
+               }
+       close M4;
+       $patt=qr @^http://($patt).*\n$@;
+
+       while ($_=shift @got || <>) {
+               while (my($file,$old)=each %files) {
+                       my $new=(stat $file)[9];
+                       $files{$file}=$new;
+                       next if !defined $old || $old==$new;
+                       push @got,$_;
+                       redo RELOAD;
+                       }
+               s@$patt@http://localhost/cgi-bin/redirector-ad.cgi?$^R\n@;
+               print;
+               }
+       } # RELOAD