#! /bin/csh -f

set usage = "$0 [ -external | -unicamp | -local ]... < INLOG > OUTLOG "

# Reads an httpd access or error log from stdin,
# passes to stdout acesses originating from specified hosts: 
#    -external   outside Unicamp  
#    -unicamp    inside Unicamp but outside IC 
#    -local      inside IC  

set PAT = ""
while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) ) 
  if ( "x$1" == "x-external" ) then
    set PAT = "${PAT}X"
    shift
  else if ( "x$1" == "x-unicamp" ) then
    set PAT = "${PAT}U"
    shift
  else if ( "x$1" == "x-local" ) then
    set PAT = "${PAT}L"
    shift  
  else
    echo "${usage}"
    exit 1
  endif
end

sed \
  -e 's/^/:X /' \
  -e '/^:X [a-zA-Z0-9][a-z_/:A-Z0-9-]* /s/^:X/:L/' \
  -e '/^:X [^ ]*\.dcc\.unicamp\.br /s/^X:/:U/' \
  -e '/^:X 143\.106\./s/^:X/:U/' \
  -e '/^:U 143\.106\.7\./s/^:X/:L/' \
  -e '/^:U 143\.106\.16\./s/^:X/:L/' \
  -e '/^:U 143\.106\.23\./s/^:X/:L/' \
  -e '/^:U 143\.106\.24\./s/^:X/:L/' \
  -e '/^:U 143\.106\.34\./s/^:X/:L/' \
  -e '/^:X [^ ]*\.unicamp\.br /s/^:X/:U/' \
  -e '/^:X [^ ]*\.embrapa\.br /s/^:X/:U/' \
  -e '/^:X [^ ]*\.cti\.br /s/^:X/:U/' \
  -e '/^:X [^ ]*\.softex\.br /s/^:X/:U/' \
  -e '/^:['${PAT}']/\!d' \
  -e 's/^:[A-Z] //'
