]> git.ipfire.org Git - people/stevee/pakfire.git/blob - tools/patch
4d4c2ae5776b3562b069358c395602474d267f40
[people/stevee/pakfire.git] / tools / patch
1 #!/bin/bash
2 # Script that automatically applies patches.
3
4 paths=
5 patches=
6
7 while [ $# -gt 0 ]; do
8 case "${1}" in
9 --search-path=*)
10 paths="${paths} ${1#--search-path=}"
11 ;;
12 *)
13 patches="${patches} ${1}"
14 ;;
15 esac
16 shift
17 done
18
19 if [ -n "${patches}" ]; then
20 echo "Applying patches..."
21 fi
22
23 # Apply all patches given on command line.
24 for patch in ${patches}; do
25 case "${patch}" in
26 /*)
27 ;;
28 *)
29 for path in ${paths}; do
30 if [ -e "${path}/${patch}" ]; then
31 patch="${path}/${patch}"
32 break
33 fi
34 done
35 ;;
36 esac
37
38 # Check if patch file does exist.
39 if ! [ -e "${patch}" ]; then
40 echo >&2 " ERROR: Patch file does not exist: ${patch}"
41 exit 1
42 fi
43
44 # Options applied to patch command.
45 options="-N"
46
47 # Get right -p1 option.
48 case "${patch}" in
49 *.patch[0-9]|*.patch[0-9]R)
50 _patch="${patch}"
51 # Get patch level from file name.
52 while [ ${#_patch} -gt 0 ]; do
53 last_pos=$(( ${#_patch} - 1 ))
54 last_char=${_patch:${last_pos}}
55 _patch=${_patch:0:${last_pos}}
56
57 case "${last_char}" in
58 [0-9])
59 options="${options} -p${last_char}"
60 break
61 ;;
62 R)
63 options="${options} -R"
64 ;;
65 esac
66 done
67 ;;
68 *.patch|*.diff)
69 # Default is -p1.
70 options="${options} -p1"
71 ;;
72 *)
73 echo >&2 " WARNING: Ignoring unknown file: ${patch}"
74 continue
75 ;;
76 esac
77
78 echo " Applying ${patch} (${options})..."
79 patch ${options} -i ${patch} || exit $?
80 done
81
82 exit 0