#!/bin/sh

set -e

. /usr/share/debconf/confmodule

LOCALCONF=/var/lib/fontconfig/local.conf
rm -f $LOCALCONF

CONFDIR=/etc/fonts/conf.d

db_get fontconfig/hinting_type
hinting_type="$RET"

hint_prio="10-debconf-"
unhinted="unhinted.conf"
autohint="autohint.conf"

if [ -h $CONFDIR/$hint_prio$unhinted ]; then
	rm $CONFDIR/$hint_prio$unhinted
fi
if [ -h $CONFDIR/$hint_prio$autohint ]; then
	rm $CONFDIR/$hint_prio$autohint
fi
	
case "$hinting_type" in
"Native")
	;;
"Autohinter")
	ln -s $CONFDIR/$autohint $CONFDIR/$hint_prio$autohint
	;;
"None")
	ln -s $CONFDIR/$unhinted $CONFDIR/$hint_prio$unhinted
	;;
esac

db_get fontconfig/subpixel_rendering
subpixel_rendering="$RET"

subpixel_prio="20-debconf-"
subpixel="sub-pixel.conf"
no_subpixel="no-sub-pixel.conf"

if [ -h $CONFDIR/$subpixel_prio$subpixel ]; then
	rm $CONFDIR/$subpixel_prio$subpixel
fi

if [ -h $CONFDIR/$subpixel_prio$no_subpixel ]; then
	rm $CONFDIR/$subpixel_prio$no_subpixel
fi

case "$subpixel_rendering" in
"Automatic")
	;;
"Always")
	ln -s $CONFDIR/$subpixel $CONFDIR/$subpixel_prio$subpixel
	;;
"Never")
	ln -s $CONFDIR/$no_subpixel $CONFDIR/$subpixel_prio$no_subpixel
	;;
esac

db_get fontconfig/enable_bitmaps
enable_bitmaps="$RET"

bitmaps_prio="30-debconf-"
yes_bitmaps="yes-bitmaps.conf"
no_bitmaps="no-bitmaps.conf"

if [ -h $CONFDIR/$bitmaps_prio$yes_bitmaps ]; then
	rm $CONFDIR/$bitmaps_prio$yes_bitmaps
fi

if [ -h $CONFDIR/$bitmaps_prio$no_bitmaps ]; then
	rm $CONFDIR/$bitmaps_prio$no_bitmaps
fi

case "$enable_bitmaps" in
"true")
#
# Bitmap fonts will be enabled by default, so there's no need
# to use this configuration file.  However, the file remains useful if
# you want to force bitmaps to be considered even when some application
# disables them.
#
#	ln -s $CONFDIR/$yes_bitmaps $CONFDIR/$bitmaps_prio$yes_bitmaps
	;;
*)
	ln -s $CONFDIR/$no_bitmaps $CONFDIR/$bitmaps_prio$no_bitmaps
	;;
esac

cp /dev/null $LOCALCONF
rm -f $LOCALCONF.md5sum
ln -sf /usr/share/fontconfig/local.conf.md5sum $LOCALCONF.md5sum
ucf --debconf-ok $LOCALCONF /etc/fonts/local.conf
rm -f $LOCALCONF.md5sum

# if the local.conf file is now empty, remove it

if [ -s /etc/fonts/local.conf ]; then
	:
else
	if [ -f /etc/fonts/local.conf ]; then
		rm /etc/fonts/local.conf
	fi
fi

# Create /usr/local/share/fonts
LOCALDIR=/usr/local/share/fonts
if [ ! -d $LOCALDIR ]; then
  if mkdir $LOCALDIR 2>/dev/null ; then
    chmod 2775 $LOCALDIR
    chown root:staff $LOCALDIR
  fi
fi

if [ "$1" = configure ]; then
  # Ensure Defoma subst file exists, with some default substitutions
  if ! defoma-subst check-rule fontconfig; then
    defoma-subst new-rule fontconfig \
      'serif --GeneralFamily,* Roman --Shape Serif Upright --Weight Medium' \
      'sans-serif --GeneralFamily,* SansSerif --Shape NoSerif Upright --Weight Medium' \
      'monospace --Width,* Fixed --GeneralFamily,2 Typewriter --Shape Upright --Weight Medium'
  fi
fi

if [ -d /var/lib/defoma/x-ttcidfont-conf.d ]; then
  # Remove old fonts.cache-1 files
  find /var/lib/defoma/x-ttcidfont-conf.d -name fonts.cache-1 | xargs rm -f
fi

#DEBHELPER#  

if [ "$1" = configure ]; then
  # (Hacked up from Red Hat 8 fontconfig RPM)
  # Force regeneration of all fontconfig cache files.
  # The redirect is because fc-cache is giving warnings about ~/fc.cache
  # the HOME setting is to avoid problems if HOME hasn't been reset
  printf "Regenerating fonts cache... " 
  HOME=/root fc-cache -f -v 1>/var/log/fontconfig.log 2>&1 || (printf "failed.\nSee /var/log/fontconfig.log for more information.\n"; exit 1)
  printf "done.\n"
fi

exit 0