| 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 <https://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 | X11R7DIR=/usr/share/fonts/X11/$1 |
| 107 | ETCDIR=/etc/X11/fonts/$1 |
| 108 | ETC7DIR=/etc/X11/fonts/X11R7/$1 |
| 109 | fi |
| 110 | |
| 111 | shift |
| 112 | |
| 113 | # Confirm that the directories to be operated on exist. |
| 114 | VALIDSRC= |
| 115 | if [ -d "$ETCDIR" ]; then |
| 116 | VALIDSRC=yes |
| 117 | else |
| 118 | warn "$ETCDIR does not exist or is not a directory" |
| 119 | fi |
| 120 | if [ -d "$ETC7DIR" ]; then |
| 121 | VALIDSRC=yes |
| 122 | else |
| 123 | if [ -n "$X11R7_LAYOUT" ]; then |
| 124 | warn "$ETC7DIR does not exist or is not a directory" |
| 125 | fi |
| 126 | fi |
| 127 | |
| 128 | VALIDDEST= |
| 129 | if [ -d "$X11R7DIR" ]; then |
| 130 | VALIDDEST=yes |
| 131 | else |
| 132 | warn "$X11R7DIR does not exist or is not a directory" |
| 133 | fi |
| 134 | |
| 135 | if [ -z "$VALIDSRC" ] || [ -z "$VALIDDEST" ]; then |
| 136 | continue |
| 137 | fi |
| 138 | |
| 139 | # Are there any files to process? |
| 140 | if [ "$(echo "$ETCDIR"/*.scale "$ETC7DIR"/*.scale)" != "$ETCDIR/*.scale $ETC7DIR/*.scale" ] |
| 141 | then |
| 142 | if [ -n "$X11R7DIR" ] && [ -d "$X11R7DIR" ]; then |
| 143 | for SCALEFILE in "$ETCDIR"/*.scale "$ETC7DIR"/*.scale; do |
| 144 | [ -e "$SCALEFILE" ] || continue |
| 145 | # Only write fonts to the .scale file that actually exist, so |
| 146 | # that removed-but-not-purged scalable font packages do not |
| 147 | # register nonexistent fonts; this has the desirable side effect |
| 148 | # that the count at the top of the file is also omitted. |
| 149 | # |
| 150 | # XXX: This technique will be tricked into yielding false |
| 151 | # negatives if the font filename has whitespace in it. |
| 152 | while read FONTFILE FONTNAME; do |
| 153 | if [ -f "$X11R7DIR/$FONTFILE" ]; then |
| 154 | echo "$FONTFILE $FONTNAME" \ |
| 155 | >>"$X11R7DIR/fonts.scale.update-tmp" |
| 156 | else |
| 157 | trace "$SCALEFILE references nonexistent font file" \ |
| 158 | "$FONTFILE; skipping" |
| 159 | fi |
| 160 | done <"$SCALEFILE" |
| 161 | done |
| 162 | if [ -e "$X11R7DIR/fonts.scale.update-tmp" ]; then |
| 163 | # Write the new scale file to a temporary location in case we |
| 164 | # are interrupted. Write the new count to the top of file. Use |
| 165 | # cat and pipe to wc so wc doesn't report the filename. |
| 166 | cat "$X11R7DIR/fonts.scale.update-tmp" \ |
| 167 | | wc -l | tr -d '[:blank:]' \ |
| 168 | >"$X11R7DIR/fonts.scale.update-new" |
| 169 | cat "$X11R7DIR/fonts.scale.update-tmp" \ |
| 170 | >>"$X11R7DIR/fonts.scale.update-new" |
| 171 | mv "$X11R7DIR/fonts.scale.update-new" "$X11R7DIR/fonts.scale" |
| 172 | rm "$X11R7DIR/fonts.scale.update-tmp" |
| 173 | else |
| 174 | # No font in the processed *.scale files was in the current |
| 175 | # directory, so remove fonts.scale. |
| 176 | rm -f "$X11R7DIR/fonts.scale" |
| 177 | fi |
| 178 | fi |
| 179 | else |
| 180 | if [ -n "$X11R7DIR" ] && [ -d "$X11R7DIR" ]; then |
| 181 | # No files to process; remove any fonts.scale file already in the |
| 182 | # font directory. |
| 183 | rm -f "$X11R7DIR/fonts.scale" |
| 184 | # Remove the font directory if it is empty. |
| 185 | rmdir "$X11R7DIR" >/dev/null 2>&1 || true |
| 186 | fi |
| 187 | fi |
| 188 | done |
| 189 | |
| 190 | exit 0 |
| 191 | |
| 192 | # vim:set ai et sts=4 sw=4 tw=80: |