summaryrefslogtreecommitdiffstats
path: root/importpatch.in
diff options
context:
space:
mode:
Diffstat (limited to 'importpatch.in')
-rwxr-xr-ximportpatch.in188
1 files changed, 188 insertions, 0 deletions
diff --git a/importpatch.in b/importpatch.in
new file mode 100755
index 0000000..6d0cb0b
--- /dev/null
+++ b/importpatch.in
@@ -0,0 +1,188 @@
+#!/bin/sh
+
+# Read in library functions
+if ! [ -r @LIB@/patchfns ]
+then
+ echo "Cannot read library @LIB@/patchfns" >&2
+ exit 1
+fi
+. @LIB@/patchfns
+
+usage()
+{
+ echo "Usage: importpatch [-f] [-p num] [-n patchname] [patchfile]"
+ if [ x$1 = x-h ]
+ then
+ cat <<EOF
+
+Import an external patch. If a patch file name is specified,
+the patch will be stored in this relative path in the patches/
+directory. Else, if an input file name is given this name is
+used as the patch name.
+
+-p num
+ Number of directory levels to strip when aplying (default=1)
+
+-n patchfile
+ File name relative to patches/ to use.
+
+-f Overwite/update existing patches.
+
+EOF
+ exit 0
+ else
+ exit 1
+ fi
+}
+
+options=`getopt -o fn:p:h -- "$@"`
+
+if [ $? -ne 0 ]
+then
+ usage
+fi
+
+eval set -- "$options"
+
+while true
+do
+ case "$1" in
+ -n)
+ opt_patch=$(echo "$2" |
+ sed -e 's:^'"$P"'patches/::' -e 's:^\.pc/::')
+ shift 2 ;;
+ -p)
+ opt_strip=$2
+ shift 2 ;;
+ -f)
+ opt_force=1
+ shift ;;
+ -h)
+ usage -h ;;
+ --)
+ shift
+ break ;;
+ esac
+done
+
+if [ $# -eq 1 ]
+then
+ input_file=$1
+elif [ $# -gt 1 ]
+then
+ usage
+fi
+
+[ -n "$opt_strip" ] && patch_args="-p$opt_strip"
+
+if [ -n "$opt_patch" ]
+then
+ patch=$(stripit "$opt_patch")
+ patch_file="${P}patches/$opt_patch"
+else
+ patch="$(stripit "$input_file")"
+ if [ -n "$patch" ]
+ then
+ opt_patch="$patch.patch"
+ else
+ echo "Please use -n to specify a patch file name."
+ exit 1
+ fi
+ patch_file="${P}patches/$opt_patch"
+fi
+
+if echo "$patch_file" | grep -q -e $'[ \t]'
+then
+ echo "Patch file name \"$patch_file\" contains whitespace."
+ exit 1
+fi
+
+if is_applied $patch
+then
+ echo "Patch $patch is applied."
+ exit 1
+fi
+
+case "$input_file" in
+'')
+ tmp=$(mktemp /tmp/patch-scripts.XXXXXX)
+ if ! cat > $tmp
+ then
+ echo "Cannot read from standard input."
+ exit 1
+ fi
+ input_file=$tmp ;;
+*.gz)
+ tmp=$(mktemp /tmp/patch-scripts.XXXXXX)
+ if ! gzip -cd "$input_file" > $tmp
+ then
+ echo "Cannot decompress file $input_file"
+ exit 1
+ fi
+ input_file=$tmp ;;
+*.bz2)
+ tmp=$(mktemp /tmp/patch-scripts.XXXXXX)
+ if ! bzip2 -cd "$input_file" > $tmp
+ then
+ echo "Cannot decompress file $input_file"
+ exit
+ fi
+ input_file=$tmp ;;
+*)
+ if ! [ -r "$input_file" ]
+ then
+ echo "Cannot read from file $input_file"
+ exit 1
+ fi
+esac
+
+if [ -e "$patch_file" ]
+then
+ if [ -z "$opt_force" ]
+ then
+ echo "Patch $patch exists. Replace with -f."
+ exit 1
+ fi
+
+ if grep -q '^%patch$' "$patch_file" &&
+ ! grep -q '^%patch$' "$input_file"
+ then
+ echo "Updating %patch section of patch $patch"
+ if ! @LIB@/parse-patch -u patch $patch_file< "$input_file"
+ then
+ echo "Failed to update %patch section of patch $patch"
+ exit 1
+ fi
+ else
+ echo "Replacing patch $patch with new version"
+ if ! cat "$input_file" > "$patch_file"
+ then
+ echo "Failed to replace patch $patch"
+ exit 1
+ fi
+ fi
+else
+ echo "Importing patch $patch (stored as $patch_file)"
+ if ! grep -q '^%patch$' "$input_file"
+ then
+ echo "%patch" >> "$patch_file"
+ fi
+ if ! cat "$input_file" >> "$patch_file"
+ then
+ echo "Failed to import patch $patch"
+ exit 1
+ fi
+fi
+
+rm -rf .pc/$patch
+
+if ! patch_in_series $patch &&
+ ! insert_in_series $opt_patch "$patch_args"
+then
+ echo "Failed to insert $patch in file series."
+fi
+
+if [ -n "$tmp" ]
+then
+ rm -f $tmp
+fi