#!/bin/bash set -e shopt -s lastpipe baseurl=https://f-droid.org stale_hours=12 quiet() { "$@" >/dev/null 2>&1 } stale() { [ ! -e "$1" ] || [ $(( $(date +%s) - $(stat -c%Y "$1") )) -gt $(( 60*60*stale_hours )) ] } wget_with_log() { set -- wget-log.txt "$@" [ -e "$1" ] || touch "$1" flock "$1" wget -a "$1" "${@:2}" } list_existing() { find repo/ -maxdepth 1 -type d | xargs bash -c 'printf "%s\n" "${@#repo/}"' bash } parse_pkgname() { set -- "${1%/}" case "$1" in '' | $baseurl/??/packages/*/* | $baseurl/packages/*/* ) false ;; $baseurl/??/packages/* | $baseurl/packages/* ) echo "${1#*/packages/}" ;; */* ) false ;; * ) echo "$1" ;; esac } extract_text() { hxextract "$1" "$2" | w3m -T text/html | grep -v '^$' } show_pkg() { name=$(extract_text .package-name "$1") summary=$(extract_text .package-summary "$1") printf '%s\n%s\n' "$name" "$summary" >&2 } fetch_pkg() { local pkg="$1" url=$baseurl/en/packages/$pkg if wget_with_log -O index.html.part "$url" then mv -T index.html.part index.html else echo "failed to fetch url ${url@Q}" >&2 return 1 fi hxwls index.html | egrep "^$baseurl/repo/${pkg}_[0-9]+\.apk\$" | sort -n | tail -n1 | read link && [ "$link" ] || return show_pkg index.html >&2 wget_with_log --show-progress -c "$link" "$link.asc" } quiet dpkg-query -W html-xml-utils || sudo apt install html-xml-utils if [ -t 0 ] then exec < <(list_existing) fi while read do [ "$REPLY" ] || continue if ! parse_pkgname "$REPLY" | read pkg || ! [ "$pkg" ] then echo "Skipping invalid URL: ${REPLY@Q}" >&2 fi pkgdir=repo/$pkg mkdir -p "$pkgdir" stale "$pkgdir"/stamp || continue stale "$pkgdir"/failstamp || continue echo "$pkg" >&2 if ( cd "$pkgdir" && fetch_pkg "$pkg" ) then touch "$pkgdir"/stamp else touch "$pkgdir"/failstamp fi done