#!/bin/sh
#
# xfs:       Starts the X Font Server
#
# Version:      @(#) /etc/rc.d/init.d/xfs 1.6
#
# chkconfig: 2345 90 10
# description: Starts and stops the X Font Server at boot time and shutdown.
#              It also takes care of (re-)generating font lists.
#
# processname: xfs
# config: /etc/X11/fs/config
# hide: true

# Source function library.
. /etc/rc.d/init.d/functions

buildfontlist() {
	for d in /usr/X11R6/lib/X11/fonts/* /usr/X11R6/lib/X11/fonts/*/* /usr/share/fonts/* /usr/share/fonts/*/*; do
		if [ -d $d ]; then
			cd $d
			# Check if we need to rerun mkfontdir
			NEEDED=no
			if ! test -e fonts.dir; then
				NEEDED=yes
			elif test "x`find . -newer fonts.dir 2>/dev/null`" != x; then
				NEEDED=yes
			fi
			if test $NEEDED = yes; then
				rm -f fonts.dir &>/dev/null
				if test "x`ls |egrep --ignore-case -v '\.ttf$|^fonts\.'`" != x; then
					# This directory contains fonts that are not TrueType...

					mkfontdir -e /usr/X11R6/lib/X11/fonts/encodings \
						  -e /usr/X11R6/lib/X11/fonts/encodings/large . &>/dev/null
				elif ls |grep \.ttf$ &>/dev/null; then
					# TrueType fonts found...
					ttmkfdir . >fonts.scale
					mkfontdir -e /usr/X11R6/lib/X11/fonts/encodings \
						  -e /usr/X11R6/lib/X11/fonts/encodings/large . &>/dev/null
				fi
			fi
		fi
	done
}

# See how we were called.
case "$1" in
  start)
	echo -n "Starting X Font Server: "
	buildfontlist
	rm -fr /tmp/.font-unix
	daemon xfs -droppriv -daemon -port -1
	touch /var/lock/subsys/xfs
	echo
	;;
  stop)
	echo -n "Shutting down X Font Server: "
	killproc xfs
	rm -f /var/lock/subsys/xfs
	echo
	;;
  status)
	status xfs
	;;
  restart)
	echo -n "Restarting X Font Server. "
	buildfontlist
	if [ -f /var/lock/subsys/xfs ]; then
	    killproc xfs -USR1
	else
	    rm -fr /tmp/.font-unix
	    daemon xfs -droppriv -daemon -port -1
	    touch /var/lock/subsys/xfs
	fi
	echo
	;;
  *)
	echo "*** Usage: xfs {start|stop|status|restart}"
	exit 1
esac

exit 0
