#!/bin/sh
# update-fonts-alias
# compiles fonts.alias files for X font directories
# see mkfontdir(1) for a description of the format of fonts.alias files
# Copyright 1999 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>.

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.alias ]; then
      # backup old fonts.alias file in case we are interrupted
      mv $XDIR/fonts.alias $XDIR/fonts.alias.update-backup
    fi
    cat > $XDIR/fonts.alias << EOF
!! fonts.alias -- automatically generated file.  DO NOT EDIT.
!! To modify, see update-fonts-alias(8).
EOF
    # are there any files to process?
    if [ "$(echo $ETCDIR/*.alias)" != "$ETCDIR/*.alias" ]; then
      for file in $ETCDIR/*.alias; do
        echo "!! $file" >> $XDIR/fonts.alias
        cat $file >> $XDIR/fonts.alias
      done
    fi
  fi
  shift
done

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

exit
