#!/bin/sh
#
# preinst script for the Debian GNU/Linux lilypond package
#   by Anthony Fok <foka@debian.org>
#   This is free software; see the GNU General Public Licence
#   version 2 or later for copying conditions.  There is NO warranty.
#   Last modified:  Mon,  5 Jan 1998 18:39:30 -0700

set -e

package=lilypond
font_supplier=public
font_typeface=lilypond
std_TEXMF=/usr/lib/texmf

#DEBHELPER#

if [ "`which kpsetool`" ]; then
    TEXMF=`kpsetool -v '$TEXMF'`
fi
: ${TEXMF:=$std_TEXMF}


case "$1" in

    install | upgrade)

	# Prior to the Debian release of the lilypond_0.1.29-1 package,
	# LilyPond's fonts were not added to teTeX's special.map,
	# and so the automatically generated TFM and PK files were placed
	# in the wrong directory under /var/spool/texmf (or $MT_DESTROOT).
	# So, now I have to clean up after my mess to make sure those fonts
	# do not interfere with the new fonts!  :)

	: ${MAKETEXDIR=$TEXMF/maketex}
	test -r $MAKETEXDIR/maketex.site && . $MAKETEXDIR/maketex.site
	: ${MT_DESTROOT=$TEXMF/fonts}

	# Remove the "stray" old LilyPond TFM and PK files.
	for i in tfm pk
	do
	    find $MT_DESTROOT/$i \( -name 'dyn10.*'$i -o -name 'font-en-tja*.*'$i \
		-o -name 'vette-beams*.*'$i \) -print0 |
		xargs --null --no-run-if-empty rm -f
	done

	# Remove empty directories.
	# A note to myself or the future maintainer:
	#   Why the "for i in 1 2" loop?  Well, it seems that either
	#   "find some_path -depth" isn't working properly, or I
	#   (Anthony) don't know its proper behaviour.  Please look
	#   into it and see if this ugly hack could be removed.
	for i in 1 2
	do
	    find $MT_DESTROOT/tfm -depth -mindepth 1 -type d -empty -print0 2> /dev/null |
		xargs -0r --null --no-run-if-empty rmdir
	    find $MT_DESTROOT/pk -depth -mindepth 2 -type d -empty -print0 2> /dev/null |
		xargs -0r --null --no-run-if-empty rmdir
	done
    ;;

    abort-upgrade)
    ;;

    *)
        echo "$0 called with unknown argument \`$1'" >&2
        exit 0
    ;;

esac
