#!/system/bin/busybox sh

# download hostlist from http(s) (need curl, its absent by default),
# resolve to ip list, feed to zapret-ip. save flash write cycles

u="https://your.host.com/censorship/hoslist.txt"

SCRIPT=$(readlink -f "$0")
EXEDIR=$(dirname "$SCRIPT")

d=/data/censorship
[ -d $d ] || mkdir $d
f=$d/hostlist.txt
t=/hostlist.txt
i=/iplist.txt

curl -k --fail --max-time 10 -o "$t" "$u" && {
 if [ -s "$t" ]; then
  m1=$(md5sum "$t" | cut -d ' ' -f 1)
  m2=$(md5sum "$f" | cut -d ' ' -f 1)
  echo $m1 $m2
  if [ -z "$m2" ] || [ "$m1" != "$m2" ]; then
   echo updating hostlist
   cp -f "$t" "$f"
  else
   echo hostlist was not changed. keeping old copy
  fi
 else
  echo downloaded hostlist is empty. disabling zapret
  rm "$f"
 fi
}

rm -f "$t"
"$EXEDIR/unzapret-ip"
[ -s "$f" ] && {
 mdig --threads=10 --family=4 <"$f" >"$i"
 [ -s "$i" ] && exec "$EXEDIR/zapret-ip" "$i"
}