#!/bin/sh
#
#  Copyright (c) 1998-2000
#   Sergey A. Babkin.  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
#  Sergey A. Babkin (sab123@hotmail.com, babkin@bellatlantic.net)
#

# Use : x2gs [cfgfile]

# path to the configuration file

if [ $# -eq 1 ]
then
	CFGFILE=$1
else
	CFGFILE=`pwd`/convert.cfg
fi

MYSELF=x2gs

# include the configuration

if [ -r $CFGFILE ] 
then {
	. $CFGFILE
} else {
	echo "
Can't find the configuration file 
   $CFGFILE
Please look at the sample file convert.cfg.sample,
copy it to convert.cfg and modify for
you actual configuration." >&2
	exit 1
} fi

LOG=$DSTDIR/convert.log

[ -n "$GSDIR" ] || {
	echo "$MYSELF: The Ghostscript base directory is not specified." >&2
	echo "$MYSELF: Installation of the Ghostscript fonts is deferred." >&2
	exit 0
}

[ -n "$GSCONFDIR" -a -d "$GSCONFDIR" ] || {
	echo "$MYSELF: The Ghostscript configuration directory does not exist." >&2
	echo "$MYSELF: Installation of the Ghostscript fonts is aborted." >&2
	exit 1
}

[ -r "$GSCONFDIR/Fontmap" ] || {
	echo "$MYSELF: Can't find Fontmap in the GS configuration directory." >&2
	echo "$MYSELF: Installation of the Ghostscript fonts is aborted." >&2
	exit 1
}

[ -n "$GSFONTDIR" -a -d "$GSFONTDIR" ] || {
	echo "$MYSELF: The Ghostscript font directory does not exist." >&2
	echo "$MYSELF: Installation of the Ghostscript fonts is aborted." >&2
	exit 1
}

# link the fonts to $GSFONTDIR

rm -f $GSCONFDIR/Fontmap.ttf

# historically x2gs supported multiple X11 directories
for d in $DSTDIR
do {
	for i in $d/*.pfa $d/*.afm
	do {
		[ -f $i ] || break;

		n=`basename $i`

		rm -f $GSFONTDIR/$n

		ln -s $i $GSFONTDIR/$n || {
			echo "$MYSELF: Unable to link $n to GS font directory">&2
		}
	} done

	cat $d/Fontmap.ttf >>$GSCONFDIR/Fontmap.ttf
} done

if [ YES = "$INSTALLFONTMAP" ]
then {
	mv $GSCONFDIR/Fontmap $GSCONFDIR/Fontmap.old || {
		echo "$MYSELF: can't save Fontmap.old" >&2
		exit 1
	}

	sed "\\|^% begin fonts from $DSTDIR|,\\|^% end fonts from $DSTDIR|d" \
			<$GSCONFDIR/Fontmap.old >$GSCONFDIR/Fontmap || {
		echo "$MYSELF: Can't create the new Fontmap file" >&2
		echo "$MYSELF: Trying to restore the old Fontmap file" >&2
		cp $GSCONFDIR/Fontmap.old $GSCONFDIR/Fontmap
		exit 1
	}
	echo "% begin fonts from $DSTDIR" >>$GSCONFDIR/Fontmap
	echo "" >>$GSCONFDIR/Fontmap
	cat $GSCONFDIR/Fontmap.ttf >>$GSCONFDIR/Fontmap
	echo "" >>$GSCONFDIR/Fontmap
	echo "% end fonts from $DSTDIR" >>$GSCONFDIR/Fontmap
} fi
