summaryrefslogtreecommitdiff
path: root/update-repo
blob: aa2804b48d6e320ab7b3ab418b8747b1deff6089 (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
102
103
104
105
106
#!/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
}

show_pkg()
{
        for x in name summary description
        do
                hxextract .package-$x "$1" |
                        w3m -T text/html |
                        sed -e '/^$/d'
                echo
        done
}

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 -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