summaryrefslogtreecommitdiff
blob: dfc3c8f1e0ae61d635ed7ff93ffb283386e417eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
# Copyright 2002 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU Public License, v2 or later
# Author Brandon Low <lostlogic@gentoo.org>
#
# Previous version (from which I've borrowed a few bits) by:
# Jochem Kossen <j.kossen@home.nl>
# Leo Lipelis <aeoo@gentoo.org>
# Karl Trygve Kalleberg <karltk@gentoo.org>
#
# $Header: /var/cvsroot/gentoo-x86/app-admin/gentoolkit/files/etc-update/etc-update,v 1.7 2002/06/26 16:56:13 lostlogic Exp $

function get_config() {
	item=$1

	# First strip off comment lines, then grab the configuration
	# item. If there's more than one of the same configuration item,
	# then allow the last setting to take precedence.
	cut -d'#' -f1-1 /etc/etc-update.conf | \
		sed -ne "s/^\ *$item\ *=\ *\"\(.*\)\"/\1/p" | tail -1
}

function scan() {

	echo "Scanning Configuration files..."
	rm -rf ${TMP}/files > /dev/null 2>&1
	mkdir ${TMP}/files || die "Failed mkdir command!" 1
	count=0
	input=0
	
	for path in ${CONFIG_PROTECT}; do if [ -d ${path} ]; then
		ofile=""
		for file in `find ${path} -iname "._cfg????_*" |
			   sed -e "s:\(^.*/\)\(._cfg*_\)\(/.*$\):\1\2\3\%\2\%\3:" |
			   sort -t'%' -k3 -k2 | cut -f1 -d'%'`; do
			rpath=`echo "${file}" | sed -e "s:/[^/]*$::"`
			rfile=`echo "${file}" | sed -e "s:^.*/::"`
			if [[ "${ofile:10}" != "${rfile:10}" ]] || [[ ${opath} != ${rpath} ]]; then
				if [[ -z `diff -Nua ${rpath}/${rfile} ${rpath}/${rfile:10}|
						  grep "^[+-][^+-]"|grep -v '# $Header: /var/cvsroot/gentoo-x86/app-admin/gentoolkit/files/etc-update/etc-update,v 1.7 2002/06/26 16:56:13 lostlogic Exp $'` ]]; then
					mv ${rpath}/${rfile} ${rpath}/${rfile:10}
					continue
				else
					count=${count}+1
					echo "${rpath}/${rfile:10}" > ${TMP}/files/${count}
					echo "${rpath}/${rfile}" >> ${TMP}/files/${count}
			    	ofile="${rfile}"
					opath="${rpath}"
					continue
				fi
			fi
			if [[ -z `diff -Nua ${rpath}/${rfile} ${rpath}/${ofile}|
					  grep "^[+-][^+-]"|grep -v '# $Header: /var/cvsroot/gentoo-x86/app-admin/gentoolkit/files/etc-update/etc-update,v 1.7 2002/06/26 16:56:13 lostlogic Exp $'` ]]; then
				mv ${rpath}/${rfile} ${rpath}/${ofile}
				continue
			else
				echo "${rpath}/${rfile}" >> ${TMP}/files/${count}
				ofile="${rfile}"
				opath="${rpath}"
			fi
		done
	fi; done

}

function sel_file() {
#	[ ! -z `ls ${TMP}/files` ] && \
	until [ -f ${TMP}/files/${input} ] || [ ${input} == -1 ]; do
		echo "The following is the list of files which need updating, each 
configuration file is followed by a list of possible replacement files."
		for file in `ls ${TMP}/files|sort -n`; do
			    echo -n "${file}) "
		    for word in `cat ${TMP}/files/${file}`; do
				echo ${word}
			done
		done
		echo -n "Please select a file to edit by entering the corresponding number (-1 to exit): "
		read input
	done
}

function do_file() {
	echo
	local -i my_input
	local -i fcount
	until (( `cat ${TMP}/files/${input}|wc -l` < 2 )); do
		my_input=0
		(( `cat ${TMP}/files/${input}|wc -l` == 2 )) && my_input=1
		until (( ${my_input} > 0 )) && \
			  (( ${my_input} < `cat ${TMP}/files/${input}|wc -l` )); do
			fcount=0
			for line in `cat ${TMP}/files/${input}`; do
				if (( ${fcount} > 0 )); then
					echo -n "${fcount}) "
					echo "${line}"
				else
					echo "Below are the new config files for ${line}:"
				fi
				fcount=${fcount}+1
			done
			echo -n "Please enter the number of the file to process (-1 to exit this file): "
			read my_input
			[ ${my_input} == -1 ] && break
		done
		[ ${my_input} == -1 ] && break

		fcount=${my_input}+1

		file=`cat ${TMP}/files/${input} | sed -e "${fcount}p;d"`
		ofile=`head -n1 ${TMP}/files/${input}`

		do_cfg "${file}" "${ofile}"

		cat ${TMP}/files/${input}|sed -e "${fcount}!p;d" > ${TMP}/files/sed
		mv ${TMP}/files/sed ${TMP}/files/${input}

		[ ${my_input} == -1 ] && break
	done
	echo
	rm ${TMP}/files/${input}
	count=${count}-1
}

function do_cfg() {
	
	local file="${1}"
	local ofile="${2}"
	local -i my_input=0
	
	until (( ${my_input} == -1 )); do
		( echo "Showing differences between ${file} and ${ofile}"
		  `echo "${diff_command}" |
		   sed -e "s:%file1:${ofile}:" \
			   -e "s:%file2:${file}:"` ) | ${pager}
		echo -n "1) Replace ${ofile} with ${file}
2) Delete ${file}, keeping ${ofile} as is
3) Interactively merge ${ofile} with ${file}
4) Show differences again
Please select from the menu above (-1 to exit, ignoring this updated file):"
		read my_input
		case ${my_input} in
			1) echo "Replacing ${ofile} with ${file}"
			   mv ${mv_opts} ${file} ${ofile}
			   break
			   ;;
			2) echo "Deleting ${file}"
			   rm ${rm_opts} ${file}
			   break
			   ;;
			3) do_merge "${file}" "${ofile}"
			   my_input=${?}
			   [ ${my_input} == 255 ] && my_input=-1
			   ;;
			4) continue
			   ;;
			*) continue
			   ;;
		esac
	done
}

function do_merge() {
	
	local file="${1}"
	local ofile="${2}"
	local mfile="${2}.merged"
	local -i my_input=0
	echo "${file} ${ofile} ${mfile}"

	if [ -e ${mfile} ] ; then
		echo "A previous version of the merged file exists, cleaning..."
		rm ${rm_opts} ${mfile}
	fi

	until (( ${my_input} == -1 )); do
		echo "Merging ${file} and ${ofile}"
		`echo "${merge_command}" |
		 sed -e "s:%merged:${mfile}:g" \
		 	 -e "s:%orig:${ofile}:g" \
			 -e "s:%new:${file}:g"`
		until (( ${my_input} == -1 )); do	
			echo -n "1) Replace ${ofile} with merged file
2) Show differences between merged file and ${ofile}
3) Remerge ${ofile} with ${file}
4) Return to the previous menu
Please select from the menu above (-1 to exit, ignoring ${file}): "
			read my_input
			case ${my_input} in
				1) echo "Replacing ${ofile} with ${mfile}"
				   mv ${mv_opts} ${mfile} ${ofile}
				   rm ${rm_opts} ${file}
				   return 255
				   ;;
				2) ( echo "Showing differences between ${mfile} and ${ofile}"
					 `echo "${diff_command}" | \
					  sed -e "s:%file1:${ofile}:" \
					  	  -e "s:%file2:${mfile}:"` ) | ${pager}
				   continue
				   ;;
				3) break
				   ;;
				4) rm ${rm_opts} ${mfile}
				   return 0
				   ;;
				*) continue
				   ;;
			esac
		done
	done
	return 255
}

function die() {
	trap "" term
	trap "" kill
	echo "Exiting: ${1}"
	rm -rf ${TMP}
	exit ${2}
}

#
# Run the script
#
scriptname=`basename $0`

trap die term

TMP=/tmp/$$
rm -rf ${TMP} 2> /dev/null
mkdir ${TMP} || die "failed mkdir command!" 1

# I need the CONFIG_PROTECT value
source /etc/make.globals

# load etc-config's configuration
rm_opts=`get_config rm_opts`
mv_opts=`get_config mv_opts`
cp_opts=`get_config cp_opts`
pager=`get_config pager`
diff_command=`get_config diff_command`
merge_command=`get_config merge_command`
[ -z ${pager} ] && pager="cat"

#echo "rm_opts: $rm_opts, mv_opts: $mv_opts, cp_opts: $cp_opts"
#echo "pager: $pager, diff_command: $diff_command, merge_command: $merge_command" 

declare -i count=0
declare -i input=0

scan

until (( ${input} == -1 )); do
	[ ${count} == 0 ] && die "No files to work on!" 0
	sel_file
	[ ${input} != -1 ] && do_file
done

die "User termination!" 0