Merge pull request #986 from AladW/manpage-optfix
[aurutils.git] / lib / aur-srcver
blob80224e4165980d816a9709f9ed689acaaac17cb2
1 #!/bin/bash
2 # aur-srcver - update and print package revisions
3 [[ -v AUR_DEBUG ]] && set -o xtrace
4 shopt -s nullglob
5 argv0=srcver
6 PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
8 # default arguments
9 makepkg_args=('--nobuild' '--nodeps' '--skipinteg')
10 num_procs=$(( "$(nproc)" + 2 ))
12 trap_exit() {
13 if [[ ! -v AUR_DEBUG ]]; then
14 rm -rf -- "$tmp"
15 else
16 printf >&2 'AUR_DEBUG: %s: temporary files at %s\n' "$argv0" "$tmp"
20 usage() {
21 printf >&2 'usage: %s [--no-prepare] <pkgbase> [<pkgbase> ...]\n' "$argv0"
22 exit 1
25 source /usr/share/makepkg/util/parseopts.sh
26 source /usr/share/makepkg/util/pkgbuild.sh
28 opt_short=j:E:
29 opt_long=('no-prepare' 'jobs:' 'buildscript:')
30 opt_hidden=('dump-options' 'noprepare' 'env:')
32 if ! parseopts "$opt_short" "${opt_long[@]}" "${opt_hidden[@]}" -- "$@"; then
33 usage
35 set -- "${OPTRET[@]}"
37 unset buildscript
38 while true; do
39 case "$1" in
40 --noprepare|--no-prepare)
41 makepkg_args+=(--noprepare) ;;
42 -E|--env)
43 shift ;; # deprecated flag, has no effect
44 -j|--jobs)
45 shift; num_procs=$1 ;;
46 --buildscript)
47 shift; buildscript=$1
48 makepkg_args+=(-p "$1") ;;
49 --dump-options)
50 printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
51 printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
52 exit ;;
53 --)
54 shift; break ;;
55 esac
56 shift
57 done
59 # Single hyphen to denote input taken from stdin
60 stdin=0
61 if (( $# == 1 )) && [[ $1 == "-" || $1 == "/dev/stdin" ]]; then
62 stdin=1
65 if (( ! $# )); then
66 usage
69 # shellcheck disable=SC2174
70 mkdir -pm 0700 "${TMPDIR:-/tmp}/aurutils-$UID"
71 tmp=$(mktemp -d --tmpdir "aurutils-$UID/$argv0.XXXXXXXX") || exit
72 trap 'trap_exit' EXIT
74 # A pipeline `foo | bar &` causes `bar` to detach from the script. In
75 # this case, aur-srcver returns immediately with `makepkg` processes
76 # still running in the background. Read all input into an array to
77 # avoid this. (cf. aur-view, #958)
78 if (( stdin )); then
79 mapfile -t packages
80 else
81 packages=("$@")
82 set --
85 i=0 # package counter
86 for n in "${packages[@]}"; do
87 if (( i++ >= num_procs )); then
88 wait -n
91 { mkdir -p "$tmp/$n"
93 if ! env -C "$n" nice -n 20 makepkg "${makepkg_args[@]}" >"$tmp/$n"/log 2>&1; then
94 echo $? >"$tmp/$n"/failed
96 } &
97 done
98 wait
100 failed=()
101 for d in "$tmp"/*/; do # iterate over directories
102 n=$(basename "$d")
104 if [[ -e $tmp/$n/failed ]]; then
105 failed+=("$n")
106 else
107 # Precautions when sourcing the PKGBUILD have no effect here,
108 # because makepkg already sourced the PKGBUILD above.
109 # shellcheck disable=SC1090
110 ( source "$n/${buildscript-PKGBUILD}"
112 fullver=$(get_full_version)
113 printf '%s\t%s\n' "${pkgbase:-$pkgname}" "$fullver"
116 done
118 if (( ${#failed[@]} )); then
119 printf >&2 '8<----\n'
122 for f in "${failed[@]}"; do
123 printf >&2 '%s: makepkg %s failed for package %s\n' "$argv0" "${makepkg_args[*]}" "$f"
125 cat "$tmp/$f"/log >&2
126 printf >&2 '8<----\n'
127 done
129 # vim: set et sw=4 sts=4 ft=sh: