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