#!/bin/sh
#
# update.sh
# BannerFilter, http://phroggy.com/bannerfilter/
#
# This script downloads new updated versions of the four data files
# used by redirector.pl, overwrites the data files in the current
# directory, and makes redirector.pl reload the files.  This script
# requires lynx, wget, curl, or another HTTP downloader to be installed
# and working (see below).
#
# Because new versions of these data files may require BannerFilter to
# be upgraded, this script also checks for the current version of
# BannerFilter and notifies you when an update is available.  You will
# only be notified of required updates; if your old version of
# BannerFilter still works fine with the latest data files, you will
# not be notified if an update is available.
#
# If running as a non-root user, be sure you have write access to the
# current directory.  If you're not running as root or the user Squid is 
# running as, or if you don't have killall, change RELOAD to 0.

# Choose your HTTP downloader:
# CMD="lynx -source"
# CMD="wget -q -O -"
# CMD="curl -s"
CMD="fetch -q -o -"

# Reload with "killall -HUP redirector.pl" if the files have changed?
RELOAD=1
# RELOAD=0

# Directory with the data files
CONFIGDIR="/usr/local/etc/bannerfilter"

# temp dir
TEMPDIR="/tmp"

##########################################################################

# Earliest version of BannerFilter compatible with your data files:
OLD="1.2"

# Read the data files from the current directory, and get the modification
# date from each.  Redirect errors to /dev/null.  If the files don't exist,
# we get blank values.
OLDBANNERS=`grep "^# last modified " $CONFIGDIR/banners.data 2>/dev/null|cut -d " " -f4`
OLDPOPUPS=`grep "^# last modified " $CONFIGDIR/popups.data 2>/dev/null|cut -d " " -f4`
OLDFRAMES=`grep "^# last modified " $CONFIGDIR/frames.data 2>/dev/null|cut -d " " -f4`
OLDEXCEPTIONS=`grep "^# last modified " $CONFIGDIR/exceptions.data 2>/dev/null|cut -d " " -f4`

# Get the earliest version of BannerFilter compatible with the
# new data files, and see whether we need to upgrade
$CMD http://phroggy.com/bannerfilter/version.txt> $TEMPDIR/version.tmp \
|| { echo "Error retrieving version.txt"; exit; }
if ( grep "^version " $TEMPDIR/version.tmp>/dev/null ); then
  NEW=`grep "^version " $TEMPDIR/version.tmp|cut -d " " -f2`
  rm $TEMPDIR/version.tmp
else
  echo "Error: something unexpected in version.txt"
  exit
fi

if [ "$OLD" != "$NEW" ]; then
  echo "The contents of BannerFilter's data files have changed, and you must"
  echo "upgrade to a new version of BannerFilter to be compatible with the"
  echo "changes.  Please upgrade to at least version $NEW at"
  echo "http://phroggy.com/bannerfilter/, and be sure to read the CHANGES."
  exit
fi

# Download all four data files

$CMD http://phroggy.com/bannerfilter/banners.data>$CONFIGDIR/banners.data.tmp \
&& grep "^# banners.data$" $CONFIGDIR/banners.data.tmp>/dev/null \
|| { echo "Error retrieving banners.data"; exit; }

$CMD http://phroggy.com/bannerfilter/popups.data>$CONFIGDIR/popups.data.tmp \
&& grep "^# popups.data$" $CONFIGDIR/popups.data.tmp>/dev/null \
|| { echo "Error retrieving popups.data"; exit; }

$CMD http://phroggy.com/bannerfilter/frames.data>$CONFIGDIR/frames.data.tmp \
&& grep "^# frames.data$" $CONFIGDIR/frames.data.tmp>/dev/null \
|| { echo "Error retrieving frames.data"; exit; }

$CMD http://phroggy.com/bannerfilter/exceptions.data>$CONFIGDIR/exceptions.data.tmp \
&& grep "^# exceptions.data$" $CONFIGDIR/exceptions.data.tmp>/dev/null \
|| { echo "Error retrieving exceptions.data"; exit; }

# Get the modification dates of the files we just downloaded, compare
# them to what we already have, and only overwrite if necessary.
# By using cat to overwrite, we preserve ownership and permissions.

NEWBANNERS=`grep "^# last modified " $CONFIGDIR/banners.data.tmp|cut -d " " -f4`
NEWPOPUPS=`grep "^# last modified " $CONFIGDIR/popups.data.tmp|cut -d " " -f4`
NEWFRAMES=`grep "^# last modified " $CONFIGDIR/frames.data.tmp|cut -d " " -f4`
NEWEXCEPTIONS=`grep "^# last modified " $CONFIGDIR/exceptions.data.tmp|cut -d " " -f4`
CHANGED=0
if [ "$OLDBANNERS" != "$NEWBANNERS" ]; then
  cat $CONFIGDIR/banners.data.tmp > $CONFIGDIR/banners.data
  CHANGED=1
fi
if [ "$OLDPOPUPS" != "$NEWPOPUPS" ]; then
  cat $CONFIGDIR/popups.data.tmp > $CONFIGDIR/popups.data
  CHANGED=1
fi
if [ "$OLDFRAMES" != "$NEWFRAMES" ]; then
  cat $CONFIGDIR/frames.data.tmp > $CONFIGDIR/frames.data
  CHANGED=1
fi
if [ "$OLDEXCEPTIONS" != "$NEWEXCEPTIONS" ]; then
  cat $CONFIGDIR/exceptions.data.tmp > $CONFIGDIR/exceptions.data
  CHANGED=1
fi

# Clean up our temp files
rm $CONFIGDIR/banners.data.tmp
rm $CONFIGDIR/popups.data.tmp
rm $CONFIGDIR/frames.data.tmp
rm $CONFIGDIR/exceptions.data.tmp

# Send all instances of redirector.pl a signal to reload the data files
if [ "$RELOAD" = "1" ] && [ "$CHANGED" = "1" ]; then
  echo -n "Sending signal HUP to all redirector.pl instances... "
  for pid in $(ps wwwax | grep -i redirector.pl | grep -v grep | awk '{print $1}')
  do
    echo -n "$pid "
    kill -HUP $pid
  done
  echo
else
  echo "No changes."
fi
