:
# SCCSID(@(#)mrgfontmk.sh	1.3 LCC) Modified 22:38:20 12/15/92
### Copyright (c) 1992 Locus Computing Corporation
### All rights reserved.
### This is an unpublished work containing CONFIDENTIAL INFORMATION
### that is the property of Locus Computing Corporation.
### Any unauthorized use, duplication or disclosure is prohibited.
########################################################################
#
#	Script to re-compile the Merge X fonts.
#	The fonts are listed in the "fonts.src" file.
#
########################################################################

# This script MUST be run by /bin/sh
if [ -z "$REALLY_BIN_SH" ]
then
	REALLY_BIN_SH=really_bin_sh
	export REALLY_BIN_SH
	SCRIPT=$0
	if [ "z$1" = "z-x" ]
	then
		shift
		exec /bin/sh -x $SCRIPT $*
	fi
	exec /bin/sh $SCRIPT $*
fi

LIBM=/usr/lib/merge
if [ ! -f $LIBM/set_nls.sh ]
then
	# Can't nlsize this message.
	echo "Error: Missing $LIBM/set_nls.sh"
	exit 1
fi
. $LIBM/set_nls.sh
# DM is the domain for messages for this script.
DM="LCC.MERGE.UNIX.MERGEFONTMAKE"

Usage()
{
	usrMsg ECHO MFM_USAGE "\
USAGE: mergefontmake  0|1|2|3|4  [s|ML]\n\
Re-compile the Merge fonts.\n\
\n\
Fonts can be compiled in four different ways, and the font\n\
only works when compiled the way the X-Server expects.\n\
The first parameter specifies which of the four ways to\n\
compile the fonts: 1, 2, 3, or 4.\n\
The parameter '0' causes it to use the defaults, whatever they are.\n\
\n\
'mergefontmake' tries to figure out what version of font\n\
compiler you are using, because different X versions use\n\
different options for compiling fonts. (Some version use\n\
the '-s' option instead of '-M' and '-L'). To force the\n\
type of options that are used, specify 's' or 'ML' as the\n\
second parameter.\n"
}

if [ $# -lt 1 -o $# -gt 2 ]
then
	Usage
	exit 1
fi

case "z$2" in
z)
	if [ ! -f /usr/merge/Version ]
	then
		usrMsg ECHO MFM_MISSING "Error: Missing /usr/merge/Version\n" /usr/merge/Version
		exit 1
	fi
	. /usr/merge/Version
	if [ "z$SYSTEMTYPE" = "zsco" ]
	then
		TYPE=ML
	else
		TYPE=s
	fi
	;;
zs)
	TYPE=s
	;;
zML)
	TYPE=ML
	;;
*)
	usrMsg ECHO MFM_BAD_SECONDP "Error: Second parameter must be 's' or 'ML' or not specified.\n"
	exit 1
	;;
esac

X_FONTDIR=/usr/X/lib/fonts/misc
X11_FONTDIR=/usr/lib/X11/fonts/misc
X_BIN=/usr/X/bin
X11_BIN=/usr/bin/X11

if [ -d $X_BIN ]
then
	XBIN=$X_BIN
elif [ -d $X11_BIN ]
then
	XBIN=$X11_BIN
else
	usrMsg ECHO MFM_NO_XBIN "Error: Cannot find the X bin directory.\n"
	exit 1
fi
if [ ! -x $XBIN/mkfontdir ]
then
	usrMsg ECHO MFM_MISSING "Error: Missing $XBIN/mkfontdir\n" $XBIN/mkfontdir
	exit 1
fi

FONTCOMP=$XBIN/bdftosnf
if [ ! -x $FONTCOMP ]
then
	usrMsg ECHO MFM_MISSING "Error: Missing $FONTCOMP\n" $FONTCOMP
	exit 1
fi

if [ -d $X_FONTDIR ]
then
	FONTDIR=$X_FONTDIR
elif [ -d $X11_FONTDIR ]
then
	FONTDIR=$X11_FONTDIR
else
	usrMsg ECHO MFM_NO_FONTDIR "Error: Cannot determine the font directory.\n"
	exit 1
fi
if [ ! -w $FONTDIR ]
then
	usrMsg ECHO MFM_I_NOPERMIS "Error: Don't have permission to install fonts into $FONTDIR\n" $FONTDIR
	exit 1
fi
FSRC=/usr/merge/xc/fonts.src
if [ ! -f $FSRC ]
then
	usrMsg ECHO MFM_MISSING "Error: Missing $FSRC\n" $FSRC
	exit 1
fi

# Options to bdftosnf:
# Bit order option: use either "-l" or "-m", (but not both of course).
#   -l  least significant bit first. 
#   -m  most significant bit first. 
# Byte order options:  SVR4: "-s" only,  SCO: "-L" or "-M"
# SCO only: use either -L or -M, (but not both of course).
#   -L  Least significant byte first.
#   -M  Most significant byte first.
# Note: SCO documents the L and M option, but they seem to be ignored.
# SVR4 only: optional "-s"
#   -s              Swap bytes.
#   (no -s option)  Don't swap bytes.
#
# SVR4 default: -m
# SCO default: -m
#
# For SVR4.0 (Microport's v3.1) must use "-s -m".
# For SVR4.2 and SVR4.0 (Microport's v4.1) must use "-l"
# For SCO's X11R3, and X11R4, must use -m (which is also the default).
# (For SCO's X11R5, must use "-l", but we use PCF files for that version.)
# If the byte order is wrong, then no characters will be visible.
# If only the bit order is wrong, then you see 1/2 of the chacter, reversed.
#
# Note: On SVR4 systems:
#    1) The "-s" option does not seem to be documented,
#	but it works, and is required for some servers.
#    2) The "-L" and "-M" options ARE documented, but they 
#	DO NOT do anything.
# On SCO systems, the "-L" and "-M" are documented and do not work.
# Some versions of bdftosnf do not output on stdout as documented.  It
# creates a file based on the input filename.

if [ "$TYPE" = "ML" ]
then
	# The order of these have no meaning other than #1 is what works
	# with ODT 1.x.
	case "$1" in
	1)
		# This is what works on ODT 1.x.
		OPTIONS="-l -M"
		;;
	2)
		# This is what works on ODT 2.0.
		OPTIONS="-m -L"
		;;
	3)
		# If the font compiler ignores L and M, then same as #1
		OPTIONS="-l -L"
		;;
	4)
		# If the font compiler ignores L and M, then same as #2
		OPTIONS="-m -M"
		;;
	0)
		# Use the defaults.
		OPTIONS=
		;;
	*)
		usrMsg ECHO MFM_01234 "Error: Must use 0, 1, 2, 3, or 4.\n"
		exit 1
		;;
	esac
else
	# For SVR4.0 (Microport's v3.1) must use "-s -m"
	# For SVR4.0 (Microport's v4.1) and SVR4.1 must use "-l"
	case "$1" in
	1)
		# This works for newer systems.
		OPTIONS="-l"
		;;
	2)
		# This works for older systems.
		OPTIONS="-m -s"
		;;
	3)
		# No known systems use this.
		OPTIONS="-l -s"
		;;
	4)
		# No known systems use this.
		OPTIONS="-m"
		;;
	0)
		# Use defaults.
		OPTIONS=
		;;
	*)
		usrMsg ECHO MFM_01234 "Error: Must use 0, 1, 2, 3, or 4.\n"
		exit 1
		;;
	esac
fi

cd $FONTDIR
export OPTIONS FONTCOMP FONTDIR
cat  $FSRC | while true
do
	read ARG1 ARG2 ARG3
	if [ -z "$ARG1" ]
	then
		exit 0
	fi
	echo $ARG2
	if [ ! -f $ARG2 ]
	then
		usrMsg ECHO MFM_MISSING "Error: Missing $ARG2\n" $ARG2
		continue
	fi
	OUTPUTNAME=`basename $ARG2 '.bdf'`.snf
	if [ -f $OUTPUTNAME -a ! -w $OUTPUTNAME ]
	then
		usrMsg ECHO MFM_U_NOPERMIS "Error: No permission to update $FONTDIR/$OUTPUTNAME\n" $FONTDIR/$OUTPUTNAME
		continue
	fi
	[ -s $OUTPUTNAME ] && mv $OUTPUTNAME $$.save
	$FONTCOMP $OPTIONS $ARG2 > $$.tmp
	RETVAL=$?
	if [ $RETVAL -ne 0 ]
	then
		usrMsg ECHO MFM_FC_ERR "Error: $FONTCOMP returned $RETVAL\n" $FONTCOMP $RETVAL
		rm -f $$.tmp
		[ -f $$.save ] && mv $$.save $OUTPUTNAME
		continue
	fi
	# Contrary to the documentation, some bdftosnf programs
	# do not output to stdout, but put the compiled data
	# into a file based on the input file name, with the
	# "bdf" replaced with "snf".  This is what we want anyway,
	# so just recognize when this happens and go with it.
	if [ ! -s $OUTPUTNAME ]
	then
		if [ ! -s $$.tmp ]
		then
			usrMsg ECHO MFM_COMP_ERR "Error compiling $ARG2\n" $ARG2
			rm -f $$.tmp
			[ -f $$.save ] && mv $$.save $OUTPUTNAME
			continue
		fi
		mv $$.tmp $OUTPUTNAME
	fi
	chmod 444 $OUTPUTNAME
	chgrp bin $OUTPUTNAME
	chown bin $OUTPUTNAME
	rm -f $$.tmp $$.save
done

$XBIN/mkfontdir
sync
exit 0
