#!/bin/sh
# Update new blocklists and start/stop/restart PeerGuardian
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# testdescription
#
echo "moblock: checking for new block lists..."
TESTHOST=www.bluetack.co.uk

wget --timeout=5 --dns-timeout=5 -O -  $TESTHOST  >/dev/null
if [ $? -ne 0 ]; then
       	echo no connection to $TESTHOST, updating later.;
       	exit 0;
fi

#CONFIGURATION
# Make sure PG_SPOOL points to the directory where
# you want to put your downloaded blocklists.
PG_SPOOL=/var/spool/moblock
# Remove the lists you don't want to download and
# use from BLOCKLISTS.
BLOCKLISTS="ads-trackers-and-bad-pr0n bogon dshield hijacked level1 level2 Microsoft spider spyware templist"
BLOCKLISTTXT=""
#PG_CONF=/etc/PG.conf
#PG_LOG=/var/log/PG.log
PG_LIST=/etc/moblock/guarding.p2p
#The URL where the blocklists reside
URL=http://www.bluetack.co.uk/config
#The format of the lists to download
SUFFIX=gz
#The format after unpacking
SUFFIX2=txt

endscript () {
date +"------------ "%F" "%X" "%Z" End PeerGuardian Script"
exit $1
}
date +"------------ "%F" "%X" "%Z" Begin PeerGuardian $1"


	cd "$PG_SPOOL"
	# check if blockfiles were updated:
	UPDATED=""
	for i in $BLOCKLISTS ; do
		TIMESTAMP=0
		if [ -e $i.$SUFFIX ] ; then
		TIMESTAMP=`stat --format=%y $i.$SUFFIX`
		echo "File $i.$SUFFIX last updated $TIMESTAMP"
		TIMESTAMP=`stat --format=%Y $i.$SUFFIX`
		fi
		wget -N $URL/$i.$SUFFIX
		if [ `stat --format=%Y $i.$SUFFIX` -gt $TIMESTAMP ] ; then
		UPDATED=$i
		fi
	done
	for i in $BLOCKLISTTXT ; do
		TIMESTAMP=0
		if [ -e $i.$SUFFIX2 ] ; then
		TIMESTAMP=`stat --format=%y $i.$SUFFIX2`
		echo "File $i.$SUFFIX2 last updated $TIMESTAMP"
		TIMESTAMP=`stat --format=%Y $i.$SUFFIX2`
		fi
		wget -N $URL/$i.$SUFFIX2
		if [ `stat --format=%Y $i.$SUFFIX2` -gt $TIMESTAMP ] ; then
		UPDATED=$i
		fi
	done
	
	
	# if none of the blockfiles were updated:
	#if [ -z $UPDATED ] ; then
	#echo "No blocklists needed updating."
	#endscript 0
	#fi

set -e	
	# if any blockfiles were updated:
	for i in $BLOCKLISTS ; do
	gunzip -c $i.$SUFFIX > $i.$SUFFIX2
	done
	cat *.txt > merged.p2b.p2p
	for i in $BLOCKLISTS ; do
	rm $i.$SUFFIX2
	done
	# uncomment below to unblock Yahoo! Mail and whatever
	# else needs unblocking here. Do this also in the
	# restart section.
	#grep -v -i "yahoo\!" merged.p2b.p2p | grep -v -i "spite media" | grep -v "Trendstep Ltd" > merged.p2b.p2p.tmp
	#mv merged.p2b.p2p.tmp merged.p2b.p2p
	mv $PG_LIST $PG_LIST.backup
	mv merged.p2b.p2p $PG_LIST
	/etc/init.d/`basename $0` reload
exit 0
