--- etc-update 2005-01-05 21:54:38.000000000 +0800 +++ etc-update 2005-01-05 22:23:16.712354120 +0800 @@ -26,9 +26,33 @@ sed -ne "s/^ *$item *= *\([\"']\{0,1\}\)\(.*\)\1/\2/p" |sed -e '$p;d' } -function scan() { +function option() { + echo -ne "${OPTION}${*}${NORMAL}" +} + +function cfgfile() { + echo -ne "${CFGFILE}${*}${NORMAL}" +} + +function updatefile() { + echo -ne "${UPDATEFILE}${*}${NORMAL}" +} + +function menu_title() { + echo + echo -e "${MENU}>> ${*}:${NORMAL}" +} + +function beautify_diff() { + awk 'BEGIN { u_fmt = 1 } + /^[<>]/ { u_fmt = 0 } + /^[^-+<>]/ || ( !u_fmt && /^-/ ) { print $0 } + /^]/ { print "'${UPDATEFILE}'" $0 "'${NORMAL}'" }' +} - echo "Scanning Configuration files..." +function scan() { + einfo "Scanning configuration files..." rm -rf ${TMP}/files > /dev/null 2>&1 mkdir ${TMP}/files || die "Failed mkdir command!" 1 count=0 @@ -65,7 +89,7 @@ MATCHES=1 fi if [[ "${MATCHES}" == "1" ]]; then - echo "Automerging trivial changes in: ${rfile:10}" + einfo "Auto-applying trivial update to $(cfgfile ${rpath}/${rfile:10})" mv ${rpath}/${rfile} ${rpath}/${rfile:10} continue else @@ -90,6 +114,7 @@ done fi; done + echo } function sel_file() { @@ -99,12 +124,16 @@ 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 + echo "$(option ${file})${PAR} $(cfgfile $(head -n1 "${TMP}/files/${file}"))" + if [ "${show_update_files}" == "yes" ]; then + for update in $(tail -n+2 ${TMP}/files/${file}); do + echo ${update} + done + echo + fi else + echo -n "${file}${PAR} " head -n1 ${TMP}/files/${file} fi done > ${TMP}/menuitems @@ -113,18 +142,19 @@ input=0 else 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." + menu_title "Select a configuration file to update" + echo else local my_title="Please select a file to update" fi if (( ${mode} == 0 )); then cat ${TMP}/menuitems - echo "Please select a file to edit by entering the corresponding number." - echo " (don't use -3 or -5 if you're unsure what to do)" - echo " (-1 to exit) (-3 to auto merge all remaining files)" - echo -n " (-5 to auto-merge AND not use 'mv -i'): " + echo + echo "($(option -1) to exit)" + echo "($(option -3) to replace all remaining files with their updates)" + echo "($(option -5) to replace all without further confirmation)" + echo -n "Enter the number for a given file/action (use -3 and -5 with CAUTION): " read input else dialog --title "${title}" --menu "${my_title}" \ @@ -137,6 +167,7 @@ export mv_opts="" fi if (( ${input} == -3 )); then + echo input=0 export OVERWRITE_ALL="yes" fi @@ -148,7 +179,10 @@ } function do_file() { - echo + if [ "${OVERWRITE_ALL}" != "yes" ]; then + echo + menu_title "Updating $(head -n1 ${TMP}/files/${input})" + fi local -i my_input local -i fcount=0 until (( `cat ${TMP}/files/${input}|wc -l` < 2 )); do @@ -164,13 +198,13 @@ else for line in `cat ${TMP}/files/${input}`; do if (( ${fcount} > 0 )); then - echo -n "${fcount}${PAR} " - echo "${line}" + echo "$(option ${fcount})${PAR} $(updatefile "${line}") ($(find "${line}" -printf "%t"))" else if (( ${mode} == 0 )); then - echo "Below are the new config files for ${line}:" + echo + echo "Multiple updates exist for ${line}:" else - local my_title="Please select a file to process for ${line}" + local my_title="Please select an update to process for ${line}" fi fi fcount=${fcount}+1 @@ -178,7 +212,7 @@ if (( ${mode} == 0 )); then cat ${TMP}/menuitems - echo -n "Please select a file to process (-1 to exit this file): " + echo -n "Please select an update to process ($(option -1) to return to the previous menu): " read my_input else dialog --title "${title}" --menu "${my_title}" \ @@ -215,7 +249,7 @@ break fi done - echo + [ "${OVERWRITE_ALL}" != "yes" ] && echo rm ${TMP}/files/${input} count=${count}-1 } @@ -230,45 +264,49 @@ if [ "${OVERWRITE_ALL}" == "yes" ]; then my_input=1 else + echo showdiffcmd=$(echo "${diff_command}" | sed -e "s:%file1:${ofile}:" -e "s:%file2:${file}:") if [ "${using_editor}" == 0 ]; then ( - echo "Showing differences between ${ofile} and ${file}" - ${showdiffcmd} + echo "Differences between original and update:" + ${showdiffcmd} | beautify_diff ) | ${pager} else - echo "Beginning of differences between ${ofile} and ${file}" + echo "Beginning of differences between $(cfgfile ${ofile}) and $(updatefile ${file})" ${showdiffcmd} - echo "End of differences between ${ofile} and ${file}" + echo "End of differences between $(cfgfile ${ofile}) and $(updatefile ${file})" fi if [ -L "${file}" ]; then echo - echo "-------------------------------------------------------------" - echo "NOTE: File is a symlink to another file. REPLACE recommended." - echo " The original file may simply have moved. Please review." - echo "-------------------------------------------------------------" + ewarn "-------------------------------------------------------------" + ewarn "NOTE: File is a symlink to another file. REPLACE recommended." + ewarn " The original file may simply have moved. Please review." + ewarn "-------------------------------------------------------------" echo fi - 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): " + echo -ne "$(option 1)${PAR} Replace $(cfgfile ${ofile}) with update +$(option 2)${PAR} Delete update, keeping original as is +$(option 3)${PAR} Interactively merge original with update +$(option 4)${PAR} Show differences again +Please select from the menu above ($(option -1) to ignore this update): " read my_input fi - + case ${my_input} in - 1) echo "Replacing ${ofile} with ${file}" + 1) [ "${OVERWRITE_ALL}" != "yes" ] && echo + einfo "Replacing $(cfgfile ${ofile}) with $(updatefile ${file})" mv ${mv_opts} ${file} ${ofile} continue ;; - 2) echo "Deleting ${file}" + 2) [ "${OVERWRITE_ALL}" != "yes" ] && echo + einfo "Deleting $(updatefile ${file})" rm ${rm_opts} ${file} continue ;; - 3) do_merge "${file}" "${ofile}" + 3) echo + do_merge "${file}" "${ofile}" my_input=${?} # [ ${my_input} == 255 ] && my_input=-1 continue @@ -287,38 +325,40 @@ 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} + einfo "A previous version of the merged file exists, cleaning..." + rm ${rm_opts} "${mfile}" fi until (( ${my_input} == -1 )); do - echo "Merging ${file} and ${ofile}" + menu_title "Merging ${ofile} with update" + echo `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) Edit merged file -5) Return to the previous menu -Please select from the menu above (-1 to exit, losing this merge): " + echo -n "$(option 1)${PAR} Replace $(cfgfile ${ofile}) with merged file +$(option 2)${PAR} Show differences between merged file and original +$(option 3)${PAR} Remerge original with update +$(option 4)${PAR} Edit merged file +$(option 5)${PAR} Return to the previous menu +Please select from the menu above ($(option -1) to exit, losing this merge): " read my_input case ${my_input} in - 1) echo "Replacing ${ofile} with ${mfile}" + 1) echo + einfo "Replacing $(cfgfile ${ofile}) with $(updatefile ${mfile})" chmod --reference=${ofile} ${mfile} mv ${mv_opts} ${mfile} ${ofile} rm ${rm_opts} ${file} return 255 ;; - 2) ( echo "Showing differences between ${ofile} and ${mfile}" + 2) echo + ( echo "Differences between original and merged file:" `echo "${diff_command}" | \ sed -e "s:%file1:${ofile}:" \ - -e "s:%file2:${mfile}:"` ) | ${pager} + -e "s:%file2:${mfile}:"` ) | beautify_diff | ${pager} continue ;; 3) break @@ -341,6 +381,7 @@ function die() { trap "" term trap "" kill + echo echo "Exiting: ${1}" rm -rf ${TMP} exit ${2} @@ -358,8 +399,14 @@ mkdir ${TMP} || die "failed mkdir command!" 1 # I need the CONFIG_PROTECT value -CONFIG_PROTECT=$(/usr/lib/portage/bin/portageq config_protect) -CONFIG_PROTECT_MASK=$(/usr/lib/portage/bin/portageq config_protect_mask) +if [[ $# == 0 ]]; then + CONFIG_PROTECT=$(/usr/lib/portage/bin/portageq config_protect) + CONFIG_PROTECT_MASK=$(/usr/lib/portage/bin/portageq config_protect_mask) +else + # For testing purposes + CONFIG_PROTECT="$*" + CONFIG_PROTECT_MASK= +fi # load etc-config's configuration EU_AUTOMERGE=`get_config eu_automerge` @@ -370,6 +417,7 @@ diff_command=`get_config diff_command` using_editor=`get_config using_editor` merge_command=`get_config merge_command` +show_update_files=`get_config show_update_files` declare -i mode=`get_config mode` [ -z ${mode} ] && mode=0 [ -z "${pager}" ] && pager="cat" @@ -377,6 +425,20 @@ #echo "rm_opts: $rm_opts, mv_opts: $mv_opts, cp_opts: $cp_opts" #echo "pager: $pager, diff_command: $diff_command, merge_command: $merge_command" +# set up colours (and ensure that the function ewarn() and einfo() can be used) +source /etc/init.d/functions.sh +if [ "${RC_NOCOLOR}" = "yes" ] +then + OPTION= + CFGFILE= + MENU= +else + OPTION=$'\e[37;01m' + CFGFILE=$'\e[32m' + UPDATEFILE=$'\e[33m' + MENU=$'\e[37;01m' +fi + if (( ${mode} == 0 )); then PAR=")" else @@ -395,7 +457,7 @@ until (( ${input} == -1 )); do if (( ${count} == 0 )); then - die "Nothing left to do; exiting. :)" 0 + die "Nothing left to do. :)" 0 fi sel_file if (( ${input} != -1 )); then