
#
# AutoHosts.sh
# Author:  Henry Hertz Hobbit
# Version: 1.1.4
# 2009-Nov-22 21:08 UTC
#
# This script file provides a way to automatically upgrade the blocking
# hosts file via UnixUtils.  You MUST install UnixUtils first though.
# See the UtilsSetup.txt file in the UnixUtilsInstallation to do that.
# The alternative is to use the full version of CygWin.  For most
# Windows users CygWin is overkill.  Those that need CygWin know why
# they need it.  They will also need to completely rewrite this script
# for CygWin.
#

CURDIR=`pwd`
UPDATEDHOSTS=0

echo
cd ${SYSTEMDRIVE}
cd /

# Do some primitive load balancing for which host we pull files from
# The last else clause is for when nothing is there yet
if [ -f /etc/LastUsedSMHost.txt ]
then
	rm -f /etc/LastUsedSMHost.txt
	touch /etc/LastUsedHFHost.txt
	DOWNLOADHOST=www.hostsfile.org
else
	if [ -f /etc/LastUsedHFHost.txt ]
	then
		rm -f /etc/LastUsedHFHost.txt
		touch /etc/LastUsedSMHost.txt
		DOWNLOADHOST=www.securemecca.com
	else
		rm -f /etc/LastUsedHFHost.txt
		touch /etc/LastUsedSMHost.txt
		DOWNLOADHOST=www.securemecca.com
	fi
fi

# Do a check to see if we are up to date first
wget -q  ${DOWNLOADHOST}/Downloads/hdate.txt
if [ -f hdate.txt ]
then
	if [ -d etc ]
	then
		if [ -f /etc/hdate.txt ]
		then
			if diff /etc/hdate.txt hdate.txt
			then
				rm -f hdate.txt
				echo THE HOSTS FILE HAS NOT CHANGED
				echo Press Enter to exit
				echo
				read UNUSEDRESPONSE
				exit 0
			fi
		else
			cp -fp hdate.txt /etc/hdate.txt
		fi
	else
		mkdir etc
	fi
	cp -fp hdate.txt etc/hdate.txt
fi
rm -f hdate.txt

if [ -d /Windows/System32/drivers/etc ]
then
	ETCDIR=/Windows/System32/drivers/etc
	cd /Windows/System32/drivers/etc
else
	if [ -d /WinNT/System32/drivers/etc ]
	then
		ETCDIR=/WinNT/System32/drivers/etc
		cd /WinNT/System32/drivers/etc
	else
		echo Your system can NOT be automatically updated.
		echo You will have to do it manually.
		echo
		echo Press Enter to exit
		read UNUSEDRESPONSE
		exit 1
	fi
fi

if [ ! -f hosts.org ]
then
	echo We are making a copy of your current hosts file to hosts.org
	echo This is done only once and is there in case you want to take
	echo the blocking hosts file off.
	echo
	cp hosts hosts.org
	attrib +r hosts.org
fi

rm -fr  AutoHosts.msw
rm -f  AutoHosts.msw.*
if [ -f "${ProgramFiles}\7-Zip\7z.exe" ] && [ ! -f 7z.exe ]
then
	cp -f  "${ProgramFiles}\7-Zip\7z.exe" 7z.exe
	wget -q  ${DOWNLOADHOST}/Downloads/AutoHosts.msw.7z
	./7z.exe x AutoHosts.msw.7z
	rm -f 7z.exe
	if [ -f AutoHosts.msw.7z ]
	then
		cd /
		cd $HomePath
		cd Desktop
		rm -fr AutoHosts.msw
		rm -fr AutoHosts.msw/*
		cp ${ETCDIR}/AutoHosts.msw.7z AutoHosts.msw.7z
		cp -f  "${ProgramFiles}\7-Zip\7z.exe" 7z.exe
		./7z.exe x AutoHosts.msw.7z
		rm -f 7z.exe AutoHosts.msw.7z
		if [ -d AutoHosts.msw ]
		then
			cd AutoHosts.msw
			cp hosts hosts.win
		fi
		cd ${ETCDIR}
	fi
else
	wget -q  ${DOWNLOADHOST}/Downloads/AutoHosts.msw.zip
	unzip AutoHosts.msw.zip
	if [ -f AutoHosts.msw.zip ]
	then
		cd /
		cd $HomePath
		cd Desktop
		rm -fr AutoHosts.msw
		rm -fr AutoHosts.msw/*
		cp ${ETCDIR}/AutoHosts.msw.zip AutoHosts.msw.zip
		unzip AutoHosts.msw.zip
		rm -f AutoHosts.msw.zip
		if [ -d AutoHosts.msw ]
		then
			cd AutoHosts.msw
			cp hosts hosts.win
		fi
		cd ${ETCDIR}
	fi
fi
rm -f AutoHosts.msw.*
if [ -d  AutoHosts.msw ]
then
	cd AutoHosts.msw
	if md5sum -c sums.md5
	then
		echo checksums are okay ... installing
		echo
		cd ..
		attrib +a -r -h -s hosts
		attrib +a -r -h -s hosts.old
		rm -f hosts.old
		mv hosts hosts.old
		grep -Uv 127\.0\.0\.1 hosts.org > 1
		grep -Uv \# 1 > newhosts
		rm -f 1
		cd AutoHosts.msw
		echo do you want to add the 2o7.net hosts to the hosts file \(y\|n\)\?
		read ADDREPLY
		case ${ADDREPLY} in
		'y' | 'Y' | 'yes' | 'Yes' | 'YES')
			if [ -f add.2o7Net ]
			then
				cat hosts add.2o7Net > ../hosts
			else
				echo there are no 2o7.net hosts to add
			fi
			;;
		*)
			cp -p hosts ..
			;;
		esac
		cd ..
		cat hosts >> newhosts
		rm -f hosts
		mv newhosts hosts
		echo Installation complete.  Leaving AutoHosts.msw folder with
		echo OpenPGP encryption signature for stronger verification on
		echo the Desktop.
		cd AutoHosts.msw
		if [ -f AutoHostsOld.sh ]
		then
			echo You have a new AutoHosts.sh in the AutoHosts.msw folder.
			echo Replace your current AutoHosts.sh with the new one ASAP.
		fi
		cd ..
		rm -fr AutoHosts.msw
		echo 
		echo You may want to edit the hosts file to uncomment some hosts and
		echo tidy up.
		echo 
	else
		echo the checksums FAILED ... exiting
		echo
	fi
else
	echo FAILED to either pull down file or unpack it.
	echo
fi

echo Press Enter to exit
echo
read UNUSEDRESPONSE

cd ${CURDIR}

exit


