#!/bin/sh
set -e

if [ "$1" = "configure" ]; then
	. /usr/share/debconf/confmodule
	db_version 2.0

	package=ttf-commercial
	targetdir="/usr/share/fonts/truetype/commercial"
	x11ttfdir="/usr/X11R6/lib/X11/fonts/TrueType"
	fonts_scale_orig="${targetdir}/${package}.scale"
	fonts_scale="/etc/X11/fonts/TrueType/${package}.scale"
	fonts_scale_tmp="/etc/X11/fonts/TrueType/${package}.scale.temp"
	xf86config="/etc/X11/XF86Config-4"
	xf86config_tmp="/etc/X11/XF86Config-4.${package}.temp"
	db_get ${package}/font_source_dir && sourcedirlist=${RET}
	test "${sourcedirlist}" || exit 0
	db_get ${package}/copy_than_link && copy_than_link=${RET}
	db_get ${package}/use_xtt_module && use_xtt_module=${RET}
	db_get ${package}/add_fontpath && add_fontpath=${RET}

	LANG=C	# this script is a lot faster in C locale.

	# Copy or Link.
	if [ "${copy_than_link}" = "true" ]; then
		echo -n "Starting to copy fonts: "
	else
		echo -n "Starting to link fonts: "
	fi
	for sourcedir in ${sourcedirlist}
	do
		for source in $(ls ${sourcedir} 2> /dev/null)
		do
			target=$(echo ${source} | tr '[:upper:]' '[:lower:]')
			if grep -q ${target} ${fonts_scale_orig}; then
				if [ "${copy_than_link}" = "true" ]; then
					test -L ${targetdir}/${target} && rm ${targetdir}/${target}
					if [ ! -f "${targetdir}/${target}" ]; then
						cp -f ${sourcedir}/${source} ${targetdir}/${target} 2> /dev/null || true
						chmod -f 644 ${targetdir}/${target}
					fi
				else
					if [ ! -L "${targetdir}/${target}" ]; then
						ln -sf ${sourcedir}/${source} ${targetdir}/${target} 2> /dev/null || true
					fi
				fi
				test -L ${x11ttfdir}/${target} || ln -sf ${targetdir}/${target} ${x11ttfdir}/${target}
				echo -n "."
			fi
		done
	done
	echo " Done."

	# Generate fonts.scale file.
	echo -n "Generating \`${package}.scale' file: "
	for file in $(ls ${targetdir})
	do
		echo -n "."
		if [ "${use_xtt_module}" = "true" ]; then
			grep ${file} ${fonts_scale_orig} >> ${fonts_scale_tmp} || true
		else
			grep ${file} ${fonts_scale_orig} | grep -v ":" >> ${fonts_scale_tmp} || true
		fi
	done
	sort -u ${fonts_scale_tmp} | wc -l | sed 's/^ *//' > ${fonts_scale}
	sort -u ${fonts_scale_tmp} >> ${fonts_scale}
	chmod -f 644 ${fonts_scale}
	rm -f ${fonts_scale_tmp}
	echo " Done."

	# Modify XF86Config-4 file.
	if [ -f "${xf86config}" ]; then
		echo -n "Modifying \`${xf86config}' file: "

		if [ "${use_xtt_module}" = "true" ]; then
			if !(grep -q "^[ 	]*Load[ 	][ 	]*[\"']*xtt[\"']*" ${xf86config}); then
				sed -e "/^[^#]*Section[ 	]*[\"']Module[\"']/{G;s/$/	Load	\"xtt\"/;}" \
					-e "/^#\([^#]*Load.*xtt.*\)$/d" \
					-e "s/^\([^#]*Load.*freetype.*\)$/#\1/" ${xf86config} > ${xf86config_tmp}
				mv ${xf86config_tmp} ${xf86config}
				MODIFIED=true
			fi
		else
			if !(grep -q "^[ 	]*Load[ 	][ 	]*[\"']*freetype[\"']*" ${xf86config}); then
				sed -e "/^[^#]*Section[ 	]*[\"']Module[\"']/{G;s/$/	Load	\"freetype\"/;}" \
					-e "/^#\([^#]*Load.*freetype.*\)$/d" \
					-e "s/^\([^#]*Load.*xtt.*\)$/#\1/" ${xf86config} > ${xf86config_tmp}
				mv ${xf86config_tmp} ${xf86config}
				MODIFIED=true
			fi
		fi
		if [ "${add_fontpath}" = "true" ]; then
			if !(grep -q "^[ 	]*FontPath[ 	][ 	]*[\"']*$(echo ${x11ttfdir} | sed 's,X11R6/,.*,')\/*[\"']*" ${xf86config})
			then
				sed -e "/^[^#]*Section[ 	]*[\"']Files[\"']/{G;s,$,	FontPath	\"${x11ttfdir}\",;}" ${xf86config} > ${xf86config_tmp}
				mv ${xf86config_tmp} ${xf86config}
				MODIFIED=true
			fi
		fi

		if [ "$MODIFIED" = "true" ]; then
			echo "Done."
		else
			echo "Already modified."
		fi
	fi

	# update-fonts-scale and mkfontdir.
	echo -n "Running update-fonts-dir in TrueType font directory: "
	update-fonts-scale ${x11ttfdir##*/}
	mkfontdir ${x11ttfdir}
	echo "Done."
fi

#DEBHELPER#
