Remove glib dependency to libelf
[openadk.git] / scripts / update-patches
blob98d4324ba911d30accf3bf882dfa50b34cfa20bd
1 #!/usr/bin/env mksh
2 #-
3 # Copyright (c) 2014
4 #       Thorsten Glaser <tg@mirbsd.org>
5 # Copyright (c) 2006
6 #       Thorsten Glaser <tg@freewrt.org>
8 # Derived from the MirPorts Framework "update-patches" script:
10 # Copyright (c) 2003, 2004, 2005
11 #       Thorsten "mirabile" Glaser <tg@MirBSD.de>
12 # Based upon code and idea (c) 2000
13 #       Marc Espie for the OpenBSD project. All rights reserved.
15 # Provided that these terms and disclaimer and all copyright notices
16 # are retained or reproduced in an accompanying document, permission
17 # is granted to deal in this work without restriction, including un-
18 # limited rights to use, publicly perform, distribute, sell, modify,
19 # merge, give away, or sublicence.
21 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
22 # the utmost extent permitted by applicable law, neither express nor
23 # implied; without malicious intent or gross negligence. In no event
24 # may a licensor, author or contributor be held liable for indirect,
25 # direct, other damage, loss, or other issues arising in any way out
26 # of dealing in the work, even if advised of the possibility of such
27 # damage or existence of a defect, except proven that it results out
28 # of said person's immediate fault when using the work as intended.
30 do_diff() {
31         local f1=$2/$1
32         local f2=$3/$1
34         if [[ ! -e $f1 ]]; then
35                 [[ -d ${f1%/*}/. ]] || mkdir -p "${f1%/*}"
36                 if [[ ! -s $f2 ]]; then
37                         cat <<EOF
38 --- $f1 (non-existant)
39 +++ $f2 (empty)
40 @@ -0,0 +0,0 @@
41 EOF
42                         return 0
43                 fi
44                 touch -t 197001010000.00 "$f1"
45         fi
46         diff -adup "$f1" "$f2"
47         return $?
50 set -A accounted
51 set -A edit
53 TRANSFORM='sed s/[.+]/\\\\&/g'
54 PATCHDIR=$CURDIR/patches
55 EXTRADIR=$CURDIR/extra
57 mkdir -p "$PATCHDIR"
59 SUBDIST=${WRKDIST##${WRKDIR1}?(/)}
60 if [[ -n $SUBDIST ]]; then
61         mv "${WRKDIR1}.orig/${SUBDIST}" "${WRKDIR1}/${SUBDIST}.orig"
62         D_BASE=${WRKDIR1}
63         D_SUB=${SUBDIST}
64 #       D_SUBP=$D_SUB
65         D_SUBP='[^/]*'
66         D_CMP=$D_SUBP
67 else
68         # WRKSRC == WRKDIR
69         D_BASE=$(dirname "${WRKDIR1}")
70         D_SUB=$(basename "${WRKDIR1}")
71         D_SUBP=$D_SUB
72         D_CMP=
74 ORGDIST=${D_BASE}/${D_SUB}.orig
76 if [[ -e $WRKDIST/.patched-newfiles ]]; then
77         touch "$ORGDIST/.patched-newfiles"
78         patch_newfiles=1
79 else
80         patch_newfiles=0
83 if [[ -e $WRKDIST/../.autoreconf_done ]]; then
84         touch "$ORGDIST/.autoreconf_done"
87 DIFF_FLAGS="-adu -I \"^--- $(print -r -- "$D_SUBP.orig/" | $TRANSFORM)@@        .*\""
88 DIFF_FLAGS="$DIFF_FLAGS -I \"^\+\+\+ $(print -r -- "$D_SUBP/" | $TRANSFORM)@@   .*\""
90 (cd "${WRKDIST}"; find . -type f -print0) |&
91 while IFS= read -p -d '' -r file; do
92         file=${file#./}
93         [[ ! -e $ORGDIST/$file && $patch_newfiles = 0 ]] && continue
94         for i in $DIFF_IGNOREFILES; do
95                 [[ $file = $i ]] && continue 2
96         done
97         cmp -s "$ORGDIST/$file" "$WRKDIST/$file" && continue
98         print -ru2 -- "Processing ${file}..."
99         # look in patchdir for an existing patchfile matching this
100         cd "$PATCHDIR"
101         for i in $PATCH_LIST; do
102                 # Ignore non-files, or old backup
103                 [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
105                 # Patch found. Is this the one?
106                 if grep "^[+-][+-][+-] $D_CMP[^/]*/$file        " "$i" >/dev/null; then
107                         # Multiple files in the diff?
108                         if [[ $(grep -c "^--- $D_CMP" "$i") -gt 1 || \
109                             $(grep -c "^+++ $D_CMP" "$i") -gt 1 ]]; then
110                                 print -ru2 -- "Cannot process, $i contains patches"
111                                 print -ru2 -- "to multiple files! Aborting."
112                                 print -n 'FAIL\0'
113                                 [[ -n $SUBDIST ]] && mv \
114                                     "${WRKDIR1}/${SUBDIST}.orig" \
115                                     "${WRKDIR1}.orig/${SUBDIST}"
116                                 exit 0
117                         fi
118                         # Multiple diffs with this file?
119                         let n=0
120                         pflst=
121                         for j in $PATCH_LIST; do
122                                 [[ ! -f $j || $j = *@(.orig|.rej|~) ]] && \
123                                     continue
124                                 grep "^[+-][+-][+-] $D_CMP[^/]*/$file   " \
125                                     "$j" >/dev/null || continue
126                                 let n++
127                                 pflst="$pflst '$j'"
128                         done
129                         if (( n != 1 )); then
130                                 print -ru2 -- "Cannot process, file $file"
131                                 print -ru2 -- "is contained in multiple patches:"
132                                 print -ru2 -- "$pflst"
133                                 print -n 'FAIL\0'
134                                 [[ -n $SUBDIST ]] && mv \
135                                     "${WRKDIR1}/${SUBDIST}.orig" \
136                                     "${WRKDIR1}.orig/${SUBDIST}"
137                                 exit 0
138                         fi
139                         # No, process this patch
141                         accounted+=("$i")
142                         # found it, copy preamble before comparision
143                         ( sed -e "/^--- /,\$d" <"$i"; \
144                           cd "$D_BASE" && do_diff "$file" "$D_SUB.orig" "$D_SUB" \
145                         ) >"$i.new"
146                         # did it change? mark it as changed
147                         tfile=$(print -r -- "$file" | $TRANSFORM)
148                         if eval diff "$(print -r -- "${DIFF_FLAGS}" | sed \
149                             "s#@@#${tfile}#g")" '"$i" "$i.new"' 1>&2; then
150                                 rm "$i.new"
151                         else
152                                 print -ru2 -- "Patch $i for $file updated"
153                                 mv "$i" "$i.orig"
154                                 mv "$i.new" "$i"
155                                 edit+=("$i")
156                         fi
157                         continue 2
158                 fi
159         done
161         # Build a sensible name for the new patch file
162         patchname=patch-${file//[\/.\x01- ]/_}
163         print -ru2 -- "No patch-* found for $file, creating $patchname"
164         ( cd "$D_BASE" && do_diff "$file" "$D_SUB.orig" "$D_SUB" ) >"$patchname"
165         edit+=("$patchname")
166         accounted+=("$patchname")
167 done
169 # Verify all patches accounted for
170 cd "$PATCHDIR"
171 for i in *; do
172         [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
173         grep '^\\ No newline at end of file' "$i" >/dev/null && \
174             print -ru2 -- "*** Patch $i needs manual intervention"
175         found=0
176         for j in "${accounted[@]}"; do
177                 [[ $i = "$j" ]] || continue
178                 found=1
179                 break
180         done
181         (( found )) || print -ru2 -- "*** Patch $i not accounted for"
182 done
184 for i in "${edit[@]}"; do
185         print -nr -- "$i"
186         print -n '\0'
187 done
188 [[ -n $SUBDIST ]] && mv "${WRKDIR1}/${SUBDIST}.orig" "${WRKDIR1}.orig/${SUBDIST}"
189 exit 0