#!/bin/bash
# Copyright 2002 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License, v2 or later
# Authors: (C) Jochem Kossen 2002, (C) Leo Lipelis 2002
# 
# Modified for Linux-Mandrake by Han Boetes 2002

#
# Customization is done in a configuration file. You don't need to edit this
# file anymore. It also means settings will be saved during upgrading.
#
configfile="/etc/etc-updaterc"
configdirs="/etc /usr/share/config"

#
# Source the configurationfile
#
if [ -e $configfile ]; then
	source $configfile
fi

#
# Make sure required variables are defined
#
if [ ! "`echo $diff_command`" ]; then
	diff_command="diff -uN %file1 %file2"
fi
if [ ! "`echo $merge_command`" ]; then
	merge_command="sdiff -s -o %merged %orig %new"
fi

#
# Find all "rpmnew" configuration files.
#
#cfg_files=`find $configdirs -iname '*.rpmnew'`

#
# Ask which one of the given two files to install
#
rm_extra_file() {
	old="$1"
	new="$2"
	
	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 ..."
		;;
	6)
		echo "*** bye ..."
		exit 1
		;;
	*)
		echo "!!! Please pick a valid choice next time !!!"
		menu1
		;;
	esac
}

#
# 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 "5) Skip (keep all files)"
echo "  OR"
echo "6) Quit etc-update"
echo
echo -n "Type (1, 2, 3, 4, 5 or 6): "									
}

#
# 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): "
}

#
# Install merged file
#
install_merged_file() {
	old="$1"
	merged="$1.merged"
	new="$2"

	menu2
	# read and echo 1 char from stdin
	read input
	echo
	case $input in
	1)
		echo "*** upgrading to $merged ..."
		chmod --reference="$old" "$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
}

#
# Show the difference between two files
#
show_diff() {
	echo
	if [ "`echo $pager`" ]; then
		(echo "*** showing difference between $1 and $2" && echo && \
		eval `echo $diff_command | sed \
		-e "s@%file1@\"$1\"@g" \
		-e "s@%file2@\"$2\"@g"` ) | $pager
	else
		echo "*** showing difference between $1 and $2" && echo
		eval `echo $diff_command | sed \
		-e "s@%file1@\"$1\"@g" \
		-e "s@%file2@\"$2\"@g"`
	fi
}

#
# Merge two files
#
merge_files() {
	old="$1"
	merged="$1.merged"
	new="$2"

	echo
	echo "*** merging $old with $new ..."

	if [ -e $merged ]; then
		echo
		echo "*** an (old?) merged file already exists. It will be removed ..."
		echo
		rm "$rm_opts" "$merged"
	fi

	# echo the help message for the merge command if it's defined
	if [ "`echo $merge_helpmessage`" ]; then
		echo
		echo "$merge_helpmessage"
	fi

	# execute the merge command
	echo
	eval `echo $merge_command |sed \
	-e "s@%merged@\"$merged\"@g" \
	-e "s@%orig@\"$old\"@g" \
	-e "s@%new@\"$new\"@g"`
}

#
# Run the script
#
find $configdirs -iname '*.rpmnew' > /var/run/etc-update.$$
trap "rm -f /var/run/etc-update.$$" KILL INT QUIT EXIT
while read -u 10 new_full_path; do
	file="${new_full_path##*/}"
	old_full_path="${new_full_path%/*}/${file%.rpmnew}"

	if cmp "${old_full_path}" "${new_full_path}"; then
		mv -f "${new_full_path}" "${old_full_path}"
	else
 		rm_extra_file "${old_full_path}" "${new_full_path}"
	fi
done 10< /var/run/etc-update.$$
rm -f /var/run/etc-update.$$
echo "*** script finished ..."
