#!/bin/bash # Copyright 2002 Gentoo Technologies, Inc. # Distributed under the terms of the GNU General Public License, v2 or later # Author: Jochem Kossen , (c) 2002 # Author: Leo Lipelis , (c) 2002 # Author: Karl Trygve Kalleberg , (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 } # # 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 ..." exit ;; *) 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 " OR" echo "5) Skip (keep all files)" echo echo -n "Type (1, 2, 3, 4 or 5): " } # # 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 ..." 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 && \ `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 } # # 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 # execute the merge command `echo $merge_command |sed \ -e s@%merged@$merged@g \ -e s@%orig@$old@g \ -e s@%new@$new@g` } # # Run the script # scriptname=`basename $0` # 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` #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 # #set -o xtrace for cfg_dir in $CONFIG_PROTECT; do if [ -d "$cfg_dir" ] ; then cfg_files=`find $cfg_dir -iname '._cfg????_*' | sort` 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} rm_extra_file ${old_full_path} ${new_full_path} done else echo "!!! Skipping non-existant directory $cfg_dir" fi done echo "*** script finished ..."