#!/bin/bash
#
# $Id:$
#
# 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.
export LANG=C
. /etc/init.d/functions
# Set umask to ensure fonts.dir and similar files get created mode 0644
umask 133

prog=xfs

FC_CACHE=/usr/bin/fc-cache

buildfontlist() {
   pushd . &> /dev/null
   # chkfontpath output filtering, strips all of the junk output by
   # chkfontpath that we do not want, including headers, FPE numbers and
   # whitespace and other junk.  Also filters out FPE's with trailing
   # modifiers such as ":unscaled" et al.
   for dir in $(/usr/sbin/chkfontpath --list | sed -e '/^Current/d;s#^[0-9]*: ##g;s#^/.*:[a-z]*$##g;/^[[:space:]]*$/d' | sort | uniq) ;do
      if [ -d "$dir" ]; then
         cd "$dir"
         # If fonts.dir does not exist, or if there are files in the
         # directory with a newer change time, regenerate fonts.dir, etc.
         # Using "-cnewer" here fixes bug #53737
         if [ ! -e fonts.dir ] || [ -n "$(find . -maxdepth 1 -type f -cnewer fonts.dir -not -name 'fonts.cache*')" ]; then
            rm -f fonts.dir
            if ls | grep -iqs '\.ot[cf]$' ; then
               # Opentype fonts found, generate fonts.scale and fonts.dir
               mkfontscale . && mkfontdir . &>/dev/null
            elif ls | grep -iqs '\.tt[cf]$' ; then
               # TrueType fonts found, generate fonts.scale and fonts.dir
               ttmkfdir -d . -o fonts.scale && mkfontdir . &>/dev/null
            elif ls | grep -Eiqsv '(^fonts\.(scale|alias|cache.*)$|.+(\.[ot]t[cf]|dir)$)' ; then
               # This directory contains non-TrueType/non-Opentype fonts
               mkfontdir . &>/dev/null
            fi
         fi
      fi
   done
   # Now we run fc-cache, assuming fonts may have been added, without
   # explicitly checking, as it is rather fast anyway.  Some older versions
   # of fc-cache will SEGV, which is prevented by invoking it with HOME=/
   [ -x "$FC_CACHE" ] && HOME=/ "$FC_CACHE"
   popd &> /dev/null
}

start() {
   FONT_UNIX_DIR=/tmp/.font-unix
   echo -n $"Starting $prog: "
   [ -x /usr/sbin/chkfontpath ] && buildfontlist
   # Clean out .font-unix dir, and recreate it with the proper ownership and
   # permissions.
   # Make sure .font-unix dir, exists.
   if [ ! -d $FONT_UNIX_DIR ]
   then
       mkdir -m 1777 $FONT_UNIX_DIR || :
       restorecon $FONT_UNIX_DIR 2>/dev/null || :
   fi

   daemon xfs -droppriv -daemon
   ret=$?
   [ $ret -eq 0 ] && touch /var/lock/subsys/xfs
   echo
   return $ret
}	

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 $?
