af6df9ec72b2f9356bc47191d694385061990bae
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / support / scripts / apply-patches.sh
1 #!/usr/bin/env bash
2 # A little script I whipped up to make it easy to
3 # patch source trees and have sane error handling
4 # -Erik
5 #
6 # (c) 2002 Erik Andersen <andersen@codepoet.org>
7 #
8 # Parameters:
9 # - "-s", optional. Silent operation, don't print anything if there
10 # isn't any error.
11 # - the build directory, optional, default value is '.'. The place where are
12 # the package sources.
13 # - the patch directory, optional, default '../kernel-patches'. The place
14 # where are the scripts you want to apply.
15 # - other parameters are the patch name patterns, optional, default value is
16 # '*'. Pattern(s) describing the patch names you want to apply.
17 #
18 # The script will look recursively for patches from the patch directory. If a
19 # file is named 'series' then only patches mentionned into it will be applied.
20 # If not, the script will look for file names matching pattern(s). If the name
21 # ends with '.tar.*', '.tbz2' or '.tgz', the file is considered as an archive
22 # and will be uncompressed into a directory named
23 # '.patches-name_of_the_archive-unpacked'. It's the turn of this directory to
24 # be scanned with '*' as pattern. Remember that scanning is recursive. Other
25 # files than series file and archives are considered as a patch.
26 #
27 # Once a patch is found, the script will try to apply it. If its name doesn't
28 # end with '.gz', '.bz', '.bz2', '.xz', '.zip', '.Z', '.diff*' or '.patch*',
29 # it will be skipped. If necessary, the patch will be uncompressed before being
30 # applied. The list of the patches applied is stored in '.applied_patches_list'
31 # file in the build directory.
32
33 silent=
34 if [ "$1" = "-s" ] ; then
35     # add option to be used by the patch tool
36     silent=-s
37     shift
38 fi
39
40 # Set directories from arguments, or use defaults.
41 builddir=${1-.}
42 patchdir=${2-../kernel-patches}
43 shift 2
44 patchpattern=${@-*}
45
46 # use a well defined sorting order
47 export LC_COLLATE=C
48
49 if [ ! -d "${builddir}" ] ; then
50     echo "Aborting.  '${builddir}' is not a directory."
51     exit 1
52 fi
53 if [ ! -d "${patchdir}" ] ; then
54     echo "Aborting.  '${patchdir}' is not a directory."
55     exit 1
56 fi
57
58 # Remove any rejects present BEFORE patching - Because if there are
59 # any, even if patches are well applied, at the end it will complain
60 # about rejects in builddir.
61 find ${builddir}/ '(' -name '*.rej' -o -name '.*.rej' ')' -print0 | \
62     xargs -0 -r rm -f
63
64 function apply_patch {
65     path=$1
66     patch=$2
67     case "$patch" in
68         *.gz)
69         type="gzip"; uncomp="gunzip -dc"; ;; 
70         *.bz)
71         type="bzip"; uncomp="bunzip -dc"; ;; 
72         *.bz2)
73         type="bzip2"; uncomp="bunzip2 -dc"; ;; 
74         *.xz)
75         type="xz"; uncomp="unxz -dc"; ;;
76         *.zip)
77         type="zip"; uncomp="unzip -d"; ;; 
78         *.Z)
79         type="compress"; uncomp="uncompress -c"; ;; 
80         *.diff*)
81         type="diff"; uncomp="cat"; ;;
82         *.patch*)
83         type="patch"; uncomp="cat"; ;;
84         *)
85         echo "Unsupported file type for ${path}/${patch}, skipping";
86         return 0
87         ;;
88     esac
89     if [ -z "$silent" ] ; then
90         echo ""
91         echo "Applying $patch using ${type}: "
92     fi
93     if [ ! -e "${path}/$patch" ] ; then
94         echo "Error: missing patch file ${path}/$patch"
95         exit 1
96     fi
97     echo $patch >> ${builddir}/.applied_patches_list
98     ${uncomp} "${path}/$patch" | patch -g0 -p1 -E -d "${builddir}" -t -N $silent
99     if [ $? != 0 ] ; then
100         echo "Patch failed!  Please fix ${patch}!"
101         exit 1
102     fi
103 }
104
105 function scan_patchdir {
106     local path=$1
107     shift 1
108     patches=${@-*}
109
110     # If there is a series file, use it instead of using ls sort order
111     # to apply patches. Skip line starting with a dash.
112     if [ -e "${path}/series" ] ; then
113         for i in `grep -Ev "^#" ${path}/series 2> /dev/null` ; do
114             apply_patch "$path" "$i"
115         done
116     else
117         for i in `cd $path; ls -d $patches 2> /dev/null` ; do
118             if [ -d "${path}/$i" ] ; then
119                 scan_patchdir "${path}/$i"
120             elif echo "$i" | grep -q -E "\.tar(\..*)?$|\.tbz2?$|\.tgz$" ; then
121                 unpackedarchivedir="$builddir/.patches-$(basename $i)-unpacked"
122                 rm -rf "$unpackedarchivedir" 2> /dev/null
123                 mkdir "$unpackedarchivedir"
124                 tar -C "$unpackedarchivedir" -xaf "${path}/$i"
125                 scan_patchdir "$unpackedarchivedir"
126             else
127                 apply_patch "$path" "$i"
128             fi
129         done
130     fi
131 }
132
133 scan_patchdir "$patchdir" "$patchpattern"
134
135 # Check for rejects...
136 if [ "`find $builddir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] ; then
137     echo "Aborting.  Reject files found."
138     exit 1
139 fi
140
141 # Remove backup files
142 find $builddir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;