summaryrefslogtreecommitdiff
path: root/fdroids.sh
blob: 8830a181c53b1a66dcad83d8e6437c8a2ae8ed91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/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 package on succ
        set -- "${1%/}"
        case "$1" in
                '' | $baseurl/??/packages/*/* | $baseurl/packages/*/* )
                        false ;;
                $baseurl/??/packages/* | $baseurl/packages/* )
                        echo "${1#*/packages/}" ;;
                */* )
                        false ;;
                * )
                        echo "$1"
                        ;;
        esac
}

fetch_pkg()
{
        local pkg="$1"
        url=$baseurl/en/packages/$url
        if wget_with_log -c -O index.html "$url"
        then
                # Wget might not touch file if it is up-to-date
                # with server
                touch "$pkgdir"/index.html
        else
                echo "failed to fetch url ${url@Q}" >&2
                return
        fi

        if hxwls "$pkgdir"/index.html |
                egrep "^$baseurl/repo/${pkg}_[0-9]+\.apk\$" |
                sort -n |
                tail -n1 |
                read link &&
                [ "$link" ]
        then
                (cd "$pkgdir" &&
                wget_with_log -c "$link" "$link.asc")
        else
                echo "failed to fetch package ${pkg@Q}" >&2
        fi
}

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"
        if stale "$pkgdir"/index.html
        then
                echo "$pkg" >&2
                (
                        cd "$pkgdir"
                        fetch_pkg "$pkg"
                )
                break
        fi
done