#!/bin/sh

#    This is register_fonts. It runs fc-cache and generates fonts.dir
#    and fonts.scale files in the appropriate places.
#    Copyright (C) 2006  Florian Franzmann
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

. /etc/sorcery/config

if spell_ok xfs; then
	XFSCONFIG="${INSTALL_ROOT}/usr/lib/X11/fs/config"
else
	XFSCONFIG="${INSTALL_ROOT}/etc/X11/fs/config"
fi
ENCODING_PATH0="${INSTALL_ROOT}/usr/lib/X11/fonts/encodings"
ENCODING_PATH1="${INSTALL_ROOT}/usr/lib/X11/fonts/encodings/large"
if spell_ok xorg || spell_ok mkfontdir; 
then
	X11CONFIG="${INSTALL_ROOT}/etc/X11/xorg.conf"
elif spell_ok xfree86 || spell_ok xfree86-4.3 || spell_ok xfree86-devel;
then
	X11CONFIG="${INSTALL_ROOT}/etc/X11/XF86Config"
fi

if spell_ok fontconfig; then
	message -n "$MESSAGE_COLOR"
	message -n "running fontconfig's fc-cache..."
	fc-cache -s
	message "done.$DEFAULT_COLOR"
fi

if spell_ok xorg \
	|| spell_ok mkfontdir \
	|| spell_ok xfree86 \
	|| spell_ok xfree-4.3 \
	|| spell_ok xfree86-devel; 
then
	message -n "$MESSAGE_COLOR"
	message -n "parsing $X11CONFIG for font paths..."
	X_FONT_PATHS="$(cat $X11CONFIG \
		    | grep '^[	 ]*FontPath[	 ]*"[^unix\|tcp].*"' \
		    | sed -e 's/[	 ]*FontPath[	 ]*"\(.*\)"/\1/')"
	message "done.$DEFAULT_COLOR"
	message -n "$MESSAGE_COLOR"
	if [ -r $XFSCONFIG ]; then
		message -n "parsing $XFSCONFIG for font paths..."
		XFS_FONT_PATHS="`exec < $XFSCONFIG
			set -f
			while read -r REPLY; do
			for LOOKAHEAD in $REPLY; do
				case $LOOKAHEAD in
					\#*)
						break
					;;
					?*=*)
						KEY=${LOOKAHEAD%%=*}
					;;
					=*)
						KEY=$TOKEN
					;;
					*)
						if [ "$KEY" = catalogue ]; then
							ANS="$ANS$TOKEN"
						fi
					;;
					esac
					TOKEN=${LOOKAHEAD#*=}
				done
			done
		if [ "$KEY" = catalogue ]; then
			ANS="$ANS$TOKEN"
		fi
		echo $ANS`"
		XFS_FONT_PATHS="$(echo $XFS_FONT_PATHS | tr ',' ' ')"
		message "done.$DEFAULT_COLOR"
	else
		message -n "$MESSAGE_COLOR"
		message -n "$XFSCONFIG not found. It seems you are not using xfs."
		message "$DEFAULT_COLOR"
	fi
	FONT_PATHS="$X_FONT_PATHS $XFS_FONT_PATHS"
	message -n "$MESSAGE_COLOR"                               &&
	message -n "generating indices for"                       &&
	message "$DEFAULT_COLOR"
	for path in $FONT_PATHS; do
		if [ -d "$path" ]; then
			message -n "$MESSAGE_COLOR"               &&
			message -n "$FILE_COLOR\t$path"           &&
			message "$DEFAULT_COLOR"                  &&
			mkfontscale "$path" 2>/dev/null           &&
			mkcfm       "$path" 2>/dev/null
			if spell_ok xorg; then
				mkfontdir -e "$ENCODING_PATH0" \
					  -e "$ENCODING_PATH1" \
					  "$path"
			else
				mkfontdir "$path"
			fi
		fi
	done
	message -n "$MESSAGE_COLOR"                               &&
	message -n "done."                                        &&
	message "$DEFAULT_COLOR"
fi
