#!/bin/bash
#
# 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

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 ':' | sort | uniq) ;do
      if [ -d "$d" ]; then
         cd "$d"
         # Check if we need to rerun mkfontdir
         REGEN_FONTS_DIR=no
         if ! [ -e fonts.dir ]; then
            REGEN_FONTS_DIR=yes
         elif [ -n "$(find . -type f -cnewer fonts.dir -not -name 'fonts.cache*')" ];then
            REGEN_FONTS_DIR=yes
         fi
         if [ "$REGEN_FONTS_DIR" = "yes" ]; then
            rm -f fonts.dir 
            if ls | grep -iqs '\.tt[cf]$' ; then
               # TrueType fonts found, generate fonts.scale and fonts.dir
               ttmkfdir -d . -o fonts.scale
               mkfontdir . &>/dev/null
            fi
            if ls | grep -iqs '\.ot[cf]$' ; then
               # Opentype fonts found, generate fonts.scale and fonts.dir
               mkfontscale .
               mkfontdir . &>/dev/null
            fi
            if 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
   # Recreating fonts.dir files above may result in stale fonts.cache-1
   # files since the directory mtimes change, so we run fc-cache to
   # update any necessary fonts.cache files.
   FC_CACHE=/usr/bin/fc-cache
   if [ "$REGEN_FONTS_DIR" = "yes" -a -x "$FC_CACHE" ] ; then
      # fc-cache 1.0.2 as included in Red Hat Linux 8.0 will SEGV when executed
      # by this initscript, so we don't run fc-cache for version 1.0.2 since
      # even if fc-cache were fixed, we can't guarantee the user would have the
      # fixed version installed.  Yes, this is an ugly, but necessary hack for
      # the time being.
      #[ $FC_CACHE --version 2>&1 | grep -q '1\.0\.2' ] ||
      HOME=/ "$FC_CACHE"
   fi
   popd &> /dev/null
}

start() {
      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
}	

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