summaryrefslogtreecommitdiffstats
path: root/bin/makepkgbuild
blob: 3e044b6189358da026d8c220922d8968aae77d3d (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
#!/bin/sh

prog=makepkgbuild

die()
{
	rc=$1; shift
	echo "$prog: $*" 1>&2
	exit $rc
}

editpb()
{
	op=$1 func=$2

	case "$op" in
	append)
		regexp="^${func}[(][)]<?$"
		edin="/^ *$func() *{
/^}
i" ;;
	prepend)
		regexp=">${func}()"
		edin="/^ *$func() *{
/^[ \t]*cd/
a" ;;
	*)	die 2 unknown editpb operation: $op
	esac

	txt=$(awk -v r="$regexp" -v f="$func" 'BEGIN { FS = "\n"; RS = "" }
		$1 ~ r { for (i = 2; i <= NF; i++) print $i }' \
		PKGMETA | sed 's/^/    /')
	[ X"$txt" = X ] && return 0

	cat <<END | ed -s PKGBUILD >/dev/null
$edin
$txt
.
wq
END
	return $?
}

if ! [ -r PKGMETA ]
then
	die 1 "PKGMETA could not be read."
fi

case "$TDIR" in
'')	die 1 "TDIR env variable is not set."
esac

ttcmds=$(awk '
	BEGIN { FS = "\n"; RS = "" }
	$1 == "template" { for (i = 2; i <= NF; i++) print $i }' PKGMETA)

oifs=$IFS
IFS='
'
for ttcmd in "$ttcmds"
do
	IFS=' '
	set -- $ttcmd
	cmd="$TDIR/$1"
	if ! [ -f "$cmd" -a -x "$cmd" ]
	then
		die 2 "Unknown template command: $cmd"
	fi
done
IFS=$oifs

ttcmd="cat PKGMETA$(echo "$ttcmds" | sed "s!^! | $TDIR/!" | tr -d '\n')"

"$TDIR/pbfields" <PKGMETA >PKGBUILD
eval $ttcmd >>PKGBUILD || die 2 "template pipeline ($ttcmd) failed"

for func in package check build
do
	for op in append prepend
	do
		editpb $op $func || die 2 "error $? when trying to $op to ${func}()"
	done
done