#!/bin/sh # cfg_update.sh 0.0.1, (c) 2008 by Patrick Boylan # # You can modify, redistribute, and pretty much do whatever you want # with this script under the terms of the GNU General Public License, # Version 2, or the new one if you prefer. If you didn't get a copy of # the GNU GPL with this program, look in /usr/share/common-licenses/ on # Debian-based systems, or http://gnu.org/licenses/gpl-2.0.txt . If # the URL should fail for some reason, they have a search engine. # # This script was written to address a minor pet peeve of mine, with # relation to updating Lynx. The install-cfg.sh script [part of the # Lynx sources] will preserve your old settings by appending them to # the new lynx.cfg . This is good, but then they're out of context. # It could also cause problems for some, because the *last* occurrence # of a given setting will take precedence, so if you change something # in the body of the file, another, conflicting item appended to the # end may still override it. # # This script [tries to] interleave your old settings with the relevant # comments in the new file. `Orphaned' settings are still appended to # the end, if it can't find a proper context for them. # # However, this IS NOT a drop-in replacement for install-cfg.sh , which # accepts different command-line options, does some filtering for dead # URLs [mine doesn't], and plays nicely with the makefile [wrt the # HELPFILE setting in particular]. # CHANGES: # 0.0.1 * I think it works, but please let me know if it doesn't... # # Lynxcookie-sneaking(a)sneakEmail,com # [but fix the (a) and the comma first] nom="$(basename "$0")" tmp1="cfg_update_tmp" how2 () { echo " Usage: $nom /path/to/old/lynx.cfg ./new/lynx.cfg [output file | dir] Import settings from your old lynx.cfg file to a newer one. If no output path is specified, a file named \`lynx.cfg[.N]' will be created in the current directory. The script *will_not* overwrite any existing file unless explicitly told to do so. A number will be appended to the name if a file named \`lynx.cfg' already exists in the specified location [and if an output directory, not filename is specified]. Any pathname may be relative or absolute, but the output directory must already exist. You should use the unmodified lynx.cfg included in the Lynx sources as your \`/path/to/new/lynx.cfg', because the updated one will already have your old settings appended to it. That one can be used as your \`/path/to/old/lynx.cfg' though [because it already has your old settings appended to it]. " } if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ] then how2 exit 0 fi if [ -f "$1" ] then old_cfg="$1" else echo "\nError: \`$1' is not a file." how2 exit 1 fi if [ -f "$2" ] then new_cfg="$2" else echo "\nError: \`$2' is not a file." how2 exit 1 fi # Some half-hearted idiot-proofing. if [ "$3" = "" ] then out_cfg="`pwd`" else out_cfg="$3" fi if [ -e "$out_cfg" ] then if [ -f "$out_cfg" ] then echo "Replacing \`$out_cfg'..." elif [ -d "$out_cfg" ] then out_cfg="$out_cfg/lynx.cfg" tmp_cfg="$out_cfg" num=0 while [ -e "$tmp_cfg" ] do tmp_cfg="${out_cfg}.$num" num=$(($num + 1)) done out_cfg="$tmp_cfg" echo "Output to file \`$out_cfg'...'" else echo "\nError: \`$out_cfg' exists, and is not a regular file." how2 exit 1 fi elif [ ! -d "$(dirname "$out_cfg")" ] then echo "\nError: Parent directory must exist!" how2 exit 1 else echo "Output to file \`$out_cfg'...'" fi # Not so half-hearted I guess... old_cfg="$(cat "$old_cfg" | grep "^[ ]*[A-Za-z]")" # Extract active configuration lines from old lynx.cfg . if [ "$old_cfg" = "" ] then echo "\`$1' has no settings to save!" exit else echo "Loaded settings from \`$1'..." fi oldIFS="$IFS" IFS=' ' seek_cfg='' for line in $old_cfg do seek_cfg="$seek_cfg$(echo $line | cut --delimiter=':' --fields=1)\n" # Extract only the variable, not its definition. done seek_cfg="$(echo -n "$seek_cfg" | sort -u)" # Remove duplicates; they'll only slow you down later. grep_cfg="$(echo -n "$seek_cfg" | tr '\n' '|')" IFS="$oldIFS" tmp1="$(dirname "$out_cfg")/$tmp1" tmp2="$tmp1" num=0 while [ -e "$tmp2" ] do tmp2="${tmp1}.$num" num=$(($num + 1)) # Make sure the temp directory does not already # exist, because it will be deleted later. done tmp1="$tmp2" mkdir "$tmp1" echo "Using temp directory \`$tmp1'..." echo "Splitting \`$new_cfg' on blank lines..." csplit --quiet --digits=6 --prefix="$tmp1/cfg" "$new_cfg" '/^$/' '{*}' echo "Matching old settings to new sections...\n" for phile in $(find "$tmp1" -type f | sort -r) do this_cfg="$(cat "$phile")" if [ "$(echo "$this_cfg" | grep "^[# ]*[A-Za-z]" | \ egrep "$grep_cfg")" != "" ] then for sought in $seek_cfg do if [ "$(echo "$this_cfg" | grep "^[#]*$sought:")" != "" ] then echo "Found $sought" echo "$old_cfg" | grep "^[ ]*$sought" \ >> "$phile" # Drop this item from your search # lists, because it's been found. # old_cfg="$(echo -n "$old_cfg" | \ grep -v "^[ ]*$sought")" seek_cfg="$(echo -n "$seek_cfg" | \ grep -v "^$sought$")" grep_cfg="$(echo -n "$seek_cfg" | \ tr '|' '\n' | \ grep -v "^$sought$")" grep_cfg="$(echo -n "$seek_cfg" | tr '\n' '|')" fi done fi done cat $(find "$tmp1" -type f | sort) > "$out_cfg" echo "\nRemoving temp-files, \`$tmp1'." rm -rf "$tmp1" if [ "$(echo -n "$old_cfg")" != "" ] then { echo echo "## The following lines were saved from your previous configuration:" echo "#" echo "$old_cfg" } >> "$out_cfg" echo "\n...Had a few pieces left over (orphaned settings)..." echo "\n$old_cfg" echo "\nAppended to end of file." fi echo "Done." # Also dedicated to the memory of Cheetah the cat, but I don't want # to go all sentimental about it here. I think I'm getting better at # writing these things without her.