head	1.13;
access;
symbols
	GENTOO_1_4_SNAP_2003010800:1.12;
locks; strict;
comment	@# @;


1.13
date	2003.02.14.21.55.18;	author vapier;	state dead;
branches;
next	1.12;

1.12
date	2002.07.15.14.37.32;	author lostlogic;	state Exp;
branches;
next	1.11;

1.11
date	2002.07.15.03.23.58;	author lostlogic;	state Exp;
branches;
next	1.10;

1.10
date	2002.07.14.22.39.40;	author lostlogic;	state Exp;
branches;
next	1.9;

1.9
date	2002.06.26.23.13.14;	author lostlogic;	state Exp;
branches;
next	1.8;

1.8
date	2002.06.26.23.07.51;	author lostlogic;	state Exp;
branches;
next	1.7;

1.7
date	2002.06.26.16.56.13;	author lostlogic;	state Exp;
branches;
next	1.6;

1.6
date	2002.06.26.06.32.26;	author lostlogic;	state Exp;
branches;
next	1.5;

1.5
date	2002.06.26.05.05.33;	author lostlogic;	state Exp;
branches;
next	1.4;

1.4
date	2002.06.26.04.20.21;	author lostlogic;	state Exp;
branches;
next	1.3;

1.3
date	2002.06.19.02.53.14;	author lostlogic;	state Exp;
branches;
next	1.2;

1.2
date	2002.06.03.21.59.22;	author karltk;	state Exp;
branches;
next	1.1;

1.1
date	2002.03.23.00.09.07;	author karltk;	state Exp;
branches;
next	;


desc
@@


1.13
log
@etc-update moved to portage
@
text
@#!/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: /home/cvsroot/gentoo-x86/app-admin/gentoolkit/files/etc-update/etc-update,v 1.12 2002/07/15 14:37:32 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" |sed -e '$p;d'
}

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:^.*/::"`
			for mpath in ${CONFIG_PROTECT_MASK}; do
				if [[ "${rpath}" == "${mpath}"* ]]; then
					mv ${rpath}/${rfile} ${rpath}/${rfile:10}
					break
				fi
			done
			[ ! -f ${file} ] && continue
			if [[ "${ofile:10}" != "${rfile:10}" ]] || 
			   [[ ${opath} != ${rpath} ]]; then
				if [[ -z `diff -Nua ${rpath}/${rfile} ${rpath}/${rfile:10}|
						  grep "^[+-][^+-]"|grep -v '# .Header:.*'` ]]; 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:.*'` ]]; 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() {
	local -i isfirst=0
	until [ -f ${TMP}/files/${input} ] || [ ${input} == -1 ]; do
		if (( ${mode} == 0 )); then
			echo "The following is the list of files which need updating, each 
configuration file is followed by a list of possible replacement files."
		else
			local my_title="Please select a file to update"
		fi
		for file in `ls ${TMP}/files|sort -n`; do
			if (( ${isfirst} == 0 )); then
				isfirst=${file}
			fi
			echo -n "${file}${PAR} "
			if (( ${mode} == 0 )); then
				for word in `cat ${TMP}/files/${file}`; do
					echo ${word}
				done
			else
				head -n1 ${TMP}/files/${file}
			fi
		done > ${TMP}/menuitems
		if (( ${mode} == 0 )); then
			cat ${TMP}/menuitems
			echo -n "Please select a file to edit by entering the corresponding number (-1 to exit): "
			read input
		else
			dialog --title "${title}" --menu "${my_title}" \
				0 0 0 `echo "-1 Exit";cat ${TMP}/menuitems` \
				2> ${TMP}/input
			input=`cat ${TMP}/input`
		fi		
		if (( ${input} == 0 )); then
			input=${isfirst}
		fi
	done
}

function do_file() {
	echo
	local -i my_input
	local -i fcount=0
	until (( `cat ${TMP}/files/${input}|wc -l` < 2 )); do
		my_input=0
		if (( `cat ${TMP}/files/${input}|wc -l` == 2 )); then
			my_input=1
		fi
		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}${PAR} "
					echo "${line}"
				else
					if (( ${mode} == 0 )); then
						echo "Below are the new config files for ${line}:"
					else
						local my_title="Please select a file to process for ${line}"
					fi
				fi
				fcount=${fcount}+1
			done > ${TMP}/menuitems
			if (( ${mode} == 0 )); then
				cat ${TMP}/menuitems
				echo -n "Please select a file to process (-1 to exit this file): "
				read my_input
			else
				dialog --title "${title}" --menu "${my_title}" \
					0 0 0 `cat ${TMP}/menuitems;echo "${fcount} Exit"` \
					2> ${TMP}/input
				my_input=`cat ${TMP}/input`
			fi
			if (( ${my_input} == 0 )); then
				my_input=1
			elif (( ${my_input} == ${fcount} )); then
				break
			fi
		done
		if (( ${my_input} == ${fcount} )); then
			break
		fi

		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}

		if (( ${my_input} == -1 )); then
			break
		fi
	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 ${ofile} and ${file}"
		  `echo "${diff_command}" |
		   sed -e "s:%file1:${ofile}:" \
			   -e "s:%file2:${file}:"` )
			   # | ${pager}
		echo -n "1) Replace original with update
2) Delete update, keeping original as is
3) Interactively merge original with update
4) Show differences again
Please select from the menu above (-1 to ignore this update): "
		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 original
3) Remerge original with update
4) Return to the previous menu
Please select from the menu above (-1 to exit, losing this merge): "
			read my_input
			case ${my_input} in
				1) echo "Replacing ${ofile} with ${mfile}"
				   chmod --reference=${ofile} ${mfile}
				   mv ${mv_opts} ${mfile} ${ofile}
				   rm ${rm_opts} ${file}
				   return 255
				   ;;
				2) ( echo "Showing differences between ${ofile} and ${mfile}"
					 `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`
declare -i mode=`get_config mode`
[ -z ${mode} ] && mode=0
[ -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" 

if (( ${mode} == 0 )); then
	PAR=")"
else
	PAR=""
	rm_opts=""
	mv_opts=""
	cp_opts=""
	pager=""P
fi

declare -i count=0
declare -i input=0
declare title="Gentoolkit's etc-update tool!"

scan

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

die "User termination!" 0
@


1.12
log
@typos stink
@
text
@d11 1
a11 1
# $Header: /home/cvsroot/gentoo-x86/app-admin/gentoolkit/files/etc-update/etc-update,v 1.10 2002/07/14 22:39:40 lostlogic Exp $
@


1.11
log
@tiny issue only effected users of colortail
@
text
@d20 1
a20 1
		sed -ne "s/^\ *$item\ *=\ *\"\(.*\)\"/\1/p" |sed -e "$p;d"
d115 1
a115 1
	local -i fcount
d118 4
a121 3
		(( `cat ${TMP}/files/${input}|wc -l` == 2 )) && my_input=1
		until (( ${my_input} > 0 )) && \
			  (( ${my_input} < `cat ${TMP}/files/${input}|wc -l` )); do
d182 1
a182 1
		( echo "Showing differences between ${file} and ${ofile}"
d185 2
a186 1
			   -e "s:%file2:${file}:"` ) | ${pager}
d247 1
a247 1
				2) ( echo "Showing differences between ${mfile} and ${ofile}"
d309 1
a309 1
	pager=""
@


1.10
log
@New gentoolkit revision fixes fairly critical bug in etc-update
etc-update didn't keep perms on merged files.  Other etc-update bugfixes
Add feature to qpkg.
@
text
@d11 1
a11 1
# $Header: /home/cvsroot/gentoo-x86/app-admin/gentoolkit/files/etc-update/etc-update,v 1.9 2002/06/26 23:13:14 lostlogic Exp $
d20 1
a20 1
		sed -ne "s/^\ *$item\ *=\ *\"\(.*\)\"/\1/p" | tail -1
@


1.9
log
@trance whined more
@
text
@d11 1
a11 1
# $Header: $
d48 1
a48 1
						  grep "^[+-][^+-]"|grep -v '# $Header:.*$'` ]]; then
d55 1
a55 1
			    	ofile="${rfile}"
d61 1
a61 1
					  grep "^[+-][^+-]"|grep -v '# $Header:.*$'` ]]; then
d75 1
a75 1
#	[ ! -z `ls ${TMP}/files` ] && \
d77 2
a78 1
		echo "The following is the list of files which need updating, each 
d80 3
d84 25
a108 7
			    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
d124 1
a124 1
					echo -n "${fcount}) "
d127 5
a131 1
					echo "Below are the new config files for ${line}:"
d134 16
a149 4
			done
			echo -n "Please enter the number of the file to process (-1 to exit this file): "
			read my_input
			[ ${my_input} == -1 ] && break
d151 3
a153 1
		[ ${my_input} == -1 ] && break
d165 3
a167 1
		[ ${my_input} == -1 ] && break
d185 3
a187 3
		echo -n "1) Replace ${ofile} with ${file}
2) Delete ${file}, keeping ${ofile} as is
3) Interactively merge ${ofile} with ${file}
d189 1
a189 1
Please select from the menu above (-1 to exit, ignoring this updated file):"
d233 2
a234 2
2) Show differences between merged file and ${ofile}
3) Remerge ${ofile} with ${file}
d236 1
a236 1
Please select from the menu above (-1 to exit, ignoring ${file}): "
d240 1
d293 2
d300 10
d312 1
d317 3
a319 1
	[ ${count} == 0 ] && die "No files to work on!" 0
d321 3
a323 1
	[ ${input} != -1 ] && do_file
@


1.8
log
@just adding CONFIG_PROTEC_MASK awareness cuz trance wanted it
@
text
@d39 1
a39 1
				if [[ "${rpath}" == "${mpath}" ]]; then
d45 2
a46 1
			if [[ "${ofile:10}" != "${rfile:10}" ]] || [[ ${opath} != ${rpath} ]]; then
@


1.7
log
@now asks about binary files (annoying but necessary)
@
text
@d38 7
@


1.6
log
@more
@
text
@d39 1
a39 1
				if [[ -z `diff -Nu ${rpath}/${rfile} ${rpath}/${rfile:10}|
d52 1
a52 1
			if [[ -z `diff -Nu ${rpath}/${rfile} ${rpath}/${ofile}|
@


1.5
log
@minor typo introduced during last minute touchup, fixed.
@
text
@d88 1
a88 1
		(( `cat ${TMP}/files/${input}|wc -l` == 2 )) && my_input=1 && fcount=2
d106 2
@


1.4
log
@etc-update rewrite and qpkg bugfix in one pretty new gentoolkit
@
text
@d38 1
a38 1
			if [[ "${ofile:10}" != "${file:10}" ]]; then
d48 1
d59 1
@


1.3
log
@quick fix for exiting on skip... probably more to come
@
text
@d3 18
a20 13
# Distributed under the terms of the GNU General Public License, v2 or later
# Author: Jochem Kossen <j.kossen@@home.nl>, (c) 2002
# Author: Leo Lipelis <aeoo@@gentoo.org>, (c) 2002
# Author: Karl Trygve Kalleberg <karltk@@gentoo.org>, (c) 2002

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
d23 7
a29 6
#
# Ask which one of the given two files to install
#
rm_extra_file() {
	old=$1
	new=$2
d31 31
a61 30
	show_diff $old $new
	menu1
	
	# read and echo 1 char from stdin
	read input
	echo
	case $input in
	1)
		echo "*** upgrading to $new ..."
		mv $mv_opts $new $old
		;;
	2)
		echo "*** keeping $old ..."
		rm $rm_opts $new
		;;
	3)
		merge_files $old $new
		install_merged_file $old $new
		;;
	4)
		rm_extra_file $old $new
		;;
	5)
		echo "*** skipping ..."
		;;
	*)
		echo "!!! Please pick a valid choice next time !!!"
		menu1
		;;
	esac
d64 14
a77 14
#
# Show menu1
#
menu1() {
echo
echo
echo "1) Upgrade to new $new"
echo "2) Keep existing $old"
echo "3) Merge the two files"
echo "4) Show the difference between the two files again"
echo "  OR"
echo "5) Skip (keep all files)"
echo
echo -n "Type (1, 2, 3, 4 or 5): "									
d80 32
a111 16
#
# Show menu2
#
menu2() {
echo
echo
echo "1) Upgrade to merged file"
echo "2) Show the difference between original and merged file"
echo "3) Redo the merge"
echo "4) Keep original file"
echo "5) Skip (keep all files)"
echo "  OR"
echo "6) Back to previous menu"
echo
echo -n "Type (1, 2, 3, 4, 5 or 6): "
}
d113 2
a114 11
#
# Install merged file
#
install_merged_file() {
	old=$1
	merged=$1.merged
	new=$2

	menu2
	# read and echo 1 char from stdin
	read input
d116 2
a117 35
	case $input in
	1)
		echo "*** upgrading to $merged ..."
		mv $mv_opts $merged $old
		rm $rm_opts $new
		;;
	2)
		show_diff $old $merged
		install_merged_file $old $new
		;;
	3)
		merge_files $old $new
		install_merged_file $old $new
		;;
	4)
		echo "*** keeping original file ..."
		rm $rm_opts $merged $new
		;;
	5)
		echo "*** skipping ..."
		;;
	6)
		echo "*** going back to previous menu ..."
		echo
		if [ -e $merged ]; then
			echo "*** an (old?) merged file exists. It will be removed ..."
			rm $rm_opts $merged
		fi
		rm_extra_file $old $new
		;;
	*)
		echo "!!! Please pick a valid choice next time !!!"
		menu2
		;;
	esac
d120 36
a155 16
#
# Show the difference between two files
#
show_diff() {
	echo
	if [ "`echo $pager`" ]; then
		(echo "*** showing difference between $1 and $2" && echo && \
		`echo $diff_command | sed \
		-e s@@%file1@@$1@@g \
		-e s@@%file2@@$2@@g` ) | $pager
	else
		echo "*** showing difference between $1 and $2" && echo
		`echo $diff_command | sed \
		-e s@@%file1@@$1@@g \
		-e s@@%file2@@$2@@g`
	fi
d158 12
a169 7
#
# Merge two files
#
merge_files() {
	old=$1
	merged=$1.merged
	new=$2
d171 37
a207 2
	echo
	echo "*** merging $old with $new ..."
d209 6
a214 12
	if [ -e $merged ]; then
		echo
		echo "*** an (old?) merged file already exists. It will be removed ..."
		echo
		rm $rm_opts $merged
	fi
	
	# execute the merge command
	`echo $merge_command |sed \
	-e s@@%merged@@$merged@@g \
	-e s@@%orig@@$old@@g \
	-e s@@%new@@$new@@g`
d222 6
d238 1
d241 1
a241 1
#echo "pager: $pager, diff_command: $diff_command, merge_command: $merge_command"
d243 2
a244 8
#
# Find all "new" configuration files, sort, so that ._cfg_0000 is presented
# before ._cfg_0001
#
#set -o xtrace
for cfg_dir in $CONFIG_PROTECT; do
    if [ -d "$cfg_dir" ] ; then
	cfg_files=`find $cfg_dir -iname '._cfg????_*' | sort`
d246 1
a246 6
	if [ -z "$cfg_files" ] ; then
	    echo "!!! No config files to update in $cfg_dir"
	fi
	for new_full_path in $cfg_files; do
	    file=${new_full_path##*/}
	    old_full_path=${new_full_path%/*}/${file:10}
d248 4
a251 5
	    rm_extra_file ${old_full_path} ${new_full_path}
	done
    else
	echo "!!! Skipping non-existant directory $cfg_dir"
    fi
a252 1
echo "*** script finished ..."
d254 1
@


1.2
log
@Fixes #1689
@
text
@a48 1
		exit
@


1.1
log
@Major rework. #544, #1244
@
text
@a7 2
scriptname=`basename $0`

d15 1
a15 1
	sed -ne "s/^\ *$item\ *=\ *\"\(.*\)\"/\1/p" | tail -1
a17 17
# 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`

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

#
# Find all "new" configuration files, sort, so that ._cfg_0000 is presented
# before ._cfg_0001
#
cfg_files=`find /etc -iname '._cfg????_*' | sort`

d186 1
a186 6
if [ -z "$cfg_files" ] ; then
	echo "!!! No config files to update"
fi
for new_full_path in $cfg_files; do
	file=${new_full_path##*/}
	old_full_path=${new_full_path%/*}/${file:10}
d188 35
a222 1
	rm_extra_file ${old_full_path} ${new_full_path}
@

