#!/bin/sh
# update-fonts-ttcap
# compiles fonts.dir files with TTCap (X-TT entention) for X font directories
# see documents in /usr/share/doc/xfs-xtt/X-TT/ for a description of
# the format of fonts.dir files with TTCap
# Original Copyright 1999 Branden Robinson.
# It is based on update-fonts-scale. Modified by ISHIKAWA Mutsumi.
# 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>.

if [ $# -eq 0 ]; then
  echo "$0: one or more font directories must be provided."
  exit 1
fi

while [ "$1" ]; do
  # try to be clever about the arguments
  if [ "$(echo $1 | cut -c1)" = "/" ]; then
    # absolute path to X font directory was provided
    XDIR=$1
    ETCDIR=/etc/X11/fonts/$(basename $XDIR)
    if [ "$XDIR" = "$ETCDIR" ]; then
      # they gave us an /etc directory as the argument
      echo "$0: path to X font directory must be used."
      exit 1
    fi
  else
    # assume they just gave us the basename
    XDIR=/usr/lib/X11/fonts/$1
    ETCDIR=/etc/X11/fonts/$1
  fi
  for dir in $XDIR $ETCDIR; do
    valid=yes
    if [ ! -d $dir ]; then
      echo "$0: $dir does not exist or is not a directory."
      valid=
    fi
  done
  if [ "$valid" ]; then
    if [ -e $XDIR/fonts.dir ]; then
      # backup old fonts.scale file in case we are interrupted
      cp $XDIR/fonts.dir $XDIR/fonts.dir.update-backup
    fi
    # are there any files to process?
    if [ "$(echo $ETCDIR/*.ttcap)" != "$ETCDIR/*.ttcap" ]; then
      for file in $ETCDIR/*.ttcap; do
        tail +2 $file >> $XDIR/fonts.dir.update-tmp
      done
      cat $XDIR/fonts.dir.update-tmp | wc -l | tr -d '[:blank:]' > $XDIR/fonts.dir
      cat $XDIR/fonts.dir.update-tmp >> $XDIR/fonts.dir
      rm $XDIR/fonts.dir.update-tmp
    fi
  fi
  shift
done

if [ -e $XDIR/fonts.dir.update-backup ]; then
  # we are done, remove the backup file
  rm $XDIR/fonts.dir.update-backup
fi

exit
