#!/bin/sh
#
# This program generates fonts.scale files for X font directories.  See
# mkfontdir(1x) for a description of the format of fonts.scale files.
#
# Copyright 1999--2002, 2004 Branden Robinson.
# Licensed under the GNU General Public License, version 2.  See the file
# /usr/share/common-licenses/GPL or <http://www.gnu.org/copyleft/gpl.txt>.

# $Id: update-fonts-scale 1325 2004-04-28 08:41:01Z branden $

PROGNAME=${0##*/}

# Query the terminal to establish a default number of columns to use for
# displaying messages to the user.  This is used only as a fallback in the
# event the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while
# the script is running, and this cannot, only being calculated once.)
DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
  DEFCOLUMNS=80
fi

message () {
  # pretty-print messages of arbitrary length
  echo "$*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS} >&2
}

observe () {
  if [ -n "$DEBUG" ]; then
    message "note: $*"
  fi
}

warn () {
  message "warning: $*"
}

die () {
  message "error: $*"
  exit 1
}

if [ $# -eq 0 ]; then
  die "one or more font directories must be provided"
fi

while [ -n "$1" ]; do
  # try to be clever about the arguments
  if expr "$1" : "/.*" >/dev/null 2>&1; then
    # absolute path to X font directory was provided
    XDIR=$1
    ETCDIR=/etc/X11/fonts/${XDIR##*/}
    if [ "$XDIR" = "$ETCDIR" ]; then
      # they gave us an /etc directory as the argument
      die "path to X font directory must be used"
    else
      warn "absolute path $XDIR was provided"
    fi
  else
    # assume they just gave us the basename
    XDIR="/usr/lib/X11/fonts/$1"
    ETCDIR="/etc/X11/fonts/$1"
  fi
  # confirm that the directories to be operated on exist
  for DIR in "$XDIR" "$ETCDIR"; do
    VALID=yes
    if [ ! -d "$DIR" ]; then
      warn "$DIR does not exist or is not a directory"
      VALID=
    fi
  done
  if [ -n "$VALID" ]; then
    # are there any files to process?
    if [ "$(echo $ETCDIR/*.scale)" != "$ETCDIR/*.scale" ]; then
      for SCALEFILE in "$ETCDIR"/*.scale; do
        # Only write fonts to the .scale file that actually exist, so that
        # removed-but-not-purged scalable font packages do not register
        # nonexistent fonts; this has the desirable side effect that the count
        # at the top of the file is also omitted.
        #
        # XXX: This technique will be tricked into yielding false negatives if
        # the font filename has whitespace in it.
        while read FONTFILE FONTNAME; do
          if [ -f "$XDIR/$FONTFILE" ]; then
            echo "$FONTFILE $FONTNAME" >>"$XDIR/fonts.scale.update-tmp"
          else
            observe "$SCALEFILE references nonexistent font file $FONTFILE;" \
              "skipping"
          fi
        done < $SCALEFILE
      done
      if [ -e "$XDIR/fonts.scale.update-tmp" ]; then
        # Write new scale file to .update-new in case we are interrupted.
        # Write the new count to the top of file.  Use cat and pipe to wc so wc
        # doesn't report the filename.
        cat "$XDIR/fonts.scale.update-tmp" | wc -l | tr -d '[:blank:]' \
          >"$XDIR/fonts.scale.update-new"
        cat "$XDIR/fonts.scale.update-tmp" >>"$XDIR/fonts.scale.update-new"
        mv "$XDIR/fonts.scale.update-new" "$XDIR/fonts.scale"
        rm "$XDIR/fonts.scale.update-tmp"
      fi
    else
      # no files to process, remove the one in the font dir
      rm -f "$XDIR/fonts.scale"
      # remove the font dir if it is empty
      rmdir "$XDIR" >/dev/null 2>&1 || true
    fi
  fi
  shift
done

exit 0

# vim:set ai et sts=2 sw=2 tw=80:
