apkgrel.in 1.61 KB
Newer Older
1 2
#!/bin/sh

3 4 5 6 7 8
# apkgrel - display or bump pkgrel in APKBUILDs
# Copyright (c) 2012 Natanael Copa <ncopa@alpinelinux.org>
#
# Distributed under GPL-2
#

9 10 11 12 13 14 15 16 17
abuild_ver=@VERSION@
datadir=@datadir@

if ! [ -f "$datadir/functions.sh" ]; then
	echo "$datadir/functions.sh: not found" >&2
	exit 1
fi
. "$datadir/functions.sh"

18 19 20 21 22 23 24 25 26 27 28 29 30

do_show() {
	awk -F= '$1 == "pkgrel" { print $2 }' "$@"
}

do_set() {
	sed -e "/^pkgrel=/s/=.*/=${setto:-0}/" \
		-i "$@"
}

do_add () {
	local f= old=
	for f in "$@"; do
31 32 33 34
		[ -n "$only_clean_git" ] \
			&& [ -n "$(git diff --name-only $f)" ] \
			&& continue
		[ -d "$f" ] && f="$f/APKBUILD"
35 36 37
		old=$(do_show "$f")
		setto=$(($old + 1))
		do_set "$f" || return 1
38
	done
39 40 41 42
}

do_verify() {
	[ -n "$force" ] && return 0
43 44 45 46 47 48 49 50 51
	local f= rc=0
	for f in "$@"; do
		[ -d "$f" ] && f="$f/APKBUILD"
		if ! grep -q '^pkgrel=[0-9]' "$f"; then
			echo "$f: Has no proper pkgrel" >&2
			rc=1
		fi
	done
	return $rc
52 53 54 55 56 57 58 59
}

do_nothing() {
	return 0
}

do_usage() {
	cat <<__EOF__
60
Usage: $prog -a|-h|-s NUM|-t|-z [-f] FILE...
61 62
Commands:
  -a  Add 1 to current pkgrel
63
  -g  Only do the change on files that have clean git status
64 65 66 67 68 69 70 71 72 73 74 75 76
  -h  Show this help
  -s  Set pkgrel to NUM
  -t  Only verify that files are in proper format
  -z  Set pkgrel to 0

Options:
  -f  Force, even if given files are not in proper format

__EOF__
}

cmd=do_show
force=
77
while getopts "afghs:tz" opt; do
78 79
	case $opt in
	a) cmd=do_add;;
80
	g) only_clean_git=1;;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	f) force=1;;
	h) cmd=do_usage;;
	s) setto=$OPTARG; cmd=do_set;;
	t) cmd=do_nothing;;
	z) setto=0; cmd=do_set;;
	esac
done

shift $(( $OPTIND - 1))

if [ $# -eq 0 ]; then
	do_usage
	exit 1
fi

do_verify "$@" || exit 1
$cmd "$@"