#!/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
#140212 zigbert: gui (gtkdialog) improvements.

# 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 sysctl='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/adlist-all /tmp/hosts-temp
}

# download the ads lists
download_adlist () {
	# mvps
	if [ "$mvps" = true ]; then
		wget -c -4 -t 2 -O /tmp/adlist1 'http://winhelp2002.mvps.org/hosts.txt'
	fi
	# sysctl
	if [ "$sysctl" = true ]; then
		wget -c -4 -t 2 -O /tmp/adlist2 'http://sysctl.org/cameleon/hosts'
	fi
	# yoyo
	if [ "$yoyo" = true ]; then
		wget -c -4 -t 2 -O /tmp/adlist3 '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
	# format of mvps list needs to be modified before combining with others
	sed -i '/^#/d' /tmp/adlist1
	sed -i '/localhost/d' /tmp/adlist1
	sed -i 's/0.0.0.0/127.0.0.1/' /tmp/adlist1
	sed -i 's/ #.*$//' /tmp/adlist1
	# combine the downloaded lists, then sort and remove duplicates
	cat /tmp/adlist1 /tmp/adlist2 /tmp/adlist3 |grep ^[0-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 
   /usr/lib/gtkdialog/box_ok "$title" complete "$(gettext '<b>Success - your settings have been changed.</b>

Your hosts file has been updated.
Restart your browser to see the changes.')"
}

msg_help () {
echo "$title $(gettext "adds stuff to your '/etc/hosts' file, so that many advertising servers and websites will not be able to connect to this PC.

Leave your cursor over a service to see a short description. You can choose one service or combine multiple services for more advert protection.

Blocking ad servers protects your privacy, saves you bandwidth, greatly improves web-browsing speeds and makes the internet much less annoying in general.")" > /tmp/box_help

/usr/lib/gtkdialog/box_help "$title" &
}
export -f msg_help

export PupAdvertBlocker='<window title="'$title' '$appver'" icon-name="gtk-execute">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info scale dialog_stop.svg 60 "<b>$title</b>" "$(gettext "Block online ads in all browsers with this simple tool")"`"'
  <frame '$(gettext 'Choose your preferred service(s) or select them all for maximum ad blocking')'>
    <vbox>
      <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>Sysctl.org</label>
        <variable>sysctl</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>
      <text><label>""</label></text>
      <hbox border-width="7" space-expand="true" space-fill="true">
        <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext "<b>Start blocker</b>
Download and block the latest list of known advertising servers. Click with nothing selected to remove all blocking")'"</label></text>
        <vbox space-expand="false" space-fill="false">
          <button>
            '"`/usr/lib/gtkdialog/xml_button-icon execute.svg big`"'
            <action>download_adlist</action>
            <action>clean_adlist</action>
            <action>append_adlist</action>
            <action>cleanup</action>
            <action>success</action>
          </button>
        </vbox>
      </hbox>
      <hbox border-width="7" space-expand="true" space-fill="true">
        <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext "<b>Edit host list</b>
Manually edit your hosts file in a text editor, adding or removing any entries you like.")'"</label></text>
        <vbox space-expand="false" space-fill="false">
          <button>
            '"`/usr/lib/gtkdialog/xml_button-icon edit.svg big`"'
            <action>defaulttexteditor /etc/hosts &</action>
          </button>
        </vbox>
      </hbox>
    </vbox>
  </frame>
  <hbox space-expand="false" space-fill="false"> 
    <hbox space-expand="false" space-fill="false"> 
      <button>
        <label>'$(gettext "Help")'</label>
        '"`/usr/lib/gtkdialog/xml_button-icon help`"'
        <action>msg_help</action>
      </button>
    </hbox>
    <text space-expand="true" space-fill="true"><label>""</label></text>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Quit")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
      <action type="exit">EXIT_NOW</action>
    </button>
  </hbox>
</vbox>
</window>'

# cleanup before start
cleanup
# run the program
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
gtkdialog -p PupAdvertBlocker
