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