summaryrefslogtreecommitdiff
path: root/contrib/updpkgsums.sh.in
blob: a40845b69e87ae78faf94f57f0701db7e92e8502 (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
107
108
#!/bin/bash
#
# updpkgsums - update source checksums in-place in PKGBUILDs
#
# Copyright (C) 2012-2013 Dave Reisner <dreisner@archlinux.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

shopt -s extglob

declare -r myname='updpkgsums'
declare -r myver='@PACKAGE_VERSION@'

usage() {
	printf "%s (pacman) v%s\n" "${myname}" "${myver}"
	echo
	printf "%s will perform an in place update of the checksums in the\n" "${myname}"
	echo "path specified by [build file], defaulting to PKGBUILD in the current"
	echo "working directory."
	echo
	printf "Usage: %s [build file]\n" "${myname}"
	echo
	echo "    -h, --help        display this help message and exit"
	echo "    -V, --version     display version information and exit"
}

version() {
	printf "%s %s\n" "$myname" "$myver"
	echo 'Copyright (C) 2012-2013 Dave Reisner <dreisner@archlinux.org>'
}

die() {
	printf "==> ERROR: $1\n" "${@:2}" >&2
	exit 1
}

case $1 in
	-h|--help) usage; exit ;;
	-V|--version) version; exit ;;
esac

buildfile=${1:-PKGBUILD}
if [[ ! -f $buildfile ]]; then
	die "%s not found or is not a file" "$buildfile"
fi

# Resolve any symlinks to avoid replacing the symlink with a file. But, we
# have to do this portably -- readlink's flags are inconsistent across OSes.
while [[ -L $buildfile ]]; do
	buildfile=$(readlink "$buildfile")
	if [[ $buildfile = */* ]]; then
		cd "${buildfile%/*}"
		buildfile=${buildfile##*/}
	fi
done

# cd into the directory with the build file. This avoids creating random src/
# directories scattered about the filesystem, and avoids cases where we might
# not be able to write in the $PWD.
if [[ $buildfile = */* ]]; then
	cd "${buildfile%/*}"
	buildfile=${buildfile##*/}
fi

# Check $PWD/ for permission to unlink the $buildfile and write a new one
if [[ ! -w . ]]; then
	die "No write permission in '%s'" "$PWD"
fi

# Generate the new sums
export BUILDDIR=$(mktemp -d --tmpdir updpkgsums.XXXXXX)
newbuildfile=$(mktemp --tmpdir updpkgsums.XXXXXX)

# In case the eventual replacement fails, we don't want to leave behind
# $newbuildfile as garbage in $TMPDIR. This fails silently if the replacement
# succeeds.
trap "rm -rf '$BUILDDIR' '$newbuildfile'" EXIT
newsums=$(makepkg -g -p "$buildfile") &&
awk -v newsums="$newsums" '
	/^[[:blank:]]*(md|sha)[[:digit:]]+sums(_[^=]+)?=/,/\)[[:blank:]]*(#.*)?$/ {
		if (!w) {
			print newsums
			w++
		}
		next
	}

	1
	END { if (!w) print newsums }
' "$buildfile" > "$newbuildfile"

# Replace the original buildfile.
if ! mv -- "$newbuildfile" "$buildfile"; then
	die "Failed to update %s. The file has not been modified." "$buildfile"
fi

# vim: set noet: