libXaw: fix wildcard ('?' must be non-empty)
[openadk.git] / scripts / update-patches
blobd38a8fdf4ec293dfd1673c26c868d3adf37e990f
1 #!/usr/bin/env bash
2 # Copyright (c) 2006
3 # Thorsten Glaser <tg@freewrt.org>
5 # Derived from the MirPorts Framework "update-patches" script:
7 # Copyright (c) 2003, 2004, 2005
8 # Thorsten "mirabile" Glaser <tg@MirBSD.de>
9 # Based upon code and idea (c) 2000
10 # Marc Espie for the OpenBSD project. All rights reserved.
12 # Provided that these terms and disclaimer and all copyright notices
13 # are retained or reproduced in an accompanying document, permission
14 # is granted to deal in this work without restriction, including un-
15 # limited rights to use, publicly perform, distribute, sell, modify,
16 # merge, give away, or sublicence.
18 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
19 # the utmost extent permitted by applicable law, neither express nor
20 # implied; without malicious intent or gross negligence. In no event
21 # may a licensor, author or contributor be held liable for indirect,
22 # direct, other damage, loss, or other issues arising in any way out
23 # of dealing in the work, even if advised of the possibility of such
24 # damage or existence of a defect, except proven that it results out
25 # of said person's immediate fault when using the work as intended.
27 [[ -n $BASH_VERSION ]] && shopt -s extglob
29 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
44 touch -t 197001010000.00 "$f1"
46 diff -adup "$f1" "$f2"
47 return $?
50 TRANSFORM='sed s/[.+]/\\\\&/g'
51 PATCHDIR=$CURDIR/patches
52 EXTRADIR=$CURDIR/extra
54 mkdir -p $PATCHDIR
56 SUBDIST=${WRKDIST##${WRKDIR1}?(/)}
57 if [[ -n $SUBDIST ]]; then
58 mv ${WRKDIR1}.orig/${SUBDIST} ${WRKDIR1}/${SUBDIST}.orig
59 D_BASE=${WRKDIR1}
60 D_SUB=${SUBDIST}
61 # D_SUBP=$D_SUB
62 D_SUBP='[^/]*'
63 D_CMP=$D_SUBP
64 else
65 # WRKSRC == WRKDIR
66 D_BASE=$(dirname ${WRKDIR1})
67 D_SUB=$(basename ${WRKDIR1})
68 D_SUBP=$D_SUB
69 D_CMP=
71 ORGDIST=${D_BASE}/${D_SUB}.orig
73 if [[ -e $WRKDIST/.patched-newfiles ]]; then
74 touch $ORGDIST/.patched-newfiles
75 patch_newfiles=1
76 else
77 patch_newfiles=0
80 DIFF_FLAGS="-adu -I \"^--- $(echo $D_SUBP.orig/ | $TRANSFORM)@@ .*\""
81 DIFF_FLAGS="$DIFF_FLAGS -I \"^\+\+\+ $(echo $D_SUBP/ | $TRANSFORM)@@ .*\""
83 for file in $(cd ${WRKDIST}; find . -type f | sed 's#^\./##'); do
84 [[ ! -e $ORGDIST/$file && $patch_newfiles = 0 ]] && continue
85 cmp -s "$ORGDIST/$file" "$WRKDIST/$file" && continue
86 echo "Processing ${file}..." >&2
87 # look in patchdir for an existing patchfile matching this
88 cd $PATCHDIR
89 for i in $PATCH_LIST; do
90 # Ignore non-files, or old backup
91 [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
93 # Patch found. Is this the one?
94 if grep "^[+-][+-][+-] $D_CMP[^/]*/$file " "$i" >/dev/null; then
95 # Multiple files in the diff?
96 if [ $(grep -c "^--- $D_CMP" "$i") -gt 1 -o \
97 $(grep -c "^+++ $D_CMP" "$i") -gt 1 ]; then
98 echo "Cannot process, $i contains patches" >&2
99 echo "to multiple files! Aborting." >&2
100 echo FAIL
101 [[ -n $SUBDIST ]] && mv \
102 ${WRKDIR1}/${SUBDIST}.orig \
103 ${WRKDIR1}.orig/${SUBDIST}
104 exit 0
106 # Multiple diffs with this file?
107 let n=0
108 pflst=
109 for j in $PATCH_LIST; do
110 [[ ! -f $j || $j = *@(.orig|.rej|~) ]] && \
111 continue
112 grep "^[+-][+-][+-] $D_CMP[^/]*/$file " \
113 "$j" >/dev/null || continue
114 let n++
115 pflst="$pflst '$j'"
116 done
117 if (( n != 1 )); then
118 echo "Cannot process, file $file" >&2
119 echo "is contained in multiple patches:" >&2
120 echo "$pflst" >&2
121 echo FAIL
122 [[ -n $SUBDIST ]] && mv \
123 ${WRKDIR1}/${SUBDIST}.orig \
124 ${WRKDIR1}.orig/${SUBDIST}
125 exit 0
127 # No, process this patch
129 accounted="$accounted $i"
130 # found it, copy preamble before comparision
131 ( sed -e "/^--- /,\$d" <$i; \
132 cd $D_BASE && do_diff "$file" "$D_SUB.orig" "$D_SUB" \
133 ) >"$i.new"
134 # did it change ? mark it as changed
135 tfile="$(echo "$file" | $TRANSFORM)"
136 if eval diff "$(echo "${DIFF_FLAGS}" \
137 | sed "s#@@#${tfile}#g")" \
138 "$i" "$i.new" 1>&2; then
139 rm "$i.new"
140 else
141 echo "Patch $i for $file updated" >&2
142 mv "$i" "$i.orig"
143 mv "$i.new" "$i"
144 edit="$edit $i"
146 continue 2
148 done
150 # Build a sensible name for the new patch file
151 patchname=patch-$(echo "$file" | sed -e 's#[/. ]#_#g')
152 echo "No patch-* found for $file, creating $patchname" >&2
153 ( cd $D_BASE && do_diff "$file" "$D_SUB.orig" "$D_SUB" ) >$patchname
154 edit="$edit $patchname"
155 accounted="$accounted $patchname"
156 done
158 # Verify all patches accounted for
159 cd $PATCHDIR
160 for i in *; do
161 [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
162 grep '^\\ No newline at end of file' $i >/dev/null \
163 && echo "*** Patch $i needs manual intervention" >&2
164 [[ $accounted != *@($i)* ]] \
165 && echo "*** Patch $i not accounted for" >&2
166 done
168 echo $edit
169 [[ -n $SUBDIST ]] && mv ${WRKDIR1}/${SUBDIST}.orig ${WRKDIR1}.orig/${SUBDIST}
170 exit 0