dotfiles/.bin/update-adlist

75 lines
1.5 KiB
Plaintext
Raw Normal View History

#!/bin/sh -e
trap _restore_all 1 2 3 6
_list=${1:-/etc/unwind/blocklistproject.txt}
_url=https://blocklistproject.github.io/Lists/alt-version
_dir=/etc/unwind/lists
_restore_all() {
echo "Signal received, aborting..."
for _f in $_dir/*.old
do
echo "Restore: $(basename $_f)"
doas mv -f "$_f" "${_f%%.old}"
done
_assemble
_restart_unwind
exit 1
}
_backup() {
_f=$(basename "$1")
if [ -f "$_dir/$_f" ]
then
echo "Backup: $_f -> $_f.old"
doas mv -f "$_dir/$_f" "$_dir/$_f.old"
fi
}
_restore() {
_f=$(basename "$1")
if [ -f "$_dir/$_f.old" ]
then
echo "Restore: $_f.old -> $_f"
doas mv -f "$_dir/$_f" "$_dir/$_f.old"
fi
}
_download() {
echo "Download: $_file"
doas ftp -V -o "$_dir/$_file" "$_url/$_file" > /dev/null \
&& doas rm -f "$_dir/$_file.old"
}
_disabled() {
if echo "$1" | grep -q "^#"
then
_f=$(basename "$1" | tr -d '# ')
echo "Skip (disabled): $_f"
doas rm -f $_dir/$_f
return 0
fi
return 1
}
_assemble() {
echo "Assemble blocklist: /etc/unwind/_assembled.txt"
cat $_dir/*.txt \
| tr -d " " \
| grep -v '^#' \
| doas sort -uo /etc/unwind/_assembled.txt
}
_restart_unwind() {
doas rcctl restart unwind
}
for _file in $(grep -v "^##" $_list | xargs)
do
if ! _disabled $_file
then
_backup $_file
_download || _restore
fi
done
_assemble
_restart_unwind