#!/bin/sh
##START buildhosts
#   This script gets various anti-ad hosts files, merges, sorts, and uniques, then installs.
#   Run from cron once a week.
#
#	Copyright © 2014 Matthew Headlee <mmh@matthewheadlee.com> (http://matthewheadlee.com/)
#
#   This file is buildhosts.
#
#   buildhosts 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 3 of the License,
#   or (at your option) any later version.
#
#   buildhosts 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
#	buildhosts.  If not, see
#   <http://www.gnu.org/licenses/>.

#Create/Truncate new-hosts, download and merge multiple hosts files
#This build of busybox/wget doesn't support passing in multiple URLs... Lame.
wget -qO - 'http://winhelp2002.mvps.org/hosts.txt' > /tmp/new-hosts
wget -qO - 'https://adaway.org/hosts.txt' >> /tmp/new-hosts
wget -qO - 'https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&mimetype=plaintext' >> /tmp/new-hosts
wget -qO - 'https://hosts-file.net/ad_servers.txt' >> /tmp/new-hosts
wget -qO - 'http://f.xn0.org/customhosts.txt' >> /tmp/new-hosts

#Convert hosts text to the UNIX format. Strip comments, blanklines, and invalid characters.
#Replaces tabs/spaces with a single space, remove localhost entries.
#This build of ddwrt/busybox is missing the sort command, using php to sort & unique.
sed -r -e "s/$(echo -en '\r')//g" \
       -e '/^#/d' \
       -e 's/#.*//g' \
       -e 's/[^a-zA-Z0-9\.\_\t \-]//g' \
       -e 's/(\t| )+/ /g' \
       -e 's/^127\.0\.0\.1/0.0.0.0/' \
       -e '/ localhost /d' \
       -e '/^ *$/d' \
       /tmp/new-hosts | \
 php -r '$a=array_unique(explode("\n",file_get_contents("php://stdin")));sort($a);echo trim(implode("\n",$a))."\n";' > /etc/hosts

#Add an entry for the router's hostname and for localhost.
echo -e "$(hostname -i) $(hostname)\n127.0.0.1 localhost" >> /etc/hosts

#Tell dnsmasq to reread the updated hosts file.
#In theory /tmp/var/run/dnsmasq.pid should contain the PID of the only running dnsmasq process
#kill -1 "$(cat /tmp/var/run/dnsmasq.pid)"
#In practice, maybe it died, maybe aliens ate the file. Lets trust ps instead.
#ps | awk '/[d]nsmasq/ {print $1}' | xargs -n1 kill -1
kill -1 $(ps w | awk '/[d]nsmasq/ {print $1}')

#Cleanup
rm -f /tmp/new-hosts
##END buildhosts
