#!/usr/bin/perl -w

use Fcntl qw(:flock);

$log = shift || "/home/fex/spool/fexsrv.log";

$ignore = join('|',qw(
  (CONNECT|CONTINUE).*(crawl|msnbot|obertux)
  DISCONNECT:.no.HTTP.request
  GET.*(favicon|robots.txt)
  User-Agent:.*(FeedFetcher|\w+bot|bot/|Website Watcher|crawler|spider|searchme|Yandex)
  From:.*(msnbot|yandex)
  Referer:.*sex.*stream
  Referer:.*stream.*sex
  X-.*prefetch
));
$ignore = "(^|\n)($ignore)";

@weed = qw(
  .*keep-alive
  .*no-cache
  Connection:
  Cache-Control:
  Host:
  TE:
  UA-CPU:
  Pragma:
  Via:
  if-modified-since
  X-Wap-Profile
  X-Nokia 
  X-OperaMini
  X-BlueCoat-Via
);

$/ = ''; 
  
if (-t STDIN) {
  open L,$log or die "$0: $log - $!\n";
  seek L,0,2;
} else {
  *L = *STDIN;
}

for (;;) {
  # test if fexsrv is writing to fexsrv.log
  if (-t STDIN) {
    flock L,LOCK_EX;
    flock L,LOCK_UN;
  }
  while (<L>) {
    next if /$ignore/i;
    s/[\x00-\x08\x0B-\x1F\x1F\x80-\x9F]/_/g;
    s/\n*$/\n\n/;
    foreach $weed (@weed) {
      while (s/\n$weed.*\n/\n/i) {}
    }
    if (/Content-Length: (\d+)/i) {
      $d = $1;
      while ($d =~ s/(\d)(\d\d\d\b)/$1,$2/) {};
      s/Content-Length: \d+/Content-Length: $d/i;
    }
    print;
  }
  sleep 1;
}
