home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / bin / ucf < prev    next >
Encoding:
Text File  |  2006-06-16  |  32.0 KB  |  971 lines

  1. #!/bin/bash
  2. #                               -*- Mode: Sh -*-
  3. # updateConfFile.sh ---
  4. # Author           : Manoj Srivastava ( srivasta@glaurung.green-gryphon.com )
  5. # Created On       : Fri Feb  1 03:41:47 2002
  6. # Created On Node  : glaurung.green-gryphon.com
  7. # Last Modified By : Manoj Srivastava
  8. # Last Modified On : Tue Jun  6 09:48:22 2006
  9. # Last Machine Used: glaurung.internal.golden-gryphon.com
  10. # Update Count     : 186
  11. # Status           : Unknown, Use with caution!
  12. # HISTORY          :
  13. # Description      :
  14. #
  15. # This script attempts to provide conffile like handling for files not
  16. # shipped in a Debian package, but handled by the postinst. Using this
  17. # script, one may ship a bunch of default cofiguration files somewhere
  18. # in /usr (/usr/share/<pkg> is a good location), and maintain files in
  19. # /etc.
  20. #
  21. # The motivation for this script was to provide conffile like handling
  22. # for start files for emacs lisp packages (for example,
  23. # /etc/emacs21/site-stard.d/50psgml-init.el) These start files are not
  24. # shipped with the package, instead, they are installed during the
  25. # post installation configuration phase by the script
  26. # /usr/lib/emacsen-common/emacs-package-install $package_name.
  27. #
  28. # This script is meant to be invoked by the packages install script at
  29. # /usr/lib/emacsen-common/packages/install/$package_name for each
  30. # flavour of installed emacsen by calling it with the proper values of
  31. # new file (/usr/share/emacs/site-lisp/<pkg>/<pkg>-init.el), and dest file
  32. # (/etc/emacs21/site-stard.d/50<pkg>-init.el)), and it should do the rest.
  33. #
  34.  
  35. # make sure we exit on error
  36. set -e
  37.  
  38. # set the version and revision
  39. progname="`basename \"$0\"`"
  40. pversion='$Revision: 1.26 $'
  41.  
  42. ######################################################################
  43. ########                                                     #########
  44. ########              Utility functions                      #########
  45. ########                                                     #########
  46. ######################################################################
  47. setq() {
  48.     # Variable Value Doc_string
  49.     if [ "x$2" = "x" ]; then
  50.     echo >&2 "$progname: Unable to determine $3"
  51.     exit 1;
  52.     else
  53.     if [ "x$VERBOSE" != "x" ]; then
  54.         echo >&2 "$progname: $3 is $2";
  55.     fi
  56.     eval "$1=\"\$2\"";
  57.     fi
  58. }
  59.  
  60. withecho () {
  61.         echo " $@" >&2
  62.         "$@"
  63. }
  64.  
  65. usageversion () {
  66.         cat >&2 <<END
  67. Debian GNU/Linux $progname $pversion.
  68.            Copyright (C) 2002-2005 Manoj Srivastava.
  69. This is free software; see the GNU General Public Licence for copying
  70. conditions.  There is NO warranty.
  71.  
  72. Usage: $progname  [options] new_file  destination
  73. Options:
  74.      -h,     --help          print this message
  75.      -s foo, --src-dir  foo  Set the src dir (historical md5sums live here)
  76.              --sum-file bar  Force the historical md5sums to be read from
  77.                              this file.  Overrides any setting of --src-dir.
  78.      -d [n], --debug    [n]  Set the Debug level to N
  79.      -n,     --no-action     Dry run. No action is actually taken.
  80.      -v,     --verbose       Make the script verbose
  81.              --three-way     Register this file in the cache, and turn on the
  82.                              diff3 option allowing the merging of maintainer
  83.                              changes into a (potentially modified) local 
  84.                              configuration file. )
  85.              --state-dir bar Set the state directory to bar instead of the
  86.                              default '/var/lib/ucf'. Used mostly for testing.
  87.              --debconf-ok    Indicate that it is ok for uct to use an already
  88.                              running debconf instance for prompting.
  89. Usage: $progname  -p  destination
  90.      -p,     --purge         Remove any reference to destination from records
  91.  
  92. By default, the directory the new_file lives in is assumed to be the src-dir,
  93. which is where we look for any historical md5sums.
  94.  
  95. END
  96.     
  97. }
  98.  
  99. ######################################################################
  100. ########                                                     #########
  101. ########                  DebConf stuff                      #########
  102. ########                                                     #########
  103. ######################################################################
  104.  
  105. # Is debconf already running? Kinda tricky, because it will be after the
  106. # confmodule is sourced, so only test before that.
  107. if [ -z "$DEBCONF_ALREADY_RUNNING" ]; then
  108.     if [ "$DEBIAN_HAS_FRONTEND" ]; then
  109.     DEBCONF_ALREADY_RUNNING='YES'
  110.     else
  111.     DEBCONF_ALREADY_RUNNING='NO'
  112.         # Load out templates, just in case our template has
  113.         # not been loaded or the Debconf DB lost or corrupted
  114.         # since then.
  115.         if test $(id -u) = 0; then
  116.             if which debconf-loadtemplate >/dev/null 2>&1; then
  117.                 debconf-loadtemplate ucf /var/lib/dpkg/info/ucf.templates
  118.             fi
  119.         else
  120.             echo >&2 "$progname: Not loading ucf templates, since we are not running as root."
  121.     fi
  122.     fi
  123. fi
  124.  
  125. export DEBCONF_ALREADY_RUNNING
  126.  
  127. # Start up debconf or at least get the db_* commands available.
  128. if [ -e /usr/share/debconf/confmodule ]; then
  129.     if test $(id -u) = 0; then
  130.     . /usr/share/debconf/confmodule
  131.     else
  132.         echo >&2 "$progname: Not loading confmodule, since we are not running as root."
  133.     fi
  134.     # Only set the title if debconf was not already running.
  135.     # If it was running, then we do not want to clobber the
  136.     # title used for configuring the whole package with debconf.
  137.     if [ "$DEBCONF_ALREADY_RUNNING" = 'NO' ]; then
  138.     if ! db_settitle ucf/title 2>/dev/null; then
  139.               # Older debconf that does not support that command.
  140.             if test $(id -u) = 0; then
  141.         db_title "Modified configuration file"
  142.             else
  143.                 echo >&2 "$progname: Not changing title, since we are not running as root."
  144.             fi
  145.     fi
  146.     fi
  147. fi
  148.     
  149.  
  150. if [ -z "$DEBCONF_OK" ]; then
  151.     if [ "$DEBCONF_ALREADY_RUNNING" = 'YES' ]; then
  152.     DEBCONF_OK='NO'
  153.     else
  154.     DEBCONF_OK='YES'
  155.     fi
  156. fi
  157.  
  158. if [ "$DEBCONF_ALREADY_RUNNING" ] && [ "$DEBCONF_OK" = NO ]; then
  159.     # Commented out for now, uncomment after a while to begin nagging
  160.     # maintainers to fix their scripts.
  161.     #cat \
  162. <<END
  163. *** WARNING: ucf was run from a maintainer script that uses debconf, but
  164.              the script did not pass --debconf-ok to ucf. The maintainer
  165.              script should be fixed to not stop debconf before calling ucf,
  166.              and pass it this parameter. For now, ucf will revert to using
  167.              old-style, non-debconf prompting. Ugh!
  168.  
  169.              Please inform the package maintainer about this problem.
  170. END
  171. fi
  172.  
  173.  
  174. # These are strings that must match the untranslated choices
  175. # of the debconf question.
  176. choice_install_new="install the package maintainer's version"
  177. choice_keep_current="keep your currently-installed version"
  178. choice_diff="show the differences between the versions"
  179. choice_sdiff="show a side-by-side difference between the versions"
  180. choice_diff_threeway="show a 3 way difference between available versions of the file"
  181. choice_merge_threeway="do a 3 way merge between available versions of the file [Very Experimental]"
  182. choice_shell="start a new shell to examine the situation"
  183.  
  184.  
  185. purge_md5sum () {
  186.     for i in $(/usr/bin/seq 6 -1 0); do
  187.     if [ -e "${statedir}/hashfile.${i}" ]; then
  188.         if [ "X$docmd" = "XYES" ]; then
  189.         cp -f "${statedir}/hashfile.${i}" \
  190.             "${statedir}/hashfile.$(($i+1))"
  191.         else 
  192.         echo cp -f "${statedir}/hashfile.${i}" \
  193.                           "${statedir}/hashfile.$(($i+1))"
  194.         fi 
  195.     fi 
  196.     done
  197.     if [ -e "$statedir/hashfile" ]; then
  198.     if [ "X$docmd" = "XYES" ]; then
  199.         cp -f "$statedir/hashfile"  "$statedir/hashfile.0" 
  200.     else
  201.         echo cp -f "$statedir/hashfile"  "$statedir/hashfile.0" 
  202.     fi
  203.     if [ "X$docmd" = "XYES" ]; then
  204.         set +e
  205.         if [ "X$VERBOSE" != "X" ]; then
  206.         echo "egrep -v [[:space:]]${safe_dest_file}$ $statedir/hashfile"
  207.         egrep -v "[[:space:]]${safe_dest_file}$"  "$statedir/hashfile" \
  208.             || true;
  209.         fi
  210.         #echo "egrep -v [[:space:]]${safe_dest_file}$ $statedir/hashfile"
  211.         egrep -v "[[:space:]]${safe_dest_file}$" "$statedir/hashfile" > \
  212.         "$statedir/hashfile.tmp" || true; 
  213.         if [ "X$docmd" = "XYES" ]; then
  214.         mv -f "$statedir/hashfile.tmp"  "$statedir/hashfile" 
  215.         else
  216.         echo mv -f "$statedir/hashfile.tmp"  "$statedir/hashfile"
  217.         fi
  218.         set -e
  219.     fi
  220.     fi
  221.     test -n "$VERBOSE" && echo "The cache file is $cached_file"
  222.     if [ ! -z "$cached_file" -a -f "$statedir/cache/$cached_file" ]; then
  223.     $action rm -f "$statedir/cache/$cached_file"
  224.     fi
  225. }
  226.  
  227. replace_md5sum () {
  228.     for i in $(/usr/bin/seq 6 -1 0); do
  229.     if [ -e "${statedir}/hashfile.${i}" ]; then
  230.         if [ "X$docmd" = "XYES" ]; then
  231.         cp -f "${statedir}/hashfile.${i}" \
  232.             "${statedir}/hashfile.$(($i+1))"
  233.         else
  234.         echo cp -f "${statedir}/hashfile.${i}" \
  235.             "${statedir}/hashfile.$(($i+1))"
  236.         fi
  237.     fi
  238.     done
  239.     if [ -e "$statedir/hashfile" ]; then
  240.     if [ "X$docmd" = "XYES" ]; then
  241.         cp -f "$statedir/hashfile"  "$statedir/hashfile.0" 
  242.     else
  243.         echo cp -f "$statedir/hashfile"  "$statedir/hashfile.0" 
  244.     fi
  245.     if [ "X$docmd" = "XYES" ]; then
  246.         set +e
  247.         if [ "X$VERBOSE" != "X" ]; then
  248.         echo "(egrep -v \"[[:space:]]${safe_dest_file}$\" \"$statedir/hashfile\";"
  249.         egrep -v "[[:space:]]${safe_dest_file}$"  "$statedir/hashfile" || true;
  250.          md5sum "$orig_new_file" | sed "s|$orig_new_file|$dest_file|" ;
  251.         fi
  252.         egrep -v "[[:space:]]${safe_dest_file}$" "$statedir/hashfile" > \
  253.         "$statedir/hashfile.tmp" || true; 
  254.         md5sum "$orig_new_file" | sed "s|$orig_new_file|$dest_file|" >> \
  255.         "$statedir/hashfile.tmp"; 
  256.         mv -f "$statedir/hashfile.tmp"  "$statedir/hashfile" 
  257.         set -e
  258.     else
  259.         echo "(egrep -v \"[[:space:]]${safe_dest_file}$\" \"$statedir/hashfile\""
  260.         echo " md5sum \"$orig_new_file\" | sed \"s|$orig_new_file|$dest_file|\"; " 
  261.         echo ") | sort > \"$statedir/hashfile\""
  262.     fi
  263.     else
  264.     if [ "X$docmd" = "XYES" ]; then
  265.         md5sum "$orig_new_file" | sed "s|$orig_new_file|$dest_file|"  > \
  266.         "$statedir/hashfile" 
  267.     else
  268.         echo " md5sum \"$orig_new_file\" | sed \"s|$orig_new_file|$dest_file|\" >" \
  269.         "\"$statedir/hashfile\""
  270.     fi
  271.     fi
  272.     if [ "X$THREEWAY" != "X" ]; then
  273.     $action cp -f "$orig_new_file" "$statedir/cache/$cached_file"
  274.     fi
  275.     # cp -f "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  276. }
  277.  
  278. replace_conf_file () {
  279.     # do not mangle $dest_file since it's the one registered in the hashfile
  280.     # or we have been ask to register
  281.     real_file="$dest_file"
  282.     if [ -L "$dest_file" ]; then
  283.     real_file="$(readlink -nf $dest_file || :)"
  284.     if [ "x$real_file" = "x" ]; then
  285.         echo >&2 "$dest_file is a broken symlink!"
  286.         $action rm -f "$dest_file";
  287.         real_file="$dest_file"
  288.     fi
  289.     fi
  290.     if [ -e "$real_file" ]; then
  291.     if [ -z "$RETAIN_OLD" ]; then
  292.         #echo "Saving  ${real_file}.${OLD_SUFFIX},  in case."
  293.         if [ "x$VERBOSE" != "x" ]; then
  294.         echo >&2 "Not saving ${real_file}, since it was unmodified"
  295.         fi
  296.     else
  297.         $action cp -f "${real_file}" "${real_file}.${OLD_SUFFIX}"
  298.     fi
  299.     fi
  300.     $action cp -f "$new_file" "${real_file}"
  301.     replace_md5sum;
  302. }
  303.  
  304.  
  305. ######################################################################
  306. ########                                                     #########
  307. ########              Command line args                      #########
  308. ########                                                     #########
  309. ######################################################################
  310. #
  311. # Long term variables#
  312. #
  313. docmd='YES'
  314. action='withecho'
  315. action=
  316. DEBUG=0
  317. VERBOSE=''
  318. statedir='/var/lib/ucf';
  319. THREEWAY=
  320.  
  321. DIST_SUFFIX="ucf-dist"
  322. NEW_SUFFIX="ucf-new"
  323. OLD_SUFFIX="ucf-old"
  324.  
  325. # Note that we use `"$@"' to let each command-line parameter expand to a
  326. # separate word. The quotes around `$@' are essential!
  327. # We need TEMP as the `eval set --' would nuke the return value of getopt.
  328. TEMP=`getopt -a -o hs:d::D::nv -n "$progname" \
  329.       --long help,src-dir:,sum-file:,dest-dir:,debug::,DEBUG::,no-action,purge,verbose,three-way,debconf-ok,state-dir: \
  330.              -- "$@"`
  331.  
  332. if [ $? != 0 ] ; then
  333.     echo "Error handling options.Terminating..." >&2 ;
  334.     exit 1 ;
  335. fi
  336.  
  337. # Note the quotes around `$TEMP': they are essential!
  338. eval set -- "$TEMP"
  339.  
  340. while true ; do
  341.     case "$1" in
  342.     -h|--help) usageversion;                        exit 0 ;;
  343.     -n|--no-action) action='echo'; docmd='NO';      shift  ;;
  344.     -v|--verbose) VERBOSE=1;                        shift  ;;
  345.     -s|--src-dir)
  346.         opt_source_dir="$2";                       shift 2 ;;
  347.     --sum-file)
  348.         opt_old_mdsum_file="$2";          shift 2 ;;
  349.     --state-dir)
  350.         opt_state_dir="$2";                        shift 2 ;;
  351.     -D|-d|--debug|--DEBUG)
  352.             # d has an optional argument. As we are in quoted mode,
  353.             # an empty parameter will be generated if its optional
  354.             # argument is not found.
  355.         case "$2" in
  356.         "") setq DEBUG 1    "The Debug value"; shift 2 ;;
  357.         *)  setq DEBUG "$2" "The Debug value"; shift 2 ;;
  358.         esac ;;
  359.         -p|--purge) PURGE=YES;                         shift   ;;
  360.        --three-way) THREEWAY=YES;                       shift   ;;
  361.        --debconf-ok) DEBCONF_OK=YES;                    shift   ;;
  362.     --)  shift ;                                   break   ;;
  363.     *) echo >&2 "Internal error!" ; exit 1 ;;
  364.     esac
  365. done
  366. # Need to run as root, or else the 
  367. if test $(id -u) != 0; then
  368.     if [ "$docmd" = "YES" ]; then
  369.         echo "$progname: Need to be run as root." >&2
  370.         echo "$progname: Setting up no action mode." >&2
  371.         action='echo'; docmd='NO'; 
  372.     fi
  373. fi
  374.  
  375. if [ "X$PURGE" = "XYES" ]; then
  376.     if [ $# != 1 ]; then
  377.     echo >&2 "*** ERROR: Need exactly one argument when purging, got $#";
  378.     echo >&2 ""
  379.     usageversion;
  380.     exit 0 ;        
  381.     fi
  382.     setq dest_file "$1" "The Destination file";
  383. else
  384.     if [ $# != 2 ]; then
  385.     echo >&2 "*** ERROR: Need exactly two arguments, got $#";
  386.     echo >&2 ""
  387.     usageversion;
  388.     exit 0 ;
  389.     fi
  390.     setq new_file  "$1" "The new file";
  391.     setq dest_file "$2" "The Destination file";
  392.     if [ ! -e "$new_file" ]; then
  393.     echo >&2 "Error: The new file ${new_file} does not exist!";
  394.     exit 1;
  395.     fi
  396. fi
  397. safe_dest_file=$(echo $dest_file | perl -nle 'print "\Q$_\E\n"')
  398.  
  399.  
  400.  
  401. # Load site defaults and over rides.
  402. if [ -f /etc/ucf.conf ]; then
  403.     . /etc/ucf.conf
  404. fi
  405.  
  406. # Command line, env variable, config file, or default
  407. if [ ! "x$opt_source_dir" = "x" ]; then
  408.     setq source_dir "$opt_source_dir" "The Source directory"
  409. elif [ ! "x$UCF_SOURCE_DIR" = "x" ]; then
  410.     setq source_dir "$UCF_SOURCE_DIR" "The Source directory"
  411. elif [ ! "x$conf_source_dir" = "x" ]; then
  412.     setq source_dir "$conf_source_dir" "The Source directory"
  413. else
  414.     if [ "X$new_file" != "X" ]; then
  415.     setq source_dir $(dirname "$new_file") "The Source directory"
  416.     else
  417.     setq source_dir /tmp "The Source directory"
  418.     fi
  419.     
  420. fi
  421.  
  422. if [ "X$PAGER" != "X" ] && type -a -p $PAGER >& /dev/null ; then
  423.     my_pager=$(type -a -p $PAGER);
  424. elif [ -s /usr/bin/pager ] && 
  425.      [ "X$(readlink -e /usr/bin/pager || :)" != "X" ]; then
  426.     my_pager=/usr/bin/pager
  427. elif [ -x /usr/bin/sensible-pager ]; then
  428.     my_pager=/usr/bin/sensible-pager
  429. elif [ -x /bin/more ]; then
  430.     my_pager=/bin/more
  431. else
  432.     my_pager=
  433. fi
  434.  
  435.  
  436.  
  437. if [ "X$my_pager" = "X" ]; then
  438.     STOP=YES
  439. elif [ "X$my_pager" = "X/bin/more" ]; then
  440.     STOP=YES
  441. fi
  442.  
  443. # Command line, env variable, config file, or default
  444. if [ ! "x$opt_state_dir" = "x" ]; then
  445.     setq statedir "$opt_state_dir" "The State directory"
  446. elif [ ! "x$UCF_STATE_DIR" = "x" ]; then
  447.     setq statedir "$UCF_STATE_DIR" "The State directory"
  448. elif [ ! "x$conf_state_dir" = "x" ]; then
  449.     setq statedir "$conf_state_dir" "The State directory"
  450. else
  451.     setq statedir '/var/lib/ucf'  "The State directory"    
  452. fi
  453.  
  454. # Command line, env variable, config file, or default
  455. if [ ! "x$opt_force_conffold" = "x" ]; then
  456.     setq force_conffold "$opt_force_conffold" "Keep the old file"
  457. elif [ ! "x$UCF_FORCE_CONFFOLD" = "x" ]; then
  458.     setq force_conffold "$UCF_FORCE_CONFFOLD" "Keep the old file"
  459. elif [ ! "x$conf_force_conffold" = "x" ]; then
  460.     setq force_conffold "$conf_force_conffold" "Keep the old file"
  461. else
  462.     force_conffold=''
  463. fi
  464.  
  465. # Command line, env variable, config file, or default
  466. if [ ! "x$opt_force_conffnew" = "x" ]; then
  467.     setq force_conffnew "$opt_force_conffnew" "Replace the old file"
  468. elif [ ! "x$UCF_FORCE_CONFFNEW" = "x" ]; then
  469.     setq force_conffnew "$UCF_FORCE_CONFFNEW" "Replace the old file"
  470. elif [ ! "x$conf_force_conffnew" = "x" ]; then
  471.     setq force_conffnew "$conf_force_conffnew" "Replace the old file"
  472. else
  473.     force_conffnew=''
  474. fi
  475.  
  476. # Command line, env variable, config file, or default
  477. if [ ! "x$opt_force_conffmiss" = "x" ]; then
  478.     setq force_conffmiss "$opt_force_conffmiss" "Replace any missing files"
  479. elif [ ! "x$UCF_FORCE_CONFFMISS" = "x" ]; then
  480.     setq force_conffmiss "$UCF_FORCE_CONFFMISS" "Replace any missing files"
  481. elif [ ! "x$conf_force_conffmiss" = "x" ]; then
  482.     setq force_conffmiss "$conf_force_conffmiss" "Replace any missing files"
  483. else
  484.     force_conffmiss=''
  485. fi
  486.  
  487. if [ -n "$opt_old_mdsum_file" ]; then
  488.     setq old_mdsum_file "$opt_old_mdsum_file" "The md5sum is found here"
  489. elif [ ! "x$UCF_OLD_MDSUM_FILE" = "x" ]; then
  490.     setq old_mdsum_file "$UCF_OLD_MDSUM_FILE" "The md5sum is found here"
  491. elif [ ! "x$conf_old_mdsum_file" = "x" ]; then
  492.     setq old_mdsum_file "$conf_old_mdsum_file" "Replace the old file"
  493. else
  494.     old_mdsum_file="$source_dir/"$(basename "${new_file}")".md5sum";
  495. fi
  496.  
  497.  
  498. if [ "X$force_conffold" != "X" -a "X$force_conffnew" != "X" ]; then
  499.     echo >&2 "Error: Only one of force_conffold and force_conffnew should";
  500.     echo >&2 "       be set";
  501.     exit 1;
  502. fi
  503.  
  504. # VERBOSE of 0 is supposed to be the same as not setting VERBOSE
  505. if [ "X$VERBOSE" = "X0" ]; then
  506.     VERBOSE=''
  507. fi
  508.  
  509.  
  510. #
  511. if [ -e "$statedir/hashfile" -a ! -w "$statedir/hashfile" ]; then
  512.     echo >&2 "ucf: do not have write privilege to the state data"
  513.     if [ "X$docmd" = "XYES" ]; then
  514.     exit 1;
  515.     fi
  516. fi
  517.  
  518. if [ ! -d $statedir/cache ]; then
  519.     $action mkdir -p $statedir/cache ;
  520. fi
  521.  
  522. # test and see if this file exists in the database
  523. if [ -e "$statedir/hashfile" ]; then
  524.     if [ "X$VERBOSE" != "X" ]; then
  525.     echo >&2 "The hash file exists"
  526.     echo egrep "[[:space:]]${safe_dest_file}$" "$statedir/hashfile"
  527.     egrep "[[:space:]]${safe_dest_file}$" "$statedir/hashfile" || true
  528.     fi
  529.     lastsum=$(egrep "[[:space:]]${safe_dest_file}$" "$statedir/hashfile" | \
  530.                    awk '{print $1;}' )
  531. fi
  532.  
  533. old_mdsum_dir="$source_dir/"$(basename "${new_file}")".md5sum.d";
  534.  
  535.  
  536. cached_file=$(echo $dest_file | tr / :)
  537. orig_new_file="$new_file"    # Since sometimes we replace the newfile below
  538.  
  539. # Determine the action for the current file. The default is to ask,
  540. # with non-replacement being the norm.
  541. # If the config dir exists
  542. #   if file in always overwrite, state +=1;
  543. #   fi
  544. #   if file in never overwrite, state +=2;
  545. #   fi
  546. #   if file in ask; state +=4
  547. #   fi
  548. #   if state == 0; then state = default
  549. #   if state >= 4; ask
  550. #   if state == 3;  ask
  551. #   if state == 2; exit
  552. #   if state == 1; then replace_conffile; exit
  553.  
  554. if [ $DEBUG -gt 0 ]; then
  555.     cat <<EOF
  556. The new start file is      \`$new_file\'
  557. The destination is         \`$dest_file\' (\`$safe_dest_file\')
  558. The history is kept under  \'$source_dir\'
  559. The file may be cached at \'$statedir/cache/$cached_file\'
  560. EOF
  561.     if [ -s "$dest_file" ]; then
  562.     echo "The destination file exists, and has md5sum:"
  563.     md5sum "$dest_file"
  564.     else
  565.     echo "The destination file does not exist."
  566.     fi
  567.     if [ "X$lastsum" != "X" ]; then
  568.     echo "The old md5sum exists, and is:"
  569.     echo $lastsum
  570.     else 
  571.     echo "The old md5sum does not exist."
  572.         if [ -d "$old_mdsum_dir" -o -f "$old_mdsum_file" ]; then
  573.             echo "However, there are historical md5sums around."
  574.         fi
  575.     fi
  576.     if [ -e "$new_file" ]; then
  577.     echo "The new file exists, and has md5sum:"
  578.     md5sum "$new_file"
  579.     else 
  580.     echo "The new file does not exist."
  581.     fi
  582.     if [ -d "$old_mdsum_dir" ]; then
  583.     echo "The historical md5sum dir $old_mdsum_dir exists"
  584.     elif [ -f "$old_mdsum_file" ]; then
  585.     echo "The historical md5sum file $old_mdsum_file exists"
  586.     else
  587.     echo "Historical md5sums are not available"
  588.     fi
  589. fi
  590.  
  591.  
  592. if [ "X$PURGE" = "XYES" ]; then
  593.     if [ "X$VERBOSE" != "X" ]; then
  594.     echo >&2 "Preparing to purge ${dest_file}"
  595.     fi
  596.     purge_md5sum;
  597.     exit 0;
  598. fi
  599.  
  600. newsum=$(md5sum "$new_file" | awk '{print $1}')
  601.  
  602.  
  603. ######################################################################
  604. ########                                                     #########
  605. ########               Do the replacement                    #########
  606. ########                                                     #########
  607. ######################################################################
  608. # Step 1: If we have no record of this file, and dest file
  609. #         does, We need to determine how to initialize the
  610. #         ${old_mdsum_prefix}.old file..               
  611. if [ -e "$dest_file" ]; then
  612.     destsum=$(md5sum "$dest_file"  | awk '{print $1}');
  613.     if [ "X$lastsum" = "X" ]; then
  614. #      a: If we have a directory containing historical md5sums of this
  615. #         file in question, we should look and see if the currently
  616. #         installed file matches any of the old md5sums; in which case
  617. #         it can be silently replaced.
  618.     if [ -d "$old_mdsum_dir" -o -f "$old_mdsum_file" ]; then
  619.         if [ -d "$old_mdsum_dir"  ]; then
  620.         for file in ${old_mdsum_dir}/*; do
  621.             oldsum=$(cat "$file"  | awk '{print $1}');
  622.             if [ "$oldsum" = "$destsum"  ]; then
  623.             if [ "X$force_conffold" = "X" ]; then
  624. #                           Bingo! replace, set the md5sum, and we are done 
  625.                 if [ "X$VERBOSE" != "X" ]; then
  626.                 echo >&2 \
  627.                     "Replacing config file $dest_file with new version"
  628.                 fi
  629.                 replace_conf_file;
  630.                 exit 0;
  631.             else
  632.                 replace_md5sum;
  633.                 cp -f "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  634.                 exit 0;
  635.             fi
  636.             fi
  637.         done
  638.         elif [ -f "$old_mdsum_file" ]; then
  639.         oldsum=$(egrep "^${destsum}" "$old_mdsum_file" || true)
  640.         if [ "X$oldsum" != "X" ]; then
  641. #                    Bingo
  642.             if [ "X$force_conffold" = "X" ]; then
  643.             if [ "X$VERBOSE" != "X" ]; then
  644.                 echo >&2 \
  645.                 "Replacing config file $dest_file with new version"
  646.             fi
  647.             replace_conf_file;
  648.             exit 0;
  649.             else
  650.             replace_md5sum;
  651.             cp -f "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  652.             exit 0;
  653.             fi
  654.         fi
  655.         fi
  656. #       Well, nothing matched. We now check to see if the
  657. #       maintainer has an opinion on how to set the ``md5sum of the
  658. #       previously installed version'', since we have no way of
  659. #       determining that automatically. Please note that unless
  660. #       there are limited number of previously released packages
  661. #       (like just one), the maintainer is also making a guess at
  662. #       this point by supplying a historical md5sum default file. 
  663.         if [ "X$VERBOSE" != "X" ]; then
  664.         echo >&2 "Histotical md5sums did not match."
  665.         fi
  666.         if [ -d "$old_mdsum_dir"  ]; then
  667.         if [ -e "${old_mdsum_dir}/default" ]; then
  668.             if [ "X$VERBOSE" != "X" ]; then
  669.             echo >&2 "However, a default entry exists, using it."
  670.             fi
  671.             lastsum=$(cat "${old_mdsum_dir}/default" | \
  672.             awk '{print $1;}')
  673.             do_replace_md5sum=1;
  674.         fi
  675.         elif [ -f "$old_mdsum_file" ]; then
  676.         oldsum=$(egrep "[[:space:]]default$" "$old_mdsum_file" | \
  677.             awk '{print $1;}')
  678.         if [ "X$oldsum" != "X" ]; then
  679. #                    Bingo
  680.             lastsum=$oldsum;
  681.             do_replace_md5sum=1;
  682.         fi
  683.         fi
  684.     fi
  685.  
  686. #       At this point, we are almost certain that either the
  687. #       historical record of md5sums is not complete, or the user has
  688. #       changed the configuration file. Rather than guessing and
  689. #       chosing one of the historical md5sums, we fall through to the
  690. #       solution used if there had been no historical md5sums
  691. #       directory/file.
  692.     if [ "X$lastsum" = "X" ]; then
  693. #      b: We do not have a historical list of md5sums, or none
  694. #         matched, and we still need to initialize the
  695. #         ${old_mdsum_prefix}.old file. We can't determine whther or
  696. #         not they made any changes, so we err on the side of caution
  697. #         and ask'
  698.         if [ "X$VERBOSE" != "X" ]; then
  699.         echo >&2 "No match found, we shall ask."
  700.         fi
  701.         lastsum='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
  702.     fi # the old md5sum file does not exist, and the historical
  703.        # record failed
  704.     fi # the old md5sum file does not exist (bug))
  705. else  # "$dest_file" does not exist
  706. # Step 2: If destfile does not exist, create it, set the file
  707. #         "${old_mdsum_prefix}.old" to the md5sum of the new file, and we
  708. #         are done
  709.     if [ "X$lastsum" = "X" ]; then
  710.         # Ok, so there is no indication that the package was ever
  711.         # installed on this machine.
  712.     echo >&2 ""
  713.     echo >&2 "Creating config file $dest_file with new version"
  714.     replace_conf_file;
  715.     exit 0;
  716.     elif [ "$lastsum" = "$newsum" ]; then
  717.         # OK, new version of the file is the same as the last version
  718.         # we saw. Since the user apparently has deleted the file,
  719.         # nothing needs be done, unless we have been told differently
  720.         if [ "X$force_conffmiss" != "X" ]; then
  721.             echo >&2 ""
  722.         echo >&2 "Recreating deleted config file $dest_file with new version, as asked"
  723.         replace_conf_file;
  724.         exit 0;
  725.         else
  726.             echo >&2 "Not replacing deleted config file $dest_file";
  727.         fi
  728.         
  729.     else
  730.         # OK. New upstream version. 
  731.         if [ "X$force_conffmiss" != "X" ]; then
  732.             # User has said to replace missing files, so we do so, no
  733.             # questions asked.
  734.             echo >&2 ""
  735.         echo >&2 "Recreating deleted config file $dest_file with new version, as asked"
  736.         replace_conf_file;
  737.         exit 0;
  738.         else
  739.             # Even though the user has deleted this file, they should
  740.             # be asked now, unless specified otherwise.
  741.             if [ "X$force_conffold" = "X" ]; then
  742.                 destsum='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
  743.             else
  744.                 exit 0;
  745.         fi    
  746.         fi            
  747.     fi
  748. fi
  749.  
  750. # Here, the destfile exists.
  751.  
  752. # step 3: If the old md5sum and the md5sum of the new file
  753. #         do not match, we need to take action.
  754. if [ "$lastsum" = "$newsum" ]; then
  755.     if [ "X$VERBOSE" != "X" ]; then
  756.     echo >&2 "md5sums match, nothing needs be done."
  757.     fi
  758.     if [ "X$do_replace_md5sum" != "X" ]; then
  759.     replace_md5sum;
  760.     fi
  761.     exit 0;            # Hah. Match. We are done.
  762. fi
  763. #      a: If the md5sum of the dest file is the same as lastsum, replace the 
  764. #         destfile, saying we are replacing old config files
  765. if [ "$destsum" = "$lastsum" ]; then
  766.     if [ "X$force_conffold" = "X" ]; then
  767.     echo >&2 "Replacing config file $dest_file with new version"
  768.     replace_conf_file;
  769.     exit 0;
  770.     else
  771.     replace_md5sum;
  772.     cp -f "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  773.     exit 0;
  774.     fi
  775. else
  776. #      b: If the md5sum of the dest file differs from lastsum, we need to ask
  777. #         the user what action to take.
  778.     if [ "X$force_conffnew" != "X" ]; then
  779.     echo >&2 "Replacing config file $dest_file with new version"
  780.     echo >&2 "even though the files differ, since you asked for it"
  781.     replace_conf_file;
  782.     exit 0;
  783.     fi
  784.     if [ "X$force_conffold" != "X" ]; then
  785.     replace_md5sum;
  786.     cp -f "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  787.     exit 0;
  788.     fi
  789. #      c: If the destination file is the same as the new maintianer provided one,
  790. #         we need do nothing.
  791.     if [ "$newsum" = "$destsum" ]; then
  792.     if [ "X$VERBOSE" != "X" ]; then
  793.         echo >&2 "md5sums of the file in place matches, nothing needs be done."
  794.     fi
  795.     replace_md5sum;
  796.     exit 0;            # Hah. Match. We are done.
  797.     fi
  798.  
  799.  
  800.     done='NO';
  801.     while [ "X$done" = "XNO" ]; do
  802.     if [ "$DEBCONF_OK" = "YES" ] && [ "$DEBIAN_HAS_FRONTEND" ]; then
  803.         # Use debconf to prompt.
  804.         if [ "X$THREEWAY" != "X" -a -e "$statedir/cache/$cached_file" ]; then
  805.             templ=ucf/changeprompt_threeway
  806.         else
  807.             templ=ucf/changeprompt
  808.         fi
  809.         db_fset $templ seen false
  810.         db_reset $templ
  811.         db_subst $templ FILE "$dest_file"
  812.         db_subst $templ BASENAME $(basename "$dest_file")
  813.         db_input critical $templ || true
  814.         if ! db_go; then
  815.             # The current ucf interface does not provide a way for it
  816.             # to tell its caller that the user chose to back up.
  817.             # However, we could get here, if the caller turned on
  818.             # debconf's backup capb. The best thing to do seems to be
  819.             # to ignore requests to back up.
  820.             continue
  821.         fi
  822.         db_get $templ
  823.         ANSWER="$RET"
  824.     else
  825.         # Prompt without using debconf.
  826.         cat >&2 <<EOPRMT
  827. Configuration file \`$dest_file'
  828.  ==> File on system created by you or by a script.
  829.  ==> File also in package provided by package maintainer.
  830.    What would you like to do about it ?  Your options are:
  831.     Y or I  : install the package maintainer's version
  832.     N or O  : keep your currently-installed version
  833.       D     : show the differences between the versions
  834.       S     : show the side-by-side differences between the versions
  835. EOPRMT
  836.         if [ "X$THREEWAY" != "X" -a -e "$statedir/cache/$cached_file" ]; then
  837.             cat >&2 <<EOTD
  838.     3 or T  : show a three way difference between current, older,
  839.               and new versions of the file
  840.       M     : Do a 3 way merge between current, older,
  841.               and new versions of the file [Very Experimental]
  842. EOTD
  843.         fi
  844.         cat >&2 <<EOPEND
  845.       Z     : start a new shell to examine the situation
  846.  The default action is to keep your current version.
  847. EOPEND
  848.         if [ "X$THREEWAY" != "X" -a -e "$statedir/cache/$cached_file" ]; then
  849.             echo -n >&2 "*** " $(basename "$dest_file") \
  850.                 " (Y/I/N/O/D/3/T/M/Z) [default=N] ?"
  851.         else
  852.             echo -n >&2 "*** " $(basename "$dest_file") \
  853.                 " (Y/I/N/O/D/Z) [default=N] ?"
  854.         fi
  855.           read -e ANSWER </dev/tty
  856.     fi
  857.  
  858.     case "$ANSWER" in
  859.         "$choice_install_new"|y|Y|I|i)
  860.         echo >&2 "Replacing config file $dest_file with new version"
  861.         RETAIN_OLD=YES
  862.         replace_conf_file;
  863.         exit 0;
  864.         ;;
  865.         "$choice_diff"|D|d)
  866.         if [ -e "$dest_file" ]; then
  867.             ( diff -uBbw "$dest_file" "$new_file" | \
  868.             sensible-pager ) </dev/tty >/dev/tty
  869.         else
  870.             ( diff -uBbw /dev/null "$new_file" | \
  871.             sensible-pager ) </dev/tty >/dev/tty
  872.         fi
  873.         if [ "X$STOP" != "X" ]; then
  874.             echo >&2 "Please hit enter to continue";
  875.             read -e ANSWER </dev/tty
  876.         fi
  877.         ;;
  878.         "$choice_sdiff"|S|s)
  879.         if [ -e "$dest_file" ]; then
  880.             ( sdiff -BbW "$dest_file" "$new_file" | \
  881.             sensible-pager ) </dev/tty >/dev/tty
  882.         else
  883.             ( sdiff -BbW /dev/null "$new_file" | \
  884.             sensible-pager ) </dev/tty >/dev/tty
  885.         fi
  886.         if [ "X$STOP" != "X" ]; then
  887.             echo >&2 "Please hit enter to continue";
  888.             read -e ANSWER </dev/tty
  889.         fi
  890.         ;;
  891.         "$choice_diff_threeway"|3|t|T)
  892.         if [ -e "$statedir/cache/$cached_file" \
  893.             -a "X$THREEWAY" != "X" ]; then
  894.                     if [ -e "$dest_file" ]; then
  895.                 ( diff3 -L Current -L Older -L New -A \
  896.                 "$dest_file" "$statedir/cache/$cached_file" \
  897.                 "$new_file" | sensible-pager ) >/dev/tty </dev/tty
  898.                     else
  899.                         ( diff3 -L Current -L Older -L New -A \
  900.                 /dev/null "$statedir/cache/$cached_file" \
  901.                 "$new_file" | sensible-pager ) >/dev/tty </dev/tty
  902.                     fi
  903.             if [ "X$STOP" != "X" ]; then
  904.             echo >&2 "Please hit enter to continue";
  905.             read -e ANSWER </dev/tty
  906.             fi
  907.         else 
  908.             if [ -e "$dest_file" ]; then
  909.             ( diff -uBbw "$dest_file" "$new_file" | \
  910.                 sensible-pager ) </dev/tty >/dev/tty
  911.             else
  912.             ( diff -uBbw /dev/null "$new_file" | \
  913.                 sensible-pager ) </dev/tty >/dev/tty
  914.             fi
  915.  
  916.             if [ "X$STOP" != "X" ]; then
  917.             echo >&2 "Please hit enter to continue";
  918.             read -e ANSWER </dev/tty
  919.             fi
  920.         fi
  921.         ;;
  922.         "$choice_merge_threeway"|M|m)
  923.         echo >&2 "Merging changes into the new version new version"
  924.         if [ -e "$statedir/cache/$cached_file" \
  925.             -a "X$THREEWAY" != "X" ]; then
  926.             diff3 -L Current -L Older -L New -m \
  927.             "$dest_file" "$statedir/cache/$cached_file" \
  928.             "$new_file" > $dest_file.${NEW_SUFFIX} || { \
  929.             
  930.             echo >&2
  931.                 echo >&2 " Conflicts found! Please edit" \
  932.                 "\`$dest_file' and sort them out manually."
  933.             echo >&2 " The file \`$dest_file.${OLD_SUFFIX}' is a" \
  934.                 "backup of your old configuration file."
  935.             echo >&2
  936.             }
  937.             new_file="$dest_file.${NEW_SUFFIX}"
  938.             RETAIN_OLD=YES
  939.             replace_conf_file;
  940.         else 
  941.             replace_conf_file;
  942.         fi
  943.                 rm -f "$dest_file.${NEW_SUFFIX}" # don't need this around no mo'
  944.                 exit 0
  945.         ;;
  946.         "$choice_shell"|Z|z)
  947.         bash >/dev/tty </dev/tty || true
  948.         ;;
  949.         $choice_keep_current|n|N|o|O|'')
  950.         replace_md5sum;
  951.  
  952.         cp -f "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  953.         exit 0;
  954.         ;;
  955.         *)
  956.         if [ "$DEBCONF_OK" = "YES" ]; then
  957.             echo "Error: unknown response from debconf:'$RET'" >&2
  958.             exit 1
  959.         else
  960.             echo
  961.             echo "Please answer with one of the single letters listed." >&2
  962.             echo
  963.         fi
  964.     esac
  965.     done
  966. fi
  967.  
  968. db_stop
  969.  
  970. exit 0;
  971.