you-dont-need-pihole/update.sh

83 lines
1.7 KiB
Bash
Raw Normal View History

2021-01-30 03:26:05 +00:00
#!/bin/bash
# vi: et sts=4 sw=4 ts=4
set -e
WORKDIR=${0%/*}
CFG=$WORKDIR/update.cfg
# Config defaults
BACKUPSUFFIX=
BLOCKLIST=$WORKDIR/block.list
LIST_DIR=$WORKDIR/lists
OUT=()
URL=()
if [[ -f $CFG ]]; then
. "$CFG"
fi
2021-01-30 03:26:05 +00:00
2022-08-11 17:40:40 +00:00
TEMP_DIR=$(mktemp -d -t "${0##*/}.XXXXXX")
2021-01-30 03:26:05 +00:00
cleanup() {
2022-08-11 17:40:40 +00:00
rm -fr -- "$TEMP_DIR"
2021-01-30 03:26:05 +00:00
}
trap 'cleanup' EXIT
2021-02-13 16:31:09 +00:00
(cd "$WORKDIR" && git submodule update --init --remote)
2021-01-30 03:26:05 +00:00
for (( I = 0 ; I < ${#OUT[@]} ; ++I )); do
MY_URL=${URL[$I]}
MY_OUT=${OUT[$I]}
if [[ -z $MY_URL ]]; then
echo "$CFG: URL[$I] empty" >&2
exit 2
fi
if [[ -z $MY_OUT ]]; then
echo "$CFG: OUT[$I] empty" >&2
exit 2
fi
2022-08-11 17:40:40 +00:00
TEMP_OUT=$(mktemp -p "$TEMP_DIR")
2021-01-30 03:26:05 +00:00
if [[ -f $MY_OUT ]]; then
cp -a -- "$MY_OUT" "$TEMP_OUT"
fi
wget \
-O "$TEMP_OUT" \
"$MY_URL"
if [[ -f $MY_OUT ]]; then
chmod --reference="$MY_OUT" "$TEMP_OUT"
if [[ -n $BACKUPSUFFIX ]]; then
mv -- "$MY_OUT" "$MY_OUT$BACKUPSUFFIX"
fi
fi
mkdir -p -- "${MY_OUT%/*}"
mv -- "$TEMP_OUT" "$MY_OUT"
# If the old one is the same, don't keep it around
if [[ -n $BACKUPSUFFIX && -f $MY_OUT$BACKUPSUFFIX ]]; then
if diff -q "$MY_OUT" "$MY_OUT$BACKUPSUFFIX"; then
rm -f -- "$MY_OUT$BACKUPSUFFIX"
fi
fi
done
if [[ -n $BACKUPSUFFIX && -f $BLOCKLIST ]]; then
mv -- "$BLOCKLIST" "$BLOCKLIST$BACKUPSUFFIX"
fi
"$WORKDIR/make-block.pl" --out="$BLOCKLIST"
# If the old one is the same the same, don't keep it around
if [[ -n $BACKUPSUFFIX && -f $BLOCKLIST$BACKUPSUFFIX ]]; then
if diff -q "$BLOCKLIST" "$BLOCKLIST$BACKUPSUFFIX"; then
rm -f -- "$BLOCKLIST$BACKUPSUFFIX"
fi
fi
if [[ -x /etc/rc.d/rc.dnsmasq ]]; then
/etc/rc.d/rc.dnsmasq restart
fi