|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# sage-apply-patches [-p<num>] [-d patch-subdir] [patch-dir] -- [...] |
| 4 | +# |
| 5 | +# Apply any patches to original spkg sources. Patch files must have |
| 6 | +# the .patch extension. |
| 7 | +# |
| 8 | +# By default the patches are applied from ../patches/ using the -p1 |
| 9 | +# option, and it is assumed that the patches are being applied from |
| 10 | +# the root of the package source. |
| 11 | +# |
| 12 | +# An optional patch subdirectory may be specified with the -d flag. |
| 13 | +# For example `sage-apply-patches -d cygwin` applies only those |
| 14 | +# patches under <patch-dir>/cygwin. |
| 15 | +# |
| 16 | +# The -p<num> arg is the argument accepted by the `patch` command, |
| 17 | +# and overrides the default -p1 |
| 18 | +# |
| 19 | +# Any additional arguments following " -- " are passed directly |
| 20 | +# to the `patch` command. |
| 21 | +# |
| 22 | +#*************************************************************************** |
| 23 | +# |
| 24 | +# Distributed under the terms of the GNU General Public License (GPL) |
| 25 | +# as published by the Free Software Foundation; either version 2 of |
| 26 | +# the License, or (at your option) any later version. |
| 27 | +# http://www.gnu.org/licenses/ |
| 28 | +#*************************************************************************** |
| 29 | + |
| 30 | +patchdir="../patches" |
| 31 | +patch_subdir="" |
| 32 | +patch_strip="-p1" |
| 33 | +patch_args_sep="" |
| 34 | +patch_args="--no-backup-if-mismatch" |
| 35 | + |
| 36 | +while [[ $# > 0 ]]; do |
| 37 | + if [[ -z "$patch_args_sep" ]]; then |
| 38 | + case $1 in |
| 39 | + -d) |
| 40 | + patch_subdir="${2%/}" |
| 41 | + shift |
| 42 | + ;; |
| 43 | + -p[0-9]) |
| 44 | + patch_strip="$1" |
| 45 | + ;; |
| 46 | + --) |
| 47 | + patch_args_sep="$1" |
| 48 | + ;; |
| 49 | + *) |
| 50 | + patchdir="${1%/}" |
| 51 | + ;; |
| 52 | + esac |
| 53 | + else |
| 54 | + patch_args="$patch_args $1" |
| 55 | + fi |
| 56 | + |
| 57 | + shift |
| 58 | +done |
| 59 | + |
| 60 | +patchdir="${patchdir}/${patch_subdir}" |
| 61 | +patchdir="${patchdir%/}" |
| 62 | +patches=( "${patchdir}"/*.patch ) |
| 63 | + |
| 64 | +if [[ -r "${patches[0]}" ]]; then |
| 65 | + echo "Applying patches from ${patchdir}..." |
| 66 | + for patch in ${patches[@]}; do |
| 67 | + # Skip non-existing or non-readable patches |
| 68 | + [ -r "$patch" ] || continue |
| 69 | + echo "Applying $patch" |
| 70 | + patch $patch_strip $patch_args < "$patch" |
| 71 | + if [ $? -ne 0 ]; then |
| 72 | + echo >&2 "Error applying '$patch'" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + done |
| 76 | +else |
| 77 | + >&2 echo "No patch files found in $patchdir" |
| 78 | +fi |
0 commit comments