Magellan Linux

Contents of /trunk/mage/usr/lib/mage/etc-update

Parent Directory Parent Directory | Revision Log Revision Log


Revision 60 - (show annotations) (download)
Tue Feb 15 22:59:14 2005 UTC (19 years, 2 months ago) by niro
File size: 10441 byte(s)
rev bump to 0.3.6-r13

1 #!/bin/bash
2 # Copyright 2002 Gentoo Technologies, Inc.
3 # Distributed under the terms of the GNU Public License v2
4 # Author Brandon Low <lostlogic@gentoo.org>
5 #
6 # Previous version (from which I've borrowed a few bits) by:
7 # Jochem Kossen <j.kossen@home.nl>
8 # Leo Lipelis <aeoo@gentoo.org>
9 # Karl Trygve Kalleberg <karltk@gentoo.org>
10 #
11 # $Header: /home/cvsd/magellan-cvs/magellan-src/mage/usr/lib/mage/etc-update,v 1.6 2005-02-15 22:59:09 niro Exp $
12
13 # Modified by Niels Rogalla <niro@magellan-linux.de> to use this with Magellan-Linux
14 # version: 0.3.6-r13
15
16 export PORTAGE_CALLER="etc-update"
17
18 function get_config() {
19 item=$1
20
21 # First strip off comment lines, then grab the configuration
22 # item. If there's more than one of the same configuration item,
23 # then allow the last setting to take precedence.
24 cut -d'#' -f1-1 /etc/etc-update.conf | \
25 sed -ne "s/^\ *$item\ *=\ *\"\(.*\)\"/\1/p" |sed -e '$p;d'
26 }
27
28 function scan() {
29
30 echo "Scanning Configuration files..."
31 rm -rf ${TMP}/files > /dev/null 2>&1
32 mkdir ${TMP}/files || die "Failed mkdir command!" 1
33 count=0
34 input=0
35
36 for path in ${CONFIG_PROTECT}; do if [ -d ${path} ]; then
37 ofile=""
38 for file in `find ${path} -iname "._cfg????_*" |
39 sed -e "s:\(^.*/\)\(._cfg*_\)\(/.*$\):\1\2\3\%\2\%\3:" |
40 sort -t'%' -k3 -k2 | cut -f1 -d'%'`; do
41 rpath=`echo "${file}" | sed -e "s:/[^/]*$::"`
42 rfile=`echo "${file}" | sed -e "s:^.*/::"`
43 for mpath in ${CONFIG_PROTECT_MASK}; do
44 if [[ "${rpath}" == "${mpath}"* ]]; then
45 mv ${rpath}/${rfile} ${rpath}/${rfile:10}
46 break
47 fi
48 done
49 [ ! -f ${file} ] && continue
50
51
52 if [[ "${ofile:10}" != "${rfile:10}" ]] ||
53 [[ ${opath} != ${rpath} ]]; then
54 MATCHES=0
55 if [[ "${EU_AUTOMERGE}" == "yes" ]]; then
56 if [ ! -e "${rpath}/${rfile}" ] || [ ! -e "${rpath}/${rfile:10}" ]; then
57 MATCHES=0
58 else
59 diff -Bbua ${rpath}/${rfile} ${rpath}/${rfile:10} | egrep '^[+-]' | egrep -v '^[+-][\t ]*#|^--- |^\+\+\+ ' | egrep -qv '^[-+][\t ]*$'
60 MATCHES=$?
61 fi
62 elif [[ -z `diff -Nua ${rpath}/${rfile} ${rpath}/${rfile:10}|
63 grep "^[+-][^+-]"|grep -v '# .Header:.*'` ]]; then
64 MATCHES=1
65 fi
66 if [[ "${MATCHES}" == "1" ]]; then
67 echo "Automerging trivial changes in: ${rfile:10}"
68 mv ${rpath}/${rfile} ${rpath}/${rfile:10}
69 continue
70 else
71 count=${count}+1
72 echo "${rpath}/${rfile:10}" > ${TMP}/files/${count}
73 echo "${rpath}/${rfile}" >> ${TMP}/files/${count}
74 ofile="${rfile}"
75 opath="${rpath}"
76 continue
77 fi
78 fi
79
80 if [[ -z `diff -Nua ${rpath}/${rfile} ${rpath}/${ofile}|
81 grep "^[+-][^+-]"|grep -v '# .Header:.*'` ]]; then
82 mv ${rpath}/${rfile} ${rpath}/${ofile}
83 continue
84 else
85 echo "${rpath}/${rfile}" >> ${TMP}/files/${count}
86 ofile="${rfile}"
87 opath="${rpath}"
88 fi
89 done
90 fi; done
91
92 }
93
94 function sel_file() {
95 local -i isfirst=0
96 until [ -f ${TMP}/files/${input} ] || [ ${input} == -1 ] || [ ${input} == -3 ]; do
97 for file in `ls ${TMP}/files|sort -n`; do
98 if (( ${isfirst} == 0 )); then
99 isfirst=${file}
100 fi
101 echo -n "${file}${PAR} "
102 if (( ${mode} == 0 )); then
103 for word in `cat ${TMP}/files/${file}`; do
104 echo ${word}
105 done
106 else
107 head -n1 ${TMP}/files/${file}
108 fi
109 done > ${TMP}/menuitems
110
111 if [ "${OVERWRITE_ALL}" == "yes" ]; then
112 input=0
113 else
114 if (( ${mode} == 0 )); then
115 echo "The following is the list of files which need updating, each
116 configuration file is followed by a list of possible replacement files."
117 else
118 local my_title="Please select a file to update"
119 fi
120
121 if (( ${mode} == 0 )); then
122 cat ${TMP}/menuitems
123 echo "Please select a file to edit by entering the corresponding number."
124 echo " (-1 to exit) (-3 to auto merge all remaining files)"
125 echo -n " (-5 to auto-merge AND not use 'mv -i'): "
126 read input
127 else
128 dialog --title "${title}" --menu "${my_title}" \
129 0 0 0 `echo "-1 Exit";cat ${TMP}/menuitems` \
130 2> ${TMP}/input
131 input=`cat ${TMP}/input`
132 fi
133 if (( ${input} == -5 )); then
134 input=-3
135 export mv_opts=""
136 fi
137 if (( ${input} == -3 )); then
138 input=0
139 export OVERWRITE_ALL="yes"
140 fi
141 fi # -3 automerge
142 if (( ${input} == 0 )); then
143 input=${isfirst}
144 fi
145 done
146 }
147
148 function do_file() {
149 echo
150 local -i my_input
151 local -i fcount=0
152 until (( `cat ${TMP}/files/${input}|wc -l` < 2 )); do
153 my_input=0
154 if (( `cat ${TMP}/files/${input}|wc -l` == 2 )); then
155 my_input=1
156 fi
157 until (( ${my_input} > 0 )) && (( ${my_input} < `cat ${TMP}/files/${input}|wc -l` )); do
158 fcount=0
159
160 if [ "${OVERWRITE_ALL}" == "yes" ]; then
161 my_input=0
162 else
163 for line in `cat ${TMP}/files/${input}`; do
164 if (( ${fcount} > 0 )); then
165 echo -n "${fcount}${PAR} "
166 echo "${line}"
167 else
168 if (( ${mode} == 0 )); then
169 echo "Below are the new config files for ${line}:"
170 else
171 local my_title="Please select a file to process for ${line}"
172 fi
173 fi
174 fcount=${fcount}+1
175 done > ${TMP}/menuitems
176
177 if (( ${mode} == 0 )); then
178 cat ${TMP}/menuitems
179 echo -n "Please select a file to process (-1 to exit this file): "
180 read my_input
181 else
182 dialog --title "${title}" --menu "${my_title}" \
183 0 0 0 `cat ${TMP}/menuitems;echo "${fcount} Exit"` \
184 2> ${TMP}/input
185 my_input=`cat ${TMP}/input`
186 fi
187 fi # OVERWRITE_ALL
188
189 if (( ${my_input} == 0 )); then
190 my_input=1
191 elif (( ${my_input} == -1 )); then
192 return
193 elif (( ${my_input} == ${fcount} )); then
194 break
195 fi
196 done
197 if (( ${my_input} == ${fcount} )); then
198 break
199 fi
200
201 fcount=${my_input}+1
202
203 file=`cat ${TMP}/files/${input} | sed -e "${fcount}p;d"`
204 ofile=`head -n1 ${TMP}/files/${input}`
205
206 do_cfg "${file}" "${ofile}"
207
208 cat ${TMP}/files/${input}|sed -e "${fcount}!p;d" > ${TMP}/files/sed
209 mv ${TMP}/files/sed ${TMP}/files/${input}
210
211 if (( ${my_input} == -1 )); then
212 break
213 fi
214 done
215 echo
216 rm ${TMP}/files/${input}
217 count=${count}-1
218 }
219
220 function do_cfg() {
221
222 local file="${1}"
223 local ofile="${2}"
224 local -i my_input=0
225
226 until (( ${my_input} == -1 )) || [ ! -f ${file} ]; do
227 if [ "${OVERWRITE_ALL}" == "yes" ]; then
228 my_input=1
229 else
230 showdiffcmd=$(echo "${diff_command}" |
231 sed -e "s:%file1:${ofile}:" -e "s:%file2:${file}:")
232
233 if [ "${using_editor}" == 0 ]; then
234 (
235 echo "Showing differences between ${ofile} and ${file}"
236 ${showdiffcmd}
237 ) | ${pager}
238 else
239 echo "Beginning of differences between ${ofile} and ${file}"
240 ${showdiffcmd}
241 echo "End of differences between ${ofile} and ${file}"
242 fi
243 if [ -L "${file}" ]; then
244 echo
245 echo "-------------------------------------------------------------"
246 echo "NOTE: File is a symlink to another file. REPLACE recommended."
247 echo " The original file may simply have moved. Please review."
248 echo "-------------------------------------------------------------"
249 echo
250 fi
251 echo -n "1) Replace original with update
252 2) Delete update, keeping original as is
253 3) Interactively merge original with update
254 4) Show differences again
255 Please select from the menu above (-1 to ignore this update): "
256 read my_input
257 fi
258
259 case ${my_input} in
260 1) echo "Replacing ${ofile} with ${file}"
261 mv ${mv_opts} ${file} ${ofile}
262 continue
263 ;;
264 2) echo "Deleting ${file}"
265 rm ${rm_opts} ${file}
266 continue
267 ;;
268 3) do_merge "${file}" "${ofile}"
269 my_input=${?}
270 # [ ${my_input} == 255 ] && my_input=-1
271 continue
272 ;;
273 4) continue
274 ;;
275 *) continue
276 ;;
277 esac
278 done
279 }
280
281 function do_merge() {
282
283 local file="${1}"
284 local ofile="${2}"
285 local mfile="${2}.merged"
286 local -i my_input=0
287 echo "${file} ${ofile} ${mfile}"
288
289 if [ -e ${mfile} ] ; then
290 echo "A previous version of the merged file exists, cleaning..."
291 rm ${rm_opts} ${mfile}
292 fi
293
294 until (( ${my_input} == -1 )); do
295 echo "Merging ${file} and ${ofile}"
296 `echo "${merge_command}" |
297 sed -e "s:%merged:${mfile}:g" \
298 -e "s:%orig:${ofile}:g" \
299 -e "s:%new:${file}:g"`
300 until (( ${my_input} == -1 )); do
301 echo -n "1) Replace ${ofile} with merged file
302 2) Show differences between merged file and original
303 3) Remerge original with update
304 4) Edit merged file
305 5) Return to the previous menu
306 Please select from the menu above (-1 to exit, losing this merge): "
307 read my_input
308 case ${my_input} in
309 1) echo "Replacing ${ofile} with ${mfile}"
310 chmod --reference=${ofile} ${mfile}
311 mv ${mv_opts} ${mfile} ${ofile}
312 rm ${rm_opts} ${file}
313 return 255
314 ;;
315 2) ( echo "Showing differences between ${ofile} and ${mfile}"
316 `echo "${diff_command}" | \
317 sed -e "s:%file1:${ofile}:" \
318 -e "s:%file2:${mfile}:"` ) | ${pager}
319 continue
320 ;;
321 3) break
322 ;;
323 4) ${EDITOR:-nano -w} "${mfile}"
324 continue
325 ;;
326 5) rm ${rm_opts} ${mfile}
327 return 0
328 ;;
329 *) continue
330 ;;
331 esac
332 done
333 done
334 rm ${rm_opts} ${mfile}
335 return 255
336 }
337
338 function die() {
339 trap "" term
340 trap "" kill
341 echo "Exiting: ${1}"
342 rm -rf ${TMP}
343 exit ${2}
344 }
345
346 #
347 # Run the script
348 #
349 scriptname=`basename $0`
350
351 trap die term
352
353 TMP=/tmp/$$
354 rm -rf ${TMP} 2> /dev/null
355 mkdir ${TMP} || die "failed mkdir command!" 1
356
357 # I need the CONFIG_PROTECT value
358 #CONFIG_PROTECT=$(/usr/lib/portage/bin/portageq config_protect)
359 #CONFIG_PROTECT_MASK=$(/usr/lib/portage/bin/portageq config_protect_mask)
360 #source /etc/CONFIG_PROTECT
361 source /etc/profile
362
363 # load etc-config's configuration
364 EU_AUTOMERGE=`get_config eu_automerge`
365 rm_opts=`get_config rm_opts`
366 mv_opts=`get_config mv_opts`
367 cp_opts=`get_config cp_opts`
368 pager=`get_config pager`
369 diff_command=`get_config diff_command`
370 using_editor=`get_config using_editor`
371 merge_command=`get_config merge_command`
372 declare -i mode=`get_config mode`
373 [ -z ${mode} ] && mode=0
374 [ -z "${pager}" ] && pager="cat"
375
376 #echo "rm_opts: $rm_opts, mv_opts: $mv_opts, cp_opts: $cp_opts"
377 #echo "pager: $pager, diff_command: $diff_command, merge_command: $merge_command"
378
379 if (( ${mode} == 0 )); then
380 PAR=")"
381 else
382 PAR=""
383 rm_opts=""
384 mv_opts=""
385 cp_opts=""
386 pager=""P
387 fi
388
389 declare -i count=0
390 declare -i input=0
391 declare title="Gentoolkit's etc-update tool!"
392
393 scan
394
395 until (( ${input} == -1 )); do
396 if (( ${count} == 0 )); then
397 die "Nothing left to do; exiting. :)" 0
398 fi
399 sel_file
400 if (( ${input} != -1 )); then
401 do_file
402 fi
403 done
404
405 die "User termination!" 0

Properties

Name Value
svn:executable *