summaryrefslogtreecommitdiff
path: root/addPkgbuildPatch
blob: 07c4fce74b8ee3a6eab63543480f041d08deaf12 (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
#!/bin/bash

tmpDir=$(dirname "$(readlink -e "$0")")

usage() {
  >&2 printf 'usage:\n'
  >&2 printf '  %s add|del\n' "$0"
  >&2 printf 'To be called next to the to be patched PKGBUILD.\n'
  exit 1
}

if [ $# -ne 1 ] || [ ! -f 'PKGBUILD' ]; then
  usage
fi

if [ "$1" != 'add' ] && [ "$1" != 'del' ]; then
  usage
fi

if [ "$1" = 'add' ] \
&& ! grep -qwF _patch_PKGBUILD PKGBUILD; then

  # add PKGBUILD-patches to the PKGBUILD
  if grep -q '^\s*pkgbase=' PKGBUILD; then
    (
      eval "$(
        sed -n '/^\s*pkgname=(/,/)/ p' PKGBUILD
      )"
      printf 'package_%s\n' "${pkgname[@]}"
    )
  else
    printf 'package\n'
  fi \
  | sponge \
  | while read -r function; do
    sed -i '
      /^'"${function}"'() {$/,/^}$/ {
        /^}$/ i _patch_PKGBUILD
      }
    ' PKGBUILD
  done
  {
    echo '_patch_PKGBUILD() {'
    ls -1 "${tmpDir}/"*".PKGBUILDpatch" \
    | grep -vxF "$(
      sed '
        s,^# skip \(\S\+\)\(\s\|$\).*$,'"${tmpDir}"'/\1.PKGBUILDpatch,
        t
        d
      ' PKGBUILD
    )" \
    | xargs -r cat
    echo ':'
    echo '}'
  } \
  >> PKGBUILD
fi

if [ "$1" = 'del' ] \
&& grep -qwF _patch_PKGBUILD PKGBUILD; then

  # del PKGBUILD-patches from the PKGBUILD
  sed -i '
    /^_patch_PKGBUILD$/d
    /^_patch_PKGBUILD() {$/,/^}/d
  ' PKGBUILD
fi