#!/bin/sh
#v0.3 created by sc0ttman, August 2010: http://murga-linux.com/puppy/viewtopic.php?t=59290
#GPL license /usr/share/doc/legal/gpl-2.0.txt
#100830 BK added GPL license, amended Exit msg, bug fixes.
#120202 rodin.s: adding gettext

# advert blocker
# downloads a list of known advert servers
# then appends them to /etc/hosts so that
# many online adverts are blocked from sight

# make a hosts file if none found, or add a marker

export TEXTDOMAIN=pup-advert-blocker
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}
export LANGORG=$LANG

if [ ! -f /etc/hosts ];then 
	echo "#host file
127.0.0.1 localhost puppypc
" > /etc/hosts
fi

# set vars
export appver='0.3'
export title='Pup Advert Blocker'
# the markers used to find the changes in /etc/hosts, which are made by this app
export markerstart='# pup-advert-blocker IPs below'
export markerend='# pup-advert-blocker IPs above'
export mvps='false'
export systcl='false'
export technobeta='false'
export yoyo='false'

# create functions
set -a

# cleanup all leftover files
cleanup () {
	# remove all temp files
	rm -f /tmp/adlist1 /tmp/adlist2 /tmp/adlist3 /tmp/adlist4 /tmp/adlist-all /tmp/hosts-temp
}

# download the ads lists
download_adlist () {
	# mvps
	if [ "$mvps" = true ]; then
		wget -c -4 -t 0 -O /tmp/adlist1 'http://www.mvps.org/winhelp2002/hosts.txt'
	fi
	# systcl
	if [ "$systcl" = true ]; then
		wget -c -4 -t 0 -O /tmp/adlist2 'http://sysctl.org/cameleon/hosts'
	fi
	# technobeta
	if [ "$technobeta" = true ]; then
		wget -c -4 -t 0 -O /tmp/adlist3 'http://www.technobeta.com/download/urlfilter.ini'
	fi
	# yoyo
	if [ "$yoyo" = true ]; then
		wget -c -4 -t 0 -O /tmp/adlist4 'http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext'
	fi
	#100830 BK bug fix: create if not exist...
	touch /tmp/adlist1
	touch /tmp/adlist2
	touch /tmp/adlist3
	touch /tmp/adlist4
	# combine the downloaded lists, then sort and remove duplicates
	cat /tmp/adlist1 /tmp/adlist2 /tmp/adlist3 /tmp/adlist4 |grep ^[1-9] |sed "s/\t//g"|sort |uniq > /tmp/adlist-all
}

# clean out everything but the list of IPs and servers
clean_adlist () {
	sed -i '/^#/d' /tmp/adlist-all # remove all comments
	sed -i '/localhost/d' /tmp/adlist-all # remove the original links to localhost (we already have them)
	sed -i '/^$/d' /tmp/adlist-all # remove empty lines
	sed -i 's/\t/ /' /tmp/adlist-all # replace all tabs with spaces
	sed -i 's/  / /g' /tmp/adlist-all # remove double spaces
	dos2unix -u /tmp/adlist-all # change all carriage returns to UNIX format
	#cat /tmp/adlist-all |sort |uniq > /tmp/adlist-all # remove duplicates (again)
	ADLISTALL="`cat /tmp/adlist-all |sort | uniq`" #100830 BK
	echo "$ADLISTALL" > /tmp/adlist-all #100830 BK
}
	
# append the list to the /etc/hosts
append_adlist () {
	# echo all but the stuff between the markers to a temp hosts file
	sed -e "/$markerstart/,/$markerend/d" /etc/hosts > /tmp/hosts-temp
	# remove the markers
	sed -i -e "/$markerstart/d" /tmp/hosts-temp
	sed -i -e "/$markerend/d" /tmp/hosts-temp
	# check the size of the final adlist
	if [ -s /tmp/adlist-all ];then
		# get contents of the downloaded adlist
		adlist=`cat /tmp/adlist-all`
		# add list contents into the hosts file, below a marker (for easier removal)
		echo "$markerstart" >> /tmp/hosts-temp
		echo "$adlist" >> /tmp/hosts-temp
		echo "$markerend" >> /tmp/hosts-temp
	else
		Xdialog --title "$title $appver" --msgbox "$(gettext 'No ad lists selected. Ad blocking will be disabled.')" 0 0
	fi
	# replace the original with the cleaned version
	mv "/tmp/hosts-temp" "/etc/hosts"
}

success () {
	# tell user 
	Xdialog --title "$title $appver" --msgbox "$(gettext 'Success - your settings have been changed.\n\nYour hosts file has been updated.\nRestart your browser to see the changes.')" 0 0 &
}

# create a GUI
export HELP_GUI='<window title="'$title' '$appver'">
	<frame>
		<vbox>
			<text width-request="360">
				<label>'$(gettext 'The')' "'$title'" '$(gettext 'tool adds stuff to your "/etc/hosts" file, so that many advertising servers and websites will not be able to connect to this PC.')'</label>
			</text>
			<text><label>""</label></text>

			<text width-request="360">
				<label>'$(gettext 'Leave your cursor over a service to see a short description. You can choose one service or combine multiple services for more advert protection.')'</label>
			</text>
			<text><label>""</label></text>

			<text width-request="360">
				<label>'$(gettext 'Blocking ad servers protects your privacy, saves you bandwidth, greatly improves web-browsing speeds and makes the internet much less annoying in general.')'</label>
			</text>
			<text><label>""</label></text>

			<text width-request="360">
				<label>'$title' '$appver', by sc0ttman</label>
			</text>
		</vbox>

		<text><label>""</label></text>

		<hbox>
			<button tooltip-text="'$(gettext 'Close this help dialog')'">
				<input file icon="gtk-quit"></input>
				<label>'$(gettext 'Close')'</label>
				<action>exec gtkdialog3 --program GUI --center &</action>
				<action type="exit">EXIT_NOW</action>
			</button>
		</hbox>
	</frame>
</window>'

export GUI='<window title="'$title' '$appver'">
	<vbox>
		<vbox homogeneous="true">
			<frame>
				<text><label>'$title'</label></text>
				<text><label>'$(gettext 'Block online ads in all browsers with this simple tool')'</label></text>
			</frame>
		</vbox>
		<vbox>

			<frame>
				<vbox>
						<text>
							<label>'$(gettext 'Choose your preferred ad blocking service(s)')'</label>
						</text>
					</vbox>
					<hbox>
						<checkbox tooltip-text="'$(gettext 'Blocks many known malware sites and unsafe adult networks')'">
							<label>Mvps.org</label>
							<variable>mvps</variable>
							<default>false</default>
						</checkbox>
 						<checkbox tooltip-text="'$(gettext 'A large, fairly comprehensive list of many known ad servers')'">
							<label>Systcl.org</label>
							<variable>systcl</variable>
							<default>false</default>
						</checkbox>
						<checkbox tooltip-text="'$(gettext 'A smaller list of popup adverts, ad servers and ad networks')'">
							<label>Technobeta.com</label>
							<variable>technobeta</variable>
							<default>false</default>
						</checkbox>
						<checkbox tooltip-text="'$(gettext 'A small and effective list of popular ad servers')'">
							<label>Yoyo.org</label>
							<variable>yoyo</variable>
							<default>false</default>
						</checkbox>
					</hbox>
			</frame>

			<frame>
				<vbox>
					<hbox>
						<text>
							<label>'$(gettext 'Click the "Start" button to download and block the latest list of known advertising servers')'</label>
						</text>
						<button width-request="70" tooltip-text="'$(gettext 'Click to download and then block a list of advertising servers')'">
							<variable>START</variable>
							<input file icon="gtk-execute"></input>
							<label>'$(gettext 'Start')'</label>
							<action>download_adlist</action>
							<action>clean_adlist</action>
							<action>append_adlist</action>
							<action>cleanup</action>
							<action>success</action>
						</button>
					</hbox>

					<text><label>""</label></text>

					<hbox>
						<text>
							<label>'$(gettext 'Or click the "Edit" button to manually edit your hosts file, using your default text editor')'</label>
						</text>
						<button width-request="70" tooltip-text="'$(gettext 'Manually edit your hosts file in a text editor, adding or removing any entries you like')'">
							<variable>EDIT</variable>
							<input file icon="gtk-edit"></input>
							<label>'$(gettext 'Edit')'</label>
							<action>defaulttexteditor /etc/hosts &</action>
						</button>
					</hbox>
				</vbox>
			</frame>

			<frame>
				<hbox>
					<button tooltip-text="'$(gettext 'Learn more about blocking ads')'">
						<variable>HELP</variable>
						<input file icon="gtk-help"></input>
						<label>'$(gettext 'Help')'</label>
						<action>exec gtkdialog3 --program HELP_GUI --center &</action>
						<action type="exit">EXIT_NOW</action>
					</button>
					<button tooltip-text="'$(gettext 'Exit Pup Advert Blocker')'">
						<variable>QUIT</variable>
						<input file icon="gtk-quit"></input>
						<label>'$(gettext 'Quit')'</label>
						<action type="exit">EXIT_NOW</action>
					</button>
				</hbox>
			</frame>
			
		</vbox>
	</vbox>
</window>'

# cleanup before start
cleanup
# run the program
gtkdialog3 --program GUI --center
