#!/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