Merge pull request #1027 from AladW/build-asroot-example
[aurutils.git] / lib / aur-pkglist
blob9183417eee0851ed56c7e0b56f13d941ad4d6e9e
1 #!/bin/bash
2 # aur-pkglist - print the AUR package list
3 [[ -v AUR_DEBUG ]] && set -o xtrace
4 argv0=pkglist
5 XDG_CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache}
6 AUR_LOCATION=${AUR_LOCATION:-'https://aur.archlinux.org'}
7 AUR_METADEST=${AUR_METADEST:-$XDG_CACHE_HOME/aurutils/$argv0} # variable name prone to change
8 PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
10 # default options
11 ttl=300 pkglist=packages update=0 verify=0 use_system_time=0
13 http_last_modified() {
14 awk -F', ' '/^[Ll]ast-[Mm]odified:/ {print $2}' "$1"
17 list_fetch() {
18 curl -A aurutils -f "$1" "${@:2}" --remote-name
21 list_headers() {
22 curl -A aurutils -f "$1" --head -Ss
25 usage() {
26 printf >&2 'usage: %s [-bqsiv] [-t ttl] [-FP pattern]\n' "$argv0"
27 exit 1
30 source /usr/share/makepkg/util/parseopts.sh
32 opt_short='t:bqsivFPJISu'
33 opt_long=('pkgbase' 'search' 'info' 'fixed-strings' 'perl-regexp' 'plain'
34 'ttl:' 'users' 'verify' 'quiet' 'systime')
35 opt_hidden=('dump-options' 'time:' 'json')
37 if ! parseopts "$opt_short" "${opt_long[@]}" "${opt_hidden[@]}" -- "$@"; then
38 usage
40 set -- "${OPTRET[@]}"
42 unset mode
43 while true; do
44 case "$1" in
45 -b|--pkgbase)
46 pkglist=pkgbase ;;
47 -s|--search)
48 pkglist=packages-meta-v1.json ;;
49 -i|--info)
50 pkglist=packages-meta-ext-v1.json ;;
51 --users)
52 pkglist=users ;;
53 -F|--fixed-strings)
54 mode=fixed ;;
55 -P|--perl-regexp)
56 mode=regex ;;
57 --plain)
58 mode=plain ;;
59 -q|--quiet)
60 mode=none ;;
61 -t|--time|--ttl)
62 shift; ttl=$1 ;;
63 -v|--verify)
64 verify=1 ;;
65 --systime)
66 use_system_time=1 ;;
67 # Deprecated options
68 -u) printf >&2 'deprecation notice: %s -u is an alias for --quiet\n' "$argv0"
69 mode=none ;;
70 -I) printf >&2 'deprecation notice: %s -I is an alias for --info\n' "$argv0"
71 pkglist=packages-meta-ext-v1.json ;;
72 -S) printf >&2 'deprecation notice: %s -S is an alias for --search\n' "$argv0"
73 pkglist=packages-meta-v1.json ;;
74 -J|--json)
75 printf >&2 'deprecation notice: %s --json is an alias for [--info|--search] | jq\n' "$argv0"
76 mode=plain ;;
77 --dump-options)
78 printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
79 printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
80 exit ;;
81 --) shift; break ;;
82 esac
83 shift
84 done
86 # default to regex if >0 arguments specified
87 if [[ ! -v mode ]] && (( $# > 0 )); then
88 mode=regex
89 elif [[ ! -v mode ]]; then
90 mode=plain
93 if ! [[ $ttl =~ [0-9]+ ]]; then
94 printf >&2 "error: --ttl requires an integer ('%s' provided)\n" "$ttl"
95 exit 1
98 # packages.gz cache
99 install -dm700 "$AUR_METADEST"
100 cd "$AUR_METADEST" || exit
102 if [[ ! -s headers_$pkglist || ! -s $pkglist ]]; then
103 update=1
105 elif [[ -s $pkglist ]]; then
106 sec_l=$(http_last_modified "headers_$pkglist" | date -f - '+%s')
108 if (( use_system_time )); then
109 sec_d=$(date '+%s')
110 else
111 sec_d=$(http_last_modified <(list_headers "$AUR_LOCATION/$pkglist.gz") | date -f - '+%s')
114 if (( sec_d - sec_l > ttl )); then
115 update=1
119 if (( update )); then
120 list_fetch "$AUR_LOCATION/$pkglist.gz" --dump-header "headers_$pkglist"
122 if (( verify )); then
123 list_fetch "$AUR_LOCATION/$pkglist.gz.sha256"
124 sha256sum --check --strict --quiet "$pkglist.gz.sha256" || exit
127 if [[ $pkglist == *.json ]]; then
128 # Remove newlines separating partial objects, i.e.
129 # [\n{...},\n{...},...\n] => [{...}{...},...]\n
130 { gunzip -c "$pkglist.gz" | tr -d '\n'
131 printf '\n'
132 } > "$pkglist"
133 else
134 gunzip -f "$pkglist.gz"
136 fi >&2
138 # pattern match
139 case $mode in
140 plain)
141 cat "$pkglist" ;;
142 fixed)
143 grep -F "$1" "$pkglist" ;;
144 regex)
145 grep -P "$1" "$pkglist" ;;
146 none)
147 printf '%s\n' "$PWD/$pkglist" ;;
148 esac
150 # vim: set et sw=4 sts=4 ft=sh: