Scripts testsuite output consistency
[pacman-ng.git] / contrib / updpkgsums.sh.in
blob38c8281034d871bbf0fd6732e4a808a727a663ca
1 #!/bin/bash
3 # updpkgsums - update source checksums in-place in PKGBUILDs
5 # Copyright (C) 2012 Dave Reisner <dreisner@archlinux.org>
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 shopt -s extglob
22 declare -r myname='updpkgsums'
23 declare -r myver='@PACKAGE_VERSION@'
25 usage() {
26 printf 'usage: %s [buildfile]\n\n' "$myname"
27 printf ' -h, --help display this help message and exit\n'
28 printf ' -V, --version display version information and exit\n\n'
29 printf '%s will perform an in place update the checksums in the\n' "$myname"
30 printf 'path specified by [buildfile], defaulting to PKGBUILD in the current\n'
31 printf 'working directory.\n'
34 version() {
35 printf "%s %s\n" "$myname" "$myver"
36 echo 'Copyright (C) 2012 Dave Reisner <dreisner@archlinux.org>'
39 case $1 in
40 -h|--help) usage; exit ;;
41 -V|--version) version; exit ;;
42 esac
44 buildfile=${1:-PKGBUILD}
45 if [[ ! -f $buildfile ]]; then
46 printf '==> ERROR: %s not found or is not a file: %s\n' "$buildfile"
47 exit 1
50 # Resolve any symlinks to avoid replacing the symlink with a file. But, we
51 # have to do this portably -- readlink's flags are inconsistent across OSes.
52 while [[ -L $buildfile ]]; do
53 buildfile=$(readlink "$buildfile")
54 if [[ $buildfile = */* ]]; then
55 cd "${buildfile%/*}"
56 buildfile=${buildfile##*/}
58 done
60 # cd into the directory with the build file. This avoids creating random src/
61 # directories scattered about the filesystem, and avoids cases where we might
62 # not be able to write in the $PWD.
63 if [[ $buildfile = */* ]]; then
64 cd "${buildfile%/*}"
65 buildfile=${buildfile##*/}
68 # Check $PWD/ for permission to unlink the $buildfile and write a new one
69 if [[ ! -w . ]]; then
70 printf $'==> ERROR: No write permission in `%s\'\n' "$PWD"
71 exit 1
75 # Generate the new sums and try to unlink the file before writing stdin back
76 # into it. This final precaution shouldn't fail based on the previous checks,
77 # but it's better to be extra careful before unlinking files.
78 newsums=$(makepkg -g -p "$buildfile") && rm -f "$buildfile" &&
79 exec awk -v newsums="$newsums" '
80 /^[[:blank:]]*(md|sha)[[:digit:]]+sums=/,/\)[[:blank:]]*(#.*)?$/ {
81 if (!w) {
82 print newsums
83 w++
85 next
89 END { if (!w) print newsums }
90 ' > "$buildfile"
91 } < "$buildfile"
93 # vim: set ts=2 sw=2 noet: