home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / dpkg / info / xserver-xorg.prerm < prev    next >
Encoding:
Text File  |  2009-04-03  |  30.7 KB  |  964 lines

  1. #!/bin/sh
  2. # Debian xserver-xorg package pre-removal script
  3. # Copyright 1998--2001, 2003, 2004 Branden Robinson.
  4. # Licensed under the GNU General Public License, version 2.  See the file
  5. # /usr/share/common-licenses/GPL or <http://www.gnu.org/copyleft/gpl.txt>.
  6. # Acknowledgements to Stephen Early, Mark Eichin, and Manoj Srivastava.
  7.  
  8.  
  9. set -e
  10.  
  11. # debconf may not be available if some massive purging is going on
  12. HAVE_DEBCONF=
  13. if [ -e /usr/share/debconf/confmodule ]; then
  14.   . /usr/share/debconf/confmodule
  15.   HAVE_DEBCONF=yes
  16. fi
  17.  
  18. THIS_PACKAGE=xserver-xorg
  19. THIS_SCRIPT=prerm
  20.  
  21. # $Id$
  22.  
  23. # This is the X Strike Force shell library for X Window System package
  24. # maintainer scripts.  It serves to define shell functions commonly used by
  25. # such packages, and performs some error checking necessary for proper operation
  26. # of those functions.  By itself, it does not "do" much; the maintainer scripts
  27. # invoke the functions defined here to accomplish package installation and
  28. # removal tasks.
  29.  
  30. # If you are reading this within a Debian package maintainer script (e.g.,
  31. # /var/lib/dpkg)info/PACKAGE.{config,preinst,postinst,prerm,postrm}), you can
  32. # skip past this library by scanning forward in this file to the string
  33. # "GOBSTOPPER".
  34.  
  35. SOURCE_VERSION=1:7.4~5ubuntu18
  36. OFFICIAL_BUILD=
  37.  
  38. # Use special abnormal exit codes so that problems with this library are more
  39. # easily tracked down.
  40. SHELL_LIB_INTERNAL_ERROR=86
  41. SHELL_LIB_THROWN_ERROR=74
  42. SHELL_LIB_USAGE_ERROR=99
  43.  
  44. # old -> new variable names
  45. if [ -z "$DEBUG_XORG_PACKAGE" ] && [ -n "$DEBUG_XFREE86_PACKAGE" ]; then
  46.   DEBUG_XORG_PACKAGE="$DEBUG_XFREE86_PACKAGE"
  47. fi
  48. if [ -z "$DEBUG_XORG_DEBCONF" ] && [ -n "$DEBUG_XFREE86_DEBCONF" ]; then
  49.   DEBUG_XORG_DEBCONF="$DEBUG_XFREE86_DEBCONF"
  50. fi
  51.  
  52. # initial sanity checks
  53. if [ -z "$THIS_PACKAGE" ]; then
  54.   cat >&2 <<EOF
  55. Error: package maintainer script attempted to use shell library without
  56. definining \$THIS_PACKAGE shell variable.  Please report the package name,
  57. version, and the text of this error message to the Debian Bug Tracking System.
  58. Visit <http://www.debian.org/Bugs/Reporting> on the World Wide Web for
  59. instructions, read the file /usr/share/doc/debian/bug-reporting.txt from the
  60. "doc-debian" package, or install the "reportbug" package and use the command of
  61. the same name to file a report against version $SOURCE_VERSION of this package.
  62. EOF
  63.   exit $SHELL_LIB_USAGE_ERROR
  64. fi
  65.  
  66. if [ -z "$THIS_SCRIPT" ]; then
  67.   cat >&2 <<EOF
  68. Error: package maintainer script attempted to use shell library without
  69. definining \$THIS_SCRIPT shell variable.  Please report the package name,
  70. version, and the text of this error message to the Debian Bug Tracking System.
  71. Visit <http://www.debian.org/Bugs/Reporting> on the World Wide Web for
  72. instructions, read the file /usr/share/doc/debian/bug-reporting.txt from the
  73. "doc-debian" package, or install the "reportbug" package and use the command of
  74. the same name to file a report against version $SOURCE_VERSION of the
  75. "$THIS_PACKAGE" package.
  76. EOF
  77.   exit $SHELL_LIB_USAGE_ERROR
  78. fi
  79.  
  80. ARCHITECTURE="$(dpkg --print-installation-architecture)"
  81.  
  82. if [ "$1" = "reconfigure" ] || [ -n "$DEBCONF_RECONFIGURE" ]; then
  83.   RECONFIGURE="true"
  84. else
  85.   RECONFIGURE=
  86. fi
  87.  
  88. if ([ "$1" = "install" ] || [ "$1" = "configure" ]) && [ -z "$2" ]; then
  89.   FIRSTINST="yes"
  90. fi
  91.  
  92. if [ -z "$RECONFIGURE" ] && [ -z "$FIRSTINST" ]; then
  93.   UPGRADE="yes"
  94. fi
  95.  
  96. trap "message;\
  97.       message \"Received signal.  Aborting $THIS_PACKAGE package $THIS_SCRIPT script.\";\
  98.       message;\
  99.       exit 1" HUP INT QUIT TERM
  100.  
  101. reject_nondigits () {
  102.   # syntax: reject_nondigits [ operand ... ]
  103.   #
  104.   # scan operands (typically shell variables whose values cannot be trusted) for
  105.   # characters other than decimal digits and barf if any are found
  106.   while [ -n "$1" ]; do
  107.     # does the operand contain anything but digits?
  108.     if ! expr "$1" : "[[:digit:]]\+$" > /dev/null 2>&1; then
  109.       # can't use die(), because it wraps message() which wraps this function
  110.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_nondigits() encountered" \
  111.            "possibly malicious garbage \"$1\"" >&2
  112.       exit $SHELL_LIB_THROWN_ERROR
  113.     fi
  114.     shift
  115.   done
  116. }
  117.  
  118. reject_whitespace () {
  119.   # syntax: reject_whitespace [ operand ]
  120.   #
  121.   # scan operand (typically a shell variable whose value cannot be trusted) for
  122.   # whitespace characters and barf if any are found
  123.   if [ -n "$1" ]; then
  124.     # does the operand contain any whitespace?
  125.     if expr "$1" : "[[:space:]]" > /dev/null 2>&1; then
  126.       # can't use die(), because I want to avoid forward references
  127.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_whitespace() encountered" \
  128.            "possibly malicious garbage \"$1\"" >&2
  129.       exit $SHELL_LIB_THROWN_ERROR
  130.     fi
  131.   fi
  132. }
  133.  
  134. reject_unlikely_path_chars () {
  135.   # syntax: reject_unlikely_path_chars [ operand ... ]
  136.   #
  137.   # scan operands (typically shell variables whose values cannot be trusted) for
  138.   # characters unlikely to be seen in a path and which the shell might
  139.   # interpret and barf if any are found
  140.   while [ -n "$1" ]; do
  141.     # does the operand contain any funny characters?
  142.     if expr "$1" : '.*[!$&()*;<>?|].*' > /dev/null 2>&1; then
  143.       # can't use die(), because I want to avoid forward references
  144.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_unlikely_path_chars()" \
  145.            "encountered possibly malicious garbage \"$1\"" >&2
  146.       exit $SHELL_LIB_THROWN_ERROR
  147.     fi
  148.     shift
  149.   done
  150. }
  151.  
  152. # Query the terminal to establish a default number of columns to use for
  153. # displaying messages to the user.  This is used only as a fallback in the
  154. # event the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while
  155. # the script is running, and this cannot, only being calculated once.)
  156. DEFCOLUMNS=$(stty size 2> /dev/null | awk '{print $2}') || true
  157. if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" > /dev/null 2>&1; then
  158.   DEFCOLUMNS=80
  159. fi
  160.  
  161. message () {
  162.   # pretty-print messages of arbitrary length
  163.   reject_nondigits "$COLUMNS"
  164.   echo "$*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS} >&2
  165. }
  166.  
  167. observe () {
  168.   # syntax: observe message ...
  169.   #
  170.   # issue observational message suitable for logging someday when support for
  171.   # it exists in dpkg
  172.   if [ -n "$DEBUG_XORG_PACKAGE" ]; then
  173.     message "$THIS_PACKAGE $THIS_SCRIPT note: $*"
  174.   fi
  175. }
  176.  
  177. warn () {
  178.   # syntax: warn message ...
  179.   #
  180.   # issue warning message suitable for logging someday when support for
  181.   # it exists in dpkg; also send to standard error
  182.   message "$THIS_PACKAGE $THIS_SCRIPT warning: $*"
  183. }
  184.  
  185. die () {
  186.   # syntax: die message ...
  187.   #
  188.   # exit script with error message
  189.   message "$THIS_PACKAGE $THIS_SCRIPT error: $*"
  190.   exit $SHELL_LIB_THROWN_ERROR
  191. }
  192.  
  193. internal_error () {
  194.   # exit script with error; essentially a "THIS SHOULD NEVER HAPPEN" message
  195.   message "internal error: $*"
  196.   if [ -n "$OFFICIAL_BUILD" ]; then
  197.     message "Please report a bug in the $THIS_SCRIPT script of the" \
  198.             "$THIS_PACKAGE package, version $SOURCE_VERSION to the Debian Bug" \
  199.             "Tracking System.  Include all messages above that mention the" \
  200.             "$THIS_PACKAGE package.  Visit " \
  201.             "<http://www.debian.org/Bugs/Reporting> on the World Wide Web for" \
  202.             "instructions, read the file" \
  203.             "/usr/share/doc/debian/bug-reporting.txt from the doc-debian" \
  204.             "package, or install the reportbug package and use the command of" \
  205.             "the same name to file a report."
  206.   fi
  207.   exit $SHELL_LIB_INTERNAL_ERROR
  208. }
  209.  
  210. usage_error () {
  211.   message "usage error: $*"
  212.   message "Please report a bug in the $THIS_SCRIPT script of the" \
  213.           "$THIS_PACKAGE package, version $SOURCE_VERSION to the Debian Bug" \
  214.           "Tracking System.  Include all messages above that mention the" \
  215.           "$THIS_PACKAGE package.  Visit " \
  216.           "<http://www.debian.org/Bugs/Reporting> on the World Wide Web for" \
  217.           "instructions, read the file" \
  218.           "/usr/share/doc/debian/bug-reporting.txt from the doc-debian" \
  219.           "package, or install the reportbug package and use the command of" \
  220.           "the same name to file a report."
  221.   exit $SHELL_LIB_USAGE_ERROR
  222. }
  223.  
  224.  
  225. maplink () {
  226.   # returns what symlink should point to; i.e., what the "sane" answer is
  227.   # Keep this in sync with the debian/*.links files.
  228.   # This is only needed for symlinks to directories.
  229.   #
  230.   # XXX: Most of these look wrong in the X11R7 world and need to be fixed.
  231.   # If we've stopped using this function, fixing it might enable us to re-enable
  232.   # it again and catch more errors.
  233.   case "$1" in
  234.     /etc/X11/xkb/compiled) echo /var/lib/xkb ;;
  235.     /etc/X11/xkb/xkbcomp) echo /usr/X11R6/bin/xkbcomp ;;
  236.     /usr/X11R6/lib/X11/app-defaults) echo /etc/X11/app-defaults ;;
  237.     /usr/X11R6/lib/X11/fs) echo /etc/X11/fs ;;
  238.     /usr/X11R6/lib/X11/lbxproxy) echo /etc/X11/lbxproxy ;;
  239.     /usr/X11R6/lib/X11/proxymngr) echo /etc/X11/proxymngr ;;
  240.     /usr/X11R6/lib/X11/rstart) echo /etc/X11/rstart ;;
  241.     /usr/X11R6/lib/X11/twm) echo /etc/X11/twm ;;
  242.     /usr/X11R6/lib/X11/xdm) echo /etc/X11/xdm ;;
  243.     /usr/X11R6/lib/X11/xinit) echo /etc/X11/xinit ;;
  244.     /usr/X11R6/lib/X11/xkb) echo /etc/X11/xkb ;;
  245.     /usr/X11R6/lib/X11/xserver) echo /etc/X11/xserver ;;
  246.     /usr/X11R6/lib/X11/xsm) echo /etc/X11/xsm ;;
  247.     /usr/bin/X11) echo ../X11R6/bin ;;
  248.     /usr/bin/rstartd) echo ../X11R6/bin/rstartd ;;
  249.     /usr/include/X11) echo ../X11R6/include/X11 ;;
  250.     /usr/lib/X11) echo ../X11R6/lib/X11 ;;
  251.     *) internal_error "maplink() called with unknown path \"$1\"" ;;
  252.   esac
  253. }
  254.  
  255. analyze_path () {
  256.   # given a supplied set of pathnames, break each one up by directory and do an
  257.   # ls -dl on each component, cumulatively; i.e.
  258.   # analyze_path /usr/X11R6/bin -> ls -dl /usr /usr/X11R6 /usr/X11R6/bin
  259.   # Thanks to Randolph Chung for this clever hack.
  260.  
  261.   local f g
  262.  
  263.   while [ -n "$1" ]; do
  264.     reject_whitespace "$1"
  265.     g=
  266.     message "Analyzing $1:"
  267.     for f in $(echo "$1" | tr / \  ); do
  268.       if [ -e /$g$f ]; then
  269.         ls -dl /$g$f /$g$f.dpkg-* 2> /dev/null || true
  270.         g=$g$f/
  271.       else
  272.         message "/$g$f: nonexistent; directory contents of /$g:"
  273.         ls -l /$g
  274.         break
  275.       fi
  276.     done
  277.     shift
  278.   done
  279. }
  280.  
  281. find_culprits () {
  282.   local f p dpkg_info_dir possible_culprits smoking_guns bad_packages package \
  283.     msg
  284.  
  285.   reject_whitespace "$1"
  286.   message "Searching for overlapping packages..."
  287.   dpkg_info_dir=/var/lib/dpkg/info
  288.   if [ -d $dpkg_info_dir ]; then
  289.     if [ "$(echo $dpkg_info_dir/*.list)" != "$dpkg_info_dir/*.list" ]; then
  290.       possible_culprits=$(ls -1 $dpkg_info_dir/*.list | egrep -v \
  291.         "(xbase-clients|x11-common|xfs|xlibs)")
  292.       if [ -n "$possible_culprits" ]; then
  293.         smoking_guns=$(grep -l "$1" $possible_culprits || true)
  294.         if [ -n "$smoking_guns" ]; then
  295.           bad_packages=$(printf "\\n")
  296.           for f in $smoking_guns; do
  297.             # too bad you can't nest parameter expansion voodoo
  298.             p=${f%*.list}      # strip off the trailing ".list"
  299.             package=${p##*/}   # strip off the directories
  300.             bad_packages=$(printf "%s\n%s" "$bad_packages" "$package")
  301.           done
  302.           msg=$(cat <<EOF
  303. The following packages appear to have file overlaps with the X.Org packages;
  304. these packages are either very old, or in violation of Debian Policy.  Try
  305. upgrading each of these packages to the latest available version if possible:
  306. for example, with the command "apt-get install".  If no newer version of a
  307. package is available, you will have to remove it; for example, with the command
  308. "apt-get remove".  If even the latest available version of the package has
  309. this file overlap, please file a bug against that package with the Debian Bug
  310. Tracking System.  You may want to refer the package maintainer to section 12.8
  311. of the Debian Policy manual.
  312. EOF
  313. )
  314.           message "$msg"
  315.           message "The overlapping packages are: $bad_packages"
  316.         else
  317.           message "no overlaps found."
  318.         fi
  319.       fi
  320.     else
  321.       message "cannot search; no matches for $dpkg_info_dir/*.list."
  322.     fi
  323.   else
  324.     message "cannot search; $dpkg_info_dir does not exist."
  325.   fi
  326. }
  327.  
  328. # we require a readlink command or shell function
  329. if ! which readlink > /dev/null 2>&1; then
  330.   message "The readlink command was not found.  Please install version" \
  331.           "1.13.1 or later of the debianutils package."
  332.   readlink () {
  333.     # returns what symlink in $1 actually points to
  334.     perl -e '$l = shift; exit 1 unless -l $l; $r = readlink $l; exit 1 unless $r; print "$r\n"' "$1"
  335.   }
  336. fi
  337.  
  338. check_symlink () {
  339.   # syntax: check_symlink symlink
  340.   #
  341.   # See if specified symlink points where it is supposed to.  Return 0 if it
  342.   # does, and 1 if it does not.
  343.   #
  344.   # Primarily used by check_symlinks_and_warn() and check_symlinks_and_bomb().
  345.  
  346.   local symlink
  347.  
  348.   # validate arguments
  349.   if [ $# -ne 1 ]; then
  350.     usage_error "check_symlink() called with wrong number of arguments;" \
  351.                 "expected 1, got $#"
  352.     exit $SHELL_LIB_USAGE_ERROR
  353.   fi
  354.  
  355.   symlink="$1"
  356.  
  357.   if [ "$(maplink "$symlink")" = "$(readlink "$symlink")" ]; then
  358.     return 0
  359.   else
  360.     return 1
  361.   fi
  362. }
  363.  
  364. check_symlinks_and_warn () {
  365.   # syntax: check_symlinks_and_warn symlink ...
  366.   #
  367.   # For each argument, check for symlink sanity, and warn if it isn't sane.
  368.   #
  369.   # Call this function from a preinst script in the event $1 is "upgrade" or
  370.   # "install".
  371.  
  372.   local errmsg symlink
  373.  
  374.   # validate arguments
  375.   if [ $# -lt 1 ]; then
  376.     usage_error "check_symlinks_and_warn() called with wrong number of" \
  377.                 "arguments; expected at least 1, got $#"
  378.     exit $SHELL_LIB_USAGE_ERROR
  379.   fi
  380.  
  381.   while [ -n "$1" ]; do
  382.     symlink="$1"
  383.     if [ -L "$symlink" ]; then
  384.       if ! check_symlink "$symlink"; then
  385.         observe "$symlink symbolic link points to wrong location" \
  386.                 "$(readlink "$symlink"); removing"
  387.         rm "$symlink"
  388.       fi
  389.     elif [ -e "$symlink" ]; then
  390.       errmsg="$symlink exists and is not a symbolic link; this package cannot"
  391.       errmsg="$errmsg be installed until this"
  392.       if [ -f "$symlink" ]; then
  393.         errmsg="$errmsg file"
  394.       elif [ -d "$symlink" ]; then
  395.         errmsg="$errmsg directory"
  396.       else
  397.         errmsg="$errmsg thing"
  398.       fi
  399.       errmsg="$errmsg is removed"
  400.       die "$errmsg"
  401.     fi
  402.     shift
  403.   done
  404. }
  405.  
  406. check_symlinks_and_bomb () {
  407.   # syntax: check_symlinks_and_bomb symlink ...
  408.   #
  409.   # For each argument, check for symlink sanity, and bomb if it isn't sane.
  410.   #
  411.   # Call this function from a postinst script.
  412.  
  413.   local problem symlink
  414.  
  415.   # validate arguments
  416.   if [ $# -lt 1 ]; then
  417.     usage_error "check_symlinks_and_bomb() called with wrong number of"
  418.                 "arguments; expected at least 1, got $#"
  419.     exit $SHELL_LIB_USAGE_ERROR
  420.   fi
  421.  
  422.   while [ -n "$1" ]; do
  423.     problem=
  424.     symlink="$1"
  425.     if [ -L "$symlink" ]; then
  426.       if ! check_symlink "$symlink"; then
  427.         problem=yes
  428.         warn "$symlink symbolic link points to wrong location" \
  429.              "$(readlink "$symlink")"
  430.       fi
  431.     elif [ -e "$symlink" ]; then
  432.       problem=yes
  433.       warn "$symlink is not a symbolic link"
  434.     else
  435.       problem=yes
  436.       warn "$symlink symbolic link does not exist"
  437.     fi
  438.     if [ -n "$problem" ]; then
  439.       analyze_path "$symlink" "$(readlink "$symlink")"
  440.       find_culprits "$symlink"
  441.       die "bad symbolic links on system"
  442.     fi
  443.     shift
  444.   done
  445. }
  446.  
  447. font_update () {
  448.   # run $UPDATECMDS in $FONTDIRS
  449.  
  450.   local dir cmd shortcmd x_font_dir_prefix
  451.  
  452.   x_font_dir_prefix="/usr/share/fonts/X11"
  453.  
  454.   if [ -z "$UPDATECMDS" ]; then
  455.     usage_error "font_update() called but \$UPDATECMDS not set"
  456.   fi
  457.   if [ -z "$FONTDIRS" ]; then
  458.     usage_error "font_update() called but \$FONTDIRS not set"
  459.   fi
  460.  
  461.   reject_unlikely_path_chars "$UPDATECMDS"
  462.   reject_unlikely_path_chars "$FONTDIRS"
  463.  
  464.   for dir in $FONTDIRS; do
  465.     if [ -d "$x_font_dir_prefix/$dir" ]; then
  466.       for cmd in $UPDATECMDS; do
  467.         if which "$cmd" > /dev/null 2>&1; then
  468.           shortcmd=${cmd##*/}
  469.           observe "running $shortcmd in $dir font directory"
  470.       cmd_opts=
  471.           if [ "$shortcmd" = "update-fonts-alias" ]; then
  472.             cmd_opts=--x11r7-layout
  473.           fi
  474.           if [ "$shortcmd" = "update-fonts-dir" ]; then
  475.             cmd_opts=--x11r7-layout
  476.           fi
  477.           if [ "$shortcmd" = "update-fonts-scale" ]; then
  478.             cmd_opts=--x11r7-layout
  479.           fi
  480.           $cmd $cmd_opts $dir || warn "$cmd $cmd_opts $dir" \
  481.                               "failed; font directory data may not" \
  482.                               "be up to date"
  483.         else
  484.           warn "$cmd not found; not updating corresponding $dir font" \
  485.                "directory data"
  486.         fi
  487.       done
  488.     else
  489.       warn "$dir is not a directory; not updating font directory data"
  490.     fi
  491.   done
  492. }
  493.  
  494. remove_conffile_prepare () {
  495.   # syntax: remove_conffile_prepare filename official_md5sum ...
  496.   #
  497.   # Check a conffile "filename" against a list of canonical MD5 checksums.
  498.   # If the file's current MD5 checksum matches one of the "official_md5sum"
  499.   # operands provided, then prepare the conffile for removal from the system.
  500.   # We defer actual deletion until the package is configured so that we can
  501.   # roll this operation back if package installation fails.
  502.   #
  503.   # Call this function from a preinst script in the event $1 is "upgrade" or
  504.   # "install" and verify $2 to ensure the package is being upgraded from a
  505.   # version (or installed over a version removed-but-not-purged) prior to the
  506.   # one in which the conffile was obsoleted.
  507.  
  508.   local conffile current_checksum
  509.  
  510.   # validate arguments
  511.   if [ $# -lt 2 ]; then
  512.     usage_error "remove_conffile_prepare() called with wrong number of" \
  513.                 "arguments; expected at least 2, got $#"
  514.     exit $SHELL_LIB_USAGE_ERROR
  515.   fi
  516.  
  517.   conffile="$1"
  518.   shift
  519.  
  520.   # does the conffile even exist?
  521.   if [ -e "$conffile" ]; then
  522.     # calculate its checksum
  523.     current_checksum=$(md5sum < "$conffile" | sed 's/[[:space:]].*//')
  524.     # compare it to each supplied checksum
  525.     while [ -n "$1" ]; do
  526.       if [ "$current_checksum" = "$1" ]; then
  527.         # we found a match; move the confffile and stop looking
  528.         observe "preparing obsolete conffile $conffile for removal"
  529.         mv "$conffile" "$conffile.$THIS_PACKAGE-tmp"
  530.         break
  531.       fi
  532.       shift
  533.     done
  534.   fi
  535. }
  536.  
  537. remove_conffile_lookup () {
  538.   # syntax: remove_conffile_lookup package filename
  539.   #
  540.   # Lookup the md5sum of a conffile in dpkg's database, and prepare for removal
  541.   # if it matches the actual file's md5sum.
  542.   #
  543.   # Call this function when you would call remove_conffile_prepare but only
  544.   # want to check against dpkg's status database instead of known checksums.
  545.  
  546.   local package conffile old_md5sum
  547.  
  548.   # validate arguments
  549.   if [ $# -ne 2 ]; then
  550.     usage_error "remove_conffile_lookup() called with wrong number of" \
  551.                 "arguments; expected 1, got $#"
  552.     exit $SHELL_LIB_USAGE_ERROR
  553.   fi
  554.  
  555.   package="$1"
  556.   conffile="$2"
  557.  
  558.   if ! [ -e "$conffile" ]; then
  559.     return
  560.   fi
  561.   old_md5sum="$(dpkg-query -W -f='${Conffiles}' "$package" | \
  562.     awk '{ if (match($0, "^ '"$conffile"' ")) print $2}')"
  563.   if [ -n "$old_md5sum" ]; then
  564.     remove_conffile_prepare "$conffile" "$old_md5sum"
  565.   fi
  566. }
  567.  
  568. remove_conffile_commit () {
  569.   # syntax: remove_conffile_commit filename
  570.   #
  571.   # Complete the removal of a conffile "filename" that has become obsolete.
  572.   #
  573.   # Call this function from a postinst script after having used
  574.   # remove_conffile_prepare() in the preinst.
  575.  
  576.   local conffile
  577.  
  578.   # validate arguments
  579.   if [ $# -ne 1 ]; then
  580.     usage_error "remove_conffile_commit() called with wrong number of" \
  581.                 "arguments; expected 1, got $#"
  582.     exit $SHELL_LIB_USAGE_ERROR
  583.   fi
  584.  
  585.   conffile="$1"
  586.  
  587.   # if the temporary file created by remove_conffile_prepare() exists, remove it
  588.   if [ -e "$conffile.$THIS_PACKAGE-tmp" ]; then
  589.     observe "committing removal of obsolete conffile $conffile"
  590.     rm "$conffile.$THIS_PACKAGE-tmp"
  591.   fi
  592. }
  593.  
  594. remove_conffile_rollback () {
  595.   # syntax: remove_conffile_rollback filename
  596.   #
  597.   # Roll back the removal of a conffile "filename".
  598.   #
  599.   # Call this function from a postrm script in the event $1 is "abort-upgrade"
  600.   # or "abort-install" is  after having used remove_conffile_prepare() in the
  601.   # preinst.
  602.  
  603.   local conffile
  604.  
  605.   # validate arguments
  606.   if [ $# -ne 1 ]; then
  607.     usage_error "remove_conffile_rollback() called with wrong number of" \
  608.                 "arguments; expected 1, got $#"
  609.     exit $SHELL_LIB_USAGE_ERROR
  610.   fi
  611.  
  612.   conffile="$1"
  613.  
  614.   # if the temporary file created by remove_conffile_prepare() exists, move it
  615.   # back
  616.   if [ -e "$conffile.$THIS_PACKAGE-tmp" ]; then
  617.     observe "rolling back removal of obsolete conffile $conffile"
  618.     mv "$conffile.$THIS_PACKAGE-tmp" "$conffile"
  619.   fi
  620. }
  621.  
  622. replace_conffile_with_symlink_prepare () {
  623.   # syntax: replace_conffile_with_symlink_prepare oldfilename newfilename \
  624.   # official_md5sum ...
  625.   #
  626.   # Check a conffile "oldfilename" against a list of canonical MD5 checksums.
  627.   # If the file's current MD5 checksum matches one of the "official_md5sum"
  628.   # operands provided, then prepare the conffile for removal from the system.
  629.   # We defer actual deletion until the package is configured so that we can
  630.   # roll this operation back if package installation fails. Otherwise copy it
  631.   # to newfilename and let dpkg handle it through conffiles mechanism.
  632.   #
  633.   # Call this function from a preinst script in the event $1 is "upgrade" or
  634.   # "install" and verify $2 to ensure the package is being upgraded from a
  635.   # version (or installed over a version removed-but-not-purged) prior to the
  636.   # one in which the conffile was obsoleted.
  637.  
  638.   local conffile current_checksum
  639.  
  640.   # validate arguments
  641.   if [ $# -lt 3 ]; then
  642.     usage_error "replace_conffile_with_symlink_prepare() called with wrong" \
  643.                 " number of arguments; expected at least 3, got $#"
  644.     exit $SHELL_LIB_USAGE_ERROR
  645.   fi
  646.  
  647.   oldconffile="$1"
  648.   shift
  649.   newconffile="$1"
  650.   shift
  651.  
  652.   remove_conffile_prepare "$_oldconffile" "$@"
  653.   # If $oldconffile still exists, then md5sums didn't match.
  654.   # Copy it to new one.
  655.   if [ -f "$oldconffile" ]; then
  656.     cp "$oldconffile" "$newconffile"
  657.   fi
  658.  
  659. }
  660.  
  661. replace_conffile_with_symlink_commit () {
  662.   # syntax: replace_conffile_with_symlink_commit oldfilename
  663.   #
  664.   # Complete the removal of a conffile "oldfilename" that has been
  665.   # replaced by a symlink.
  666.   #
  667.   # Call this function from a postinst script after having used
  668.   # replace_conffile_with_symlink_prepare() in the preinst.
  669.  
  670.   local conffile
  671.  
  672.   # validate arguments
  673.   if [ $# -ne 1 ]; then
  674.     usage_error "replace_conffile_with_symlink_commit() called with wrong" \
  675.                 "number of arguments; expected 1, got $#"
  676.     exit $SHELL_LIB_USAGE_ERROR
  677.   fi
  678.  
  679.   conffile="$1"
  680.  
  681.   remove_conffile_commit "$conffile"
  682. }
  683.  
  684. replace_conffile_with_symlink_rollback () {
  685.   # syntax: replace_conffile_with_symlink_rollback oldfilename newfilename
  686.   #
  687.   # Roll back the replacing of a conffile "oldfilename" with symlink to
  688.   # "newfilename".
  689.   #
  690.   # Call this function from a postrm script in the event $1 is "abort-upgrade"
  691.   # or "abort-install" and verify $2 to ensure the package failed to upgrade
  692.   # from a version (or install over a version removed-but-not-purged) prior
  693.   # to the one in which the conffile was obsoleted.
  694.   # You should have  used replace_conffile_with_symlink_prepare() in the
  695.   # preinst.
  696.  
  697.   local conffile
  698.  
  699.   # validate arguments
  700.   if [ $# -ne 2 ]; then
  701.     usage_error "replace_conffile_with_symlink_rollback() called with wrong" \
  702.                 "number of arguments; expected 2, got $#"
  703.     exit $SHELL_LIB_USAGE_ERROR
  704.   fi
  705.  
  706.   oldconffile="$1"
  707.   newconffile="$2"
  708.  
  709.   remove_conffile_rollback "$_oldconffile"
  710.   if [ -f "$newconffile" ]; then
  711.     rm "$newconffile"
  712.   fi
  713. }
  714.  
  715. run () {
  716.   # syntax: run command [ argument ... ]
  717.   #
  718.   # Run specified command with optional arguments and report its exit status.
  719.   # Useful for commands whose exit status may be nonzero, but still acceptable,
  720.   # or commands whose failure is not fatal to us.
  721.   #
  722.   # NOTE: Do *not* use this function with db_get or db_metaget commands; in
  723.   # those cases the return value of the debconf command *must* be checked
  724.   # before the string returned by debconf is used for anything.
  725.  
  726.   local retval
  727.  
  728.   # validate arguments
  729.   if [ $# -lt 1 ]; then
  730.     usage_error "run() called with wrong number of arguments; expected at" \
  731.                 "least 1, got $#"
  732.     exit $SHELL_LIB_USAGE_ERROR
  733.   fi
  734.  
  735.   "$@" || retval=$?
  736.  
  737.   if [ ${retval:-0} -ne 0 ]; then
  738.     observe "command \"$*\" exited with status $retval"
  739.   fi
  740. }
  741.  
  742. register_x_lib_dir_with_ld_so () {
  743.   # syntax: register_x_lib_dir_with_ld_so
  744.   #
  745.   # Configure the dynamic loader ld.so to search /usr/X11R6/lib for shared
  746.   # libraries.
  747.   #
  748.   # Call this function from the postinst script of a package that places a
  749.   # shared library in /usr/X11R6/lib, before invoking ldconfig.
  750.  
  751.   local dir ldsoconf
  752.  
  753.   dir="/usr/X11R6/lib"
  754.   ldsoconf="/etc/ld.so.conf"
  755.  
  756.   # is the line not already present?
  757.   if ! fgrep -qsx "$dir" "$ldsoconf"; then
  758.     observe "adding $dir directory to $ldsoconf"
  759.     echo "$dir" >> "$ldsoconf"
  760.   fi
  761. }
  762.  
  763. deregister_x_lib_dir_with_ld_so () {
  764.   # syntax: deregister_x_lib_dir_with_ld_so
  765.   #
  766.   # Configure dynamic loader ld.so to not search /usr/X11R6/lib for shared
  767.   # libraries, if and only if no shared libaries remain there.
  768.   #
  769.   # Call this function from the postrm script of a package that places a shared
  770.   # library in /usr/X11R6/lib, in the event "$1" is "remove", and before
  771.   # invoking ldconfig.
  772.  
  773.   local dir ldsoconf fgrep_status cmp_status
  774.  
  775.   dir="/usr/X11R6/lib"
  776.   ldsoconf="/etc/ld.so.conf"
  777.  
  778.   # is the line present?
  779.   if fgrep -qsx "$dir" "$ldsoconf"; then
  780.     # are there any shared objects in the directory?
  781.     if [ "$(echo "$dir"/lib*.so.*.*)" = "$dir/lib*.so.*.*" ]; then
  782.       # glob expansion produced nothing, so no shared libraries are present
  783.       observe "removing $dir directory from $ldsoconf"
  784.       # rewrite the file (very carefully)
  785.       set +e
  786.       fgrep -svx "$dir" "$ldsoconf" > "$ldsoconf.dpkg-tmp"
  787.       fgrep_status=$?
  788.       set -e
  789.       case $fgrep_status in
  790.         0|1) ;; # we don't actually care if any lines matched or not
  791.         *) die "error reading \"$ldsoconf\"; fgrep exited with status" \
  792.           "$fgrep_status" ;;
  793.       esac
  794.       set +e
  795.       cmp -s "$ldsoconf.dpkg-tmp" "$ldsoconf"
  796.       cmp_status=$?
  797.       set -e
  798.       case $cmp_status in
  799.         0) rm "$ldsoconf.dpkg-tmp" ;; # files are identical
  800.         1) mv "$ldsoconf.dpkg-tmp" "$ldsoconf" ;; # files differ
  801.         *) die "error comparing \"$ldsoconf.dpkg-tmp\" to \"$ldsoconf\";" \
  802.           "cmp exited with status $cmp_status" ;;
  803.       esac
  804.     fi
  805.   fi
  806. }
  807.  
  808. make_symlink_sane () {
  809.   # syntax: make_symlink_sane symlink target
  810.   #
  811.   # Ensure that the symbolic link symlink exists, and points to target.
  812.   #
  813.   # If symlink does not exist, create it and point it at target.
  814.   #
  815.   # If symlink exists but is not a symbolic link, back it up.
  816.   #
  817.   # If symlink exists, is a symbolic link, but points to the wrong location, fix
  818.   # it.
  819.   #
  820.   # If symlink exists, is a symbolic link, and already points to target, do
  821.   # nothing.
  822.   #
  823.   # This function wouldn't be needed if ln had an -I, --idempotent option.
  824.  
  825.   # Validate arguments.
  826.   if [ $# -ne 2 ]; then
  827.     usage_error "make_symlink_sane() called with wrong number of arguments;" \
  828.       "expected 2, got $#"
  829.     exit $SHELL_LIB_USAGE_ERROR
  830.   fi
  831.  
  832.   # We could just use the positional parameters as-is, but that makes things
  833.   # harder to follow.
  834.   local symlink target
  835.  
  836.   symlink="$1"
  837.   target="$2"
  838.  
  839.   if [ -L "$symlink" ] && [ "$(readlink "$symlink")" = "$target" ]; then
  840.       observe "link from $symlink to $target already exists"
  841.   else
  842.     observe "creating symbolic link from $symlink to $target"
  843.     mkdir -p "${target%/*}" "${symlink%/*}"
  844.     ln -s -b -S ".dpkg-old" "$target" "$symlink"
  845.   fi
  846. }
  847.  
  848. migrate_dir_to_symlink () {
  849.   # syntax: migrate_dir_to_symlink old_location new_location
  850.   #
  851.   # Per Debian Policy section 6.5.4, "A directory will never be replaced by a
  852.   # symbolic link to a directory or vice versa; instead, the existing state
  853.   # (symlink or not) will be left alone and dpkg will follow the symlink if
  854.   # there is one."
  855.   #
  856.   # We have to do it ourselves.
  857.   #
  858.   # This function moves the contents of old_location, a directory, into
  859.   # new_location, a directory, then makes old_location a symbolic link to
  860.   # new_location.
  861.   #
  862.   # old_location need not exist, but if it does, it must be a directory (or a
  863.   # symlink to a directory).  If it is not, it is backed up.  If new_location
  864.   # exists already and is not a directory, it is backed up.
  865.   #
  866.   # This function should be called from a package's preinst so that other
  867.   # packages unpacked after this one --- but before this package's postinst runs
  868.   # --- are unpacked into new_location even if their payloads contain
  869.   # old_location filespecs.
  870.  
  871.   # Validate arguments.
  872.   if [ $# -ne 2 ]; then
  873.     usage_error "migrate_dir_to_symlink() called with wrong number of"
  874.                 "arguments; expected 2, got $#"
  875.     exit $SHELL_LIB_USAGE_ERROR
  876.   fi
  877.  
  878.   # We could just use the positional parameters as-is, but that makes things
  879.   # harder to follow.
  880.   local new old
  881.  
  882.   old="$1"
  883.   new="$2"
  884.  
  885.   # Is old location a symlink?
  886.   if [ -L "$old" ]; then
  887.     # Does it already point to new location?
  888.     if [ "$(readlink "$old")" = "$new" ]; then
  889.       # Nothing to do; migration has already been done.
  890.       observe "migration of $old to $new already done"
  891.       return 0
  892.     else
  893.       # Back it up.
  894.       warn "backing up symbolic link $old as $old.dpkg-old"
  895.       mv -b "$old" "$old.dpkg-old"
  896.     fi
  897.   fi
  898.  
  899.   # Does old location exist, but is not a directory?
  900.   if [ -e "$old" ] && ! [ -d "$old" ]; then
  901.       # Back it up.
  902.       warn "backing up non-directory $old as $old.dpkg-old"
  903.       mv -b "$old" "$old.dpkg-old"
  904.   fi
  905.  
  906.   observe "migrating $old to $new"
  907.  
  908.   # Is new location a symlink?
  909.   if [ -L "$new" ]; then
  910.     # Does it point the wrong way, i.e., back to where we're migrating from?
  911.     if [ "$(readlink "$new")" = "$old" ]; then
  912.       # Get rid of it.
  913.       observe "removing symbolic link $new which points to $old"
  914.       rm "$new"
  915.     else
  916.       # Back it up.
  917.       warn "backing up symbolic link $new as $new.dpkg-old"
  918.       mv -b "$new" "$new.dpkg-old"
  919.     fi
  920.   fi
  921.  
  922.   # Does new location exist, but is not a directory?
  923.   if [ -e "$new" ] && ! [ -d "$new" ]; then
  924.     warn "backing up non-directory $new as $new.dpkg-old"
  925.     mv -b "$new" "$new.dpkg-old"
  926.   fi
  927.  
  928.   # Create new directory if it does not yet exist.
  929.   if ! [ -e "$new" ]; then
  930.     observe "creating $new"
  931.     mkdir -p "$new"
  932.   fi
  933.  
  934.   # Copy files in old location to new location.  Back up any filenames that
  935.   # already exist in the new location with the extension ".dpkg-old".
  936.   observe "copying files from $old to $new"
  937.   if ! (cd "$old" && cp -a -b -S ".dpkg-old" . "$new"); then
  938.     die "error(s) encountered while copying files from $old to $new"
  939.   fi
  940.  
  941.   # Remove files at old location.
  942.   observe "removing $old"
  943.   rm -r "$old"
  944.  
  945.   # Create symlink from old location to new location.
  946.   make_symlink_sane "$old" "$new"
  947. }
  948.  
  949. # vim:set ai et sw=2 ts=2 tw=80:
  950.  
  951. # GOBSTOPPER: The X Strike Force shell library ends here.
  952.  
  953. CONFIG_DIR=/etc/X11
  954. SERVER_SYMLINK="$CONFIG_DIR/X"
  955. CONFIG_AUX_DIR=/var/lib/x11
  956. SERVER_SYMLINK_CHECKSUM="$CONFIG_AUX_DIR/${SERVER_SYMLINK##*/}.md5sum"
  957. UNCONFIGURED_LINK_TARGET=$(which true)
  958.  
  959.  
  960.  
  961. exit 0
  962.  
  963. # vim:set ai et sts=2 sw=2 tw=0:
  964.