#!/bin/sh
#
# xfs:       Starts the X Font Server
#
# Version:      @(#) /etc/init.d/xfs 2.0
#
# 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/init.d/functions

umask 133

prog=xfs

# Make sure that xfs has "/" as the CWD
cd /

buildfontlist() {
   pushd . &> /dev/null
   for d in $(/usr/sbin/chkfontpath --list | cut -f 2 -d ':') ;do
      if [ -d "$d" ]; then
         cd $d
         # Check if we need to rerun mkfontdir
         NEEDED=no
         if ! [ -e fonts.dir ]; then
            NEEDED=yes
         elif [ "$(find . -type f -cnewer fonts.dir 2>/dev/null)" != "" ];then
            NEEDED=yes
         fi
         if [ "$NEEDED" = "yes" ]; then
            rm -f fonts.dir &>/dev/null
            if ls | grep -i "\.tt[cf]$" &>/dev/null; then
               # TrueType fonts found...
               ttmkfdir -d . -o fonts.scale
               mkfontdir . &>/dev/null
               [ -e fonts.dir ] && chmod 644 fonts.scale fonts.dir
            fi
            if [ "$(ls |egrep -iv '\.tt[cf]$|^fonts\.|^encodings\.')" != "" ]; then
               # This directory contains fonts that are not TrueType...
               mkfontdir . &>/dev/null
               [ -e fonts.dir ] && chmod 644 fonts.dir
            fi
         fi
      fi
   done
   popd &> /dev/null
}

start() {
   if [ -L /usr/X11R6/bin/X ]; then
      echo -n $"Starting $prog: "
      [ -x /usr/sbin/chkfontpath ] && buildfontlist
      rm -fr /tmp/.font-unix
      daemon xfs -droppriv -daemon
      ret=$?
      [ $ret -eq 0 ] && touch /var/lock/subsys/xfs
      echo
      return $ret
   fi
}	

stop() {
   echo -n $"Shutting down $prog: "
   killproc xfs
   ret=$?
   [ $ret -eq 0 ] && rm -f /var/lock/subsys/xfs
   echo
   return $ret
}	

rhstatus() {
   status xfs
}	

reload() {
   if [ -f /var/lock/subsys/xfs ]; then
      echo -n $"Reloading $prog: "
      [ -x /usr/sbin/chkfontpath ] && buildfontlist
      killproc xfs -USR1
      ret=$?
      echo
      return $ret
   else
      stop
      start
   fi
}

restart() {
   echo $"Restarting $prog:"
   stop
   start
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
    restart
    ;;
  reload)
  	reload
	;;
  condrestart)
  	[ -f /var/lock/subsys/xfs ] && reload || :
	;;
  status)
  	rhstatus
	;;
  *)
	echo $"*** Usage: $prog {start|stop|status|restart|reload|condrestart}"
	exit 1
esac

exit $?
