summaryrefslogtreecommitdiff
path: root/fdroids.sh
diff options
context:
space:
mode:
Diffstat (limited to 'fdroids.sh')
-rwxr-xr-xfdroids.sh122
1 files changed, 87 insertions, 35 deletions
diff --git a/fdroids.sh b/fdroids.sh
index b06d9a7..8830a18 100755
--- a/fdroids.sh
+++ b/fdroids.sh
@@ -1,49 +1,101 @@
1#!/bin/bash 1#!/bin/bash
2set -e 2set -e
3quiet() { "$@" >/dev/null 2>&1; } 3shopt -s lastpipe
4quiet dpkg-query -W html-xml-utils || sudo apt install html-xml-utils 4
5stale() { [ $(( $(date +%s) - $(stat -c%Y "$1") )) -gt $(( 60*60*12 )) ]; } 5baseurl=https://f-droid.org
6stale_hours=12
7
8quiet()
9{
10 "$@" >/dev/null 2>&1
11}
12
13stale()
14{
15 [ ! -e "$1" ] ||
16 [ $(( $(date +%s) - $(stat -c%Y "$1") )) -gt $(( 60*60*stale_hours )) ];
17}
18
6wget_with_log() 19wget_with_log()
7{ 20{
8 touch "$1" 21 set -- wget-log.txt "$@"
22 [ -e "$1" ] || touch "$1"
9 flock "$1" wget -a "$1" "${@:2}" 23 flock "$1" wget -a "$1" "${@:2}"
10} 24}
25
26list_existing()
27{
28 find repo/ -maxdepth 1 -type d |
29 xargs bash -c 'printf "%s\n" "${@#repo/}"' bash
30}
31
32parse_pkgname()
33{
34 # Set package on succ
35 set -- "${1%/}"
36 case "$1" in
37 '' | $baseurl/??/packages/*/* | $baseurl/packages/*/* )
38 false ;;
39 $baseurl/??/packages/* | $baseurl/packages/* )
40 echo "${1#*/packages/}" ;;
41 */* )
42 false ;;
43 * )
44 echo "$1"
45 ;;
46 esac
47}
48
49fetch_pkg()
50{
51 local pkg="$1"
52 url=$baseurl/en/packages/$url
53 if wget_with_log -c -O index.html "$url"
54 then
55 # Wget might not touch file if it is up-to-date
56 # with server
57 touch "$pkgdir"/index.html
58 else
59 echo "failed to fetch url ${url@Q}" >&2
60 return
61 fi
62
63 if hxwls "$pkgdir"/index.html |
64 egrep "^$baseurl/repo/${pkg}_[0-9]+\.apk\$" |
65 sort -n |
66 tail -n1 |
67 read link &&
68 [ "$link" ]
69 then
70 (cd "$pkgdir" &&
71 wget_with_log -c "$link" "$link.asc")
72 else
73 echo "failed to fetch package ${pkg@Q}" >&2
74 fi
75}
76
77quiet dpkg-query -W html-xml-utils || sudo apt install html-xml-utils
78
11if [ -t 0 ] 79if [ -t 0 ]
12then 80then
13 exec < fdroids.txt 81 exec < <(list_existing)
14fi 82fi
15while read url 83while read
16do 84do
17 baseurl=https://f-droid.org 85 [ "$REPLY" ] || continue
18 url=${url%/} 86 if ! parse_pkgname "$REPLY" | read pkg || ! [ "$pkg" ]
19 case "$url" in 87 then
20 $baseurl/??/packages/*/* | $baseurl/packages/*/* ) 88 echo "Skipping invalid URL: ${REPLY@Q}" >&2
21 continue ;; 89 fi
22 $baseurl/??/packages/* | $baseurl/packages/* ) 90 pkgdir=repo/$pkg
23 pkg=${url#*/packages/} ;; 91 mkdir -p "$pkgdir"
24 * ) continue ;; 92 if stale "$pkgdir"/index.html
25 esac
26 html=repo/$pkg/index.html
27 if [ ! -e "$html" ] || stale "$html"
28 then 93 then
29 (
30 echo "$pkg" >&2 94 echo "$pkg" >&2
31 mkdir -p "${html%/*}" 95 (
32 cd "${html%/*}" || continue 96 cd "$pkgdir"
33 if wget_with_log wget-log.txt -c -O "$html" "$url" 97 fetch_pkg "$pkg"
34 then 98 )
35 touch "$html" 99 break
36 else
37 continue
38 fi
39 link=$(hxwls "$html" | egrep "/repo/${pkg}_[0-9]+\.apk\$" | sort -n | tail -n1)
40 if [ "$link" ] &&
41 wget_with_log wget-log.txt -c "$link" "$link.asc"
42 then
43 continue
44 else
45 echo "failed to fetch $pkg" >&2
46 fi
47 )
48 fi 100 fi
49done 101done