#!/bin/bash
#creates a zip version of a privoxy package suitable for android
#needs statically linked gentoo binary packages of privoxy
[ "$1" ]|| { echo "usage: $(basename $0) gentoo-privoxy-binary-pkg.tbz2"; exit ; }


TMPDIR=$(mktemp -d)
[[ -d $TMPDIR ]] || { echo "mktemp failed."; exit 1 ; }

OLD_PWD=$PWD
ZIP_PRIVOXY=$(basename "$1" .tbz2).zip

mkdir -p $TMPDIR/etc

for file in $@;do tar -xj -C $TMPDIR -f "$file";done

rm -r $TMPDIR/etc/{init.d,logrotate.d} $TMPDIR/{usr/share/man,lib,var}
sed -i \
	-e 's# /etc/# /sdcard/etc/#' \
	-e 's# /var/# /sdcard/var/#' \
	-e 's# /usr/# /sdcard/usr/#' \
	-e 's/^#debug/debug/' \
		$TMPDIR/etc/privoxy/config
cat <<END > $TMPDIR/etc/resolv.conf
#Big, fast, commercial, tracking
#https://www.opendns.com/setupguide/
#nameserver 208.67.222.222
#https://developers.google.com/speed/public-dns/
#nameserver 8.8.8.8
#https://dns.yandex.com/
#nameserver 77.88.8.8

#ad blocking, commercial
#http://www.alternate-dns.com/
#nameserver 23.253.163.53

#censorshipfree (?), commercial (?)
#https://dns.watch
#nameserver 84.200.70.40
#https://freedns.zone
#nameserver 37.235.1.174
#http://servidordenoms.cat/vull-aquest-dns
#nameserver 109.69.8.51

#censorshipfree, open design/community driven (?)
#https://www.opennicproject.org/ (choose one near you)
#nameserver 185.97.7.7
#https://www.orsn.org/de/tech/pubdns/ (choose one near you)
nameserver 212.224.71.71
#https://digitalcourage.de/support/zensurfreier-dns-server
#nameserver 85.214.20.141
#https://blog.uncensoreddns.org/
#nameserver 91.239.100.100
END
cat <<END >$TMPDIR/privoxy.start
#sdcard is mounted non-executable, so copy privoxy to the homedir of the used app
#Homedir of connectbot (change if a different app is used)
CB_HOME=/data/data/org.connectbot
[ /sdcard/usr/sbin/privoxy -nt \$CB_HOME/privoxy ] && cat /sdcard/usr/sbin/privoxy > \$CB_HOME/privoxy && chmod 755 \$CB_HOME/privoxy
\$CB_HOME/privoxy --no-daemon /sdcard/etc/privoxy/config
END

cd $TMPDIR
zip -q -r $OLD_PWD/$ZIP_PRIVOXY *
cd $OLD_PWD

rm -r $TMPDIR
