| 1 | #!/bin/sh |
| 2 | |
| 3 | # $Id: update-fonts-dir 189 2005-06-11 00:04:27Z branden $ |
| 4 | |
| 5 | # This program compiles fonts.dir files for X font directories; see |
| 6 | # mkfontdir(1x) for a description of the format of fonts.dir files. |
| 7 | |
| 8 | # Copyright 1999, 2001, 2002, 2004 Branden Robinson. |
| 9 | # Licensed under the GNU General Public License, version 2. See the file |
| 10 | # /usr/share/common-licenses/GPL or <https://www.gnu.org/copyleft/gpl.txt>. |
| 11 | |
| 12 | PROGNAME=${0##*/} |
| 13 | ENCDIR=/usr/share/fonts/X11/encodings |
| 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 warning message. |
| 30 | warn () { |
| 31 | message "warning: $*" >&2 |
| 32 | } |
| 33 | |
| 34 | # Display an error message and exit. |
| 35 | die () { |
| 36 | message "fatal error: $*" >&2 |
| 37 | exit 1 |
| 38 | } |
| 39 | |
| 40 | # Display a usage message. |
| 41 | usage () { |
| 42 | if [ -n "$*" ]; then |
| 43 | message "usage error: $*" |
| 44 | fi |
| 45 | cat <<EOF |
| 46 | Usage: $PROGNAME DIRECTORY ... |
| 47 | $PROGNAME { -7 | --x11r7-layout } DIRECTORY ... |
| 48 | $PROGNAME { -h | --help } |
| 49 | This program combines X font alias information from several packages into a |
| 50 | single file that is placed in each specified X font directory DIRECTORY. This |
| 51 | utility is primarily useful to Debian package maintainer scripts. See |
| 52 | update-fonts-dir(8) for more information. |
| 53 | Options: |
| 54 | -h, --help display this usage message and exit |
| 55 | -7, --x11r7-layout use new font layout introduced with X11R7 |
| 56 | EOF |
| 57 | } |
| 58 | |
| 59 | X11R7_LAYOUT= |
| 60 | |
| 61 | # Validate arguments. |
| 62 | case "$1" in |
| 63 | -h|--help) |
| 64 | usage |
| 65 | exit 0 |
| 66 | ;; |
| 67 | -7|--x11r7-layout) |
| 68 | X11R7_LAYOUT=true |
| 69 | shift |
| 70 | ;; |
| 71 | esac |
| 72 | |
| 73 | case "$1" in |
| 74 | -*) |
| 75 | usage "unrecognized option" >&2 |
| 76 | exit 2 |
| 77 | ;; |
| 78 | esac |
| 79 | |
| 80 | |
| 81 | if [ $# -eq 0 ]; then |
| 82 | usage "one or more font directories must be specified" >&2 |
| 83 | exit 2 |
| 84 | fi |
| 85 | |
| 86 | while [ -n "$1" ]; do |
| 87 | # Try to be clever about the argument; were we given an absolute path? |
| 88 | if expr "$1" : "/.*" >/dev/null 2>&1; then |
| 89 | # Yes; an absolute path to an X font directory was provided. |
| 90 | XDIRS=$1 |
| 91 | if [ -n "$X11R7_LAYOUT" ]; then |
| 92 | ETCDIR=/etc/X11/fonts/X11R7/${XDIRS##*/} |
| 93 | else |
| 94 | ETCDIR=/etc/X11/fonts/${XDIRS##*/} |
| 95 | fi |
| 96 | if [ "$XDIRS" = "$ETCDIR" ]; then |
| 97 | # We were given an /etc directory as an argument. |
| 98 | die "path to X font directory must be used" |
| 99 | else |
| 100 | warn "absolute path $XDIRS was provided" |
| 101 | fi |
| 102 | else |
| 103 | # No; a relative path was provided -- assume we were given just the |
| 104 | # basename. |
| 105 | XDIRS="/usr/share/fonts/X11/$1" |
| 106 | fi |
| 107 | # Confirm that the directories to be operated on exist. |
| 108 | for XDIR in $XDIRS; do |
| 109 | VALID=yes |
| 110 | if ! [ -d "$XDIR" ]; then |
| 111 | warn "$XDIR does not exist or is not a directory" |
| 112 | VALID= |
| 113 | fi |
| 114 | if [ -n "$VALID" ]; then |
| 115 | # Use encoding directories if they are available. |
| 116 | if [ -d "$ENCDIR" ] && [ -d "$ENCDIR/large" ]; then |
| 117 | mkfontdir -e "$ENCDIR" -e "$ENCDIR/large" "$XDIR" |
| 118 | else |
| 119 | mkfontdir "$XDIR" |
| 120 | fi |
| 121 | # Are there any fonts in the font directory? |
| 122 | if [ "$(head -n 1 "$XDIR/fonts.dir")" = "0" ]; then |
| 123 | # There are no files to process; remove any generated files already |
| 124 | # in the font directory. |
| 125 | rm -f "$XDIR/fonts.dir" "$XDIR/encodings.dir" |
| 126 | # Remove the font dirextory if it is empty. |
| 127 | rmdir "$XDIR" >/dev/null 2>&1 || true |
| 128 | fi |
| 129 | fi |
| 130 | done |
| 131 | shift |
| 132 | done |
| 133 | |
| 134 | exit 0 |
| 135 | |
| 136 | # vim:set ai et sts=4 sw=4 tw=80: |