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 / sbin / update-fonts-scale < prev    next >
Encoding:
Text File  |  2006-08-02  |  6.3 KB  |  202 lines

  1. #!/bin/sh
  2.  
  3. # $Id: update-fonts-scale 189 2005-06-11 00:04:27Z branden $
  4.  
  5. # This program generates fonts.scale files for X font directories; see
  6. # mkfontdir(1x) for a description of the format of fonts.scale files.
  7.  
  8. # Copyright 1999-2002, 2004 Branden Robinson.
  9. # Copyright 2006 Steve Langasek.
  10. # Licensed under the GNU General Public License, version 2.  See the file
  11. # /usr/share/common-licenses/GPL or <http://www.gnu.org/copyleft/gpl.txt>.
  12.  
  13. PROGNAME=${0##*/}
  14.  
  15. # Query the terminal to establish a default number of columns to use for
  16. # displaying messages to the user.  This is used only as a fallback in the event
  17. # the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while the
  18. # script is running, and this cannot, only being calculated once.)
  19. DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
  20. if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
  21.     DEFCOLUMNS=80
  22. fi
  23.  
  24. # Display a message, wrapping lines at the terminal width.
  25. message () {
  26.     echo "$*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
  27. }
  28.  
  29. # Display a debugging message.
  30. trace () {
  31.     if [ -n "$DEBUG" ]; then
  32.         message "note: $*" >&2
  33.     fi
  34. }
  35.  
  36. # Display a warning message.
  37. warn () {
  38.     message "warning: $*" >&2
  39. }
  40.  
  41. # Display an error message and exit.
  42. die () {
  43.     message "fatal error: $*" >&2
  44.     exit 1
  45. }
  46.  
  47. # Display a usage message.
  48. usage () {
  49.     if [ -n "$*" ]; then
  50.         message "usage error: $*"
  51.     fi
  52.     cat <<EOF
  53. Usage: $PROGNAME DIRECTORY ...
  54.        $PROGNAME { -h | --help }
  55. This program combines scalable X font information from several packages into a
  56. single file that is placed in each specified X font directory DIRECTORY.  This
  57. utility is primarily useful to Debian package maintainer scripts.  See
  58. update-fonts-scale(8) for more information.
  59. Options:
  60.     -h, --help                        display this usage message and exit
  61. EOF
  62. }
  63.  
  64. X11R7_LAYOUT=
  65.  
  66. # Validate arguments.
  67. case "$1" in
  68.     -h|--help)
  69.         usage
  70.         exit 0
  71.         ;;
  72.     -7|--x11r7-layout)
  73.         X11R7_LAYOUT=true
  74.         shift
  75.         ;;
  76. esac
  77.  
  78. case "$1" in
  79.     -*)
  80.         usage "unrecognized option" >&2
  81.         exit 2
  82.         ;;
  83. esac
  84.  
  85. if [ $# -eq 0 ]; then
  86.     usage "one or more font directories must be specified" >&2
  87.     exit 2
  88. fi
  89.  
  90. while [ -n "$1" ]; do
  91.     # Try to be clever about the argument; were we given an absolute path?
  92.     if expr "$1" : "/.*" >/dev/null 2>&1; then
  93.         # Yes; an absolute path to an X font directory was provided.
  94.         X11R7DIR=$1
  95.         ETCDIR=/etc/X11/fonts/${X11R7DIR##*/}
  96.         ETC7DIR=/etc/X11/fonts/X11R7/${X11R7DIR##*/}
  97.         if [ "$X11R7DIR" = "$ETCDIR" ] || [ "$X11R7DIR" = "$ETC7DIR" ]; then
  98.             # We were given an /etc directory as an argument.
  99.             die "path to X font directory must be used"
  100.         else
  101.             warn "absolute path $X11R7DIR was provided"
  102.         fi
  103.     else
  104.         # No; a relative path was provided -- assume we were given just the
  105.         # basename.
  106.         X11R6DIR=/usr/lib/X11/fonts/$1
  107.         X11R7DIR=/usr/share/fonts/X11/$1
  108.         ETCDIR=/etc/X11/fonts/$1
  109.         ETC7DIR=/etc/X11/fonts/X11R7/$1
  110.     fi
  111.  
  112.     shift
  113.  
  114.     # Confirm that the directories to be operated on exist.
  115.     VALIDSRC=
  116.     if [ -d "$ETCDIR" ]; then
  117.         VALIDSRC=yes
  118.     else
  119.         warn "$ETCDIR does not exist or is not a directory"
  120.     fi
  121.     if [ -d "$ETC7DIR" ]; then
  122.         VALIDSRC=yes
  123.     else
  124.         if [ -n "$X11R7_LAYOUT" ]; then
  125.             warn "$ETC7DIR does not exist or is not a directory"
  126.         fi
  127.     fi
  128.  
  129.     VALIDDEST=
  130.     for DIR in "$X11R7DIR" "$X11R6DIR"; do
  131.         [ -n "$DIR" ] || continue
  132.         if [ -d "$DIR" ]; then
  133.             VALIDDEST=yes
  134.         else
  135.             warn "$DIR does not exist or is not a directory"
  136.         fi
  137.     done
  138.  
  139.     if [ -z "$VALIDSRC" ] || [ -z "$VALIDDEST" ]; then
  140.         continue
  141.     fi
  142.  
  143.     # Are there any files to process?
  144.     if [ "$(echo "$ETCDIR"/*.scale "$ETC7DIR"/*.scale)" != "$ETCDIR/*.scale $ETC7DIR/*.scale" ]
  145.     then
  146.         for XDIR in "$X11R7DIR" "$X11R6DIR"; do
  147.             if [ -z "$XDIR" ] || ! [ -d "$XDIR" ]; then
  148.                 continue
  149.             fi
  150.             for SCALEFILE in "$ETCDIR"/*.scale "$ETC7DIR"/*.scale; do
  151.                 [ -e "$SCALEFILE" ] || continue
  152.                 # Only write fonts to the .scale file that actually exist, so
  153.                 # that removed-but-not-purged scalable font packages do not
  154.                 # register nonexistent fonts; this has the desirable side effect
  155.                 # that the count at the top of the file is also omitted.
  156.                 #
  157.                 # XXX: This technique will be tricked into yielding false
  158.                 # negatives if the font filename has whitespace in it.
  159.                 while read FONTFILE FONTNAME; do
  160.                     if [ -f "$XDIR/$FONTFILE" ]; then
  161.                         echo "$FONTFILE $FONTNAME" \
  162.                           >>"$XDIR/fonts.scale.update-tmp"
  163.                     else
  164.                         trace "$SCALEFILE references nonexistent font file" \
  165.                           "$FONTFILE; skipping"
  166.                     fi
  167.                 done <"$SCALEFILE"
  168.             done
  169.             if [ -e "$XDIR/fonts.scale.update-tmp" ]; then
  170.                 # Write the new scale file to a temporary location in case we
  171.                 # are interrupted.  Write the new count to the top of file.  Use
  172.                 # cat and pipe to wc so wc doesn't report the filename.
  173.                 cat "$XDIR/fonts.scale.update-tmp" | wc -l | tr -d '[:blank:]' \
  174.                   >"$XDIR/fonts.scale.update-new"
  175.                 cat "$XDIR/fonts.scale.update-tmp" \
  176.                   >>"$XDIR/fonts.scale.update-new"
  177.                 mv "$XDIR/fonts.scale.update-new" "$XDIR/fonts.scale"
  178.                 rm "$XDIR/fonts.scale.update-tmp"
  179.             else
  180.                 # No font in the processed *.scale files was in the current
  181.                 # directory, so remove fonts.scale.
  182.                 rm -f "$XDIR/fonts.scale"
  183.             fi
  184.         done
  185.     else
  186.         for XDIR in "$X11R7DIR" "$X11R6DIR"; do
  187.             if [ -z "$XDIR" ] || ! [ -d "$XDIR" ]; then
  188.                 continue
  189.             fi
  190.             # No files to process; remove any fonts.scale file already in the
  191.             # font directory.
  192.             rm -f "$XDIR/fonts.scale"
  193.             # Remove the font directory if it is empty.
  194.             rmdir "$XDIR" >/dev/null 2>&1 || true
  195.         done
  196.     fi
  197. done
  198.  
  199. exit 0
  200.  
  201. # vim:set ai et sts=4 sw=4 tw=80:
  202.