#!/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 : convert [cfgfile]

# Convert TTF fonts from source directory to Type1 fonts in the destination
# directory, converted to the specified encodings. Also generate the
# fonts.scale, fonts.dir and fonts.alias files in the destination directory.

# clean some variables so that they won't be inherited from environment

ENCDIR=
MAPDIR=

# path to the configuration file

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

# these setting would be edited during installation

TTF2PT1_BINDIR=
TTF2PT1_LIBXDIR=
TTF2PT1_SHAREDIR=

[ -z "$TTF2PT1_BINDIR" ] && {
	TTF2PT1_BINDIR=`pwd`/..
}
[ -z "$TTF2PT1_LIBXDIR" ] && {
	TTF2PT1_LIBXDIR=`pwd`/..
}
[ -z "$TTF2PT1_SHAREDIR" ] && {
	TTF2PT1_SHAREDIR=`pwd`/..
}

# directory from where we are started

RUNDIR=`pwd`

# paths to various utilities

T1ASM=$TTF2PT1_LIBXDIR/t1asm
[ -f $T1ASM -a -x $T1ASM ] || {
	# if it's not in libxdir, use whatever t1asm the system provides
	T1ASM=t1asm
}
TTF2PT1=$TTF2PT1_BINDIR/ttf2pt1
TRANS=$TTF2PT1_SHAREDIR/scripts/trans
T1FDIR=$TTF2PT1_SHAREDIR/scripts/t1fdir
FORCEISO=$TTF2PT1_SHAREDIR/scripts/forceiso
X2GS=$TTF2PT1_SHAREDIR/scripts/x2gs
SUFFIX="pfa"

MYSELF=convert

# 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

# path to the directory with descriptions of encodings
[ -z "$ENCDIR" ] && {
	ENCDIR=$TTF2PT1_SHAREDIR/encodings
}

# directory with the external Unicode maps
[ -z "$MAPDIR" ] && {
	MAPDIR=$TTF2PT1_SHAREDIR/maps
}

LOG=$DSTDIR/convert.log

# configure the ttf2pt1 options from our options

# artefact of backwards-compatibility with .cfg
[ -z "$CORRECTWIDTH" -a YES != "$DONTCORRECTWIDTH" ] && {
	TTF2PT1="$TTF2PT1 -OW"
}
[ YES = "$CORRECTWIDTH" ] && {
	TTF2PT1="$TTF2PT1 -OW"
}

[ YES != "$HINTSUBST" ] && {
	TTF2PT1="$TTF2PT1 -Ou" # meaning changed past 3.22
}

[ YES = "$ALLGLYPHS" -a YES = "$ENFORCEISO" ] && {
	echo "$MYSELF: options ALLGLYPHS and ENFORCEISO are mutually exclusive" >&2
	exit 1
}

[ YES = "$ALLGLYPHS" ] && {
	TTF2PT1="$TTF2PT1 -a"
}

[ YES = "$GENUID" ] && {
	TTF2PT1="$TTF2PT1 -uA"
}

[ YES != "$ENFORCEISO" ] && {
	FORCEISO=cat
}

[ YES = "$CREATEPFB" ] && {
	T1ASM="$T1ASM -b"
	SUFFIX="pfb"
}

# parse the information about the source files

eval "`echo \"$SRCDIRS\" | awk '
	BEGIN	{ n=0; }
	/^ *$/	{ next; }
		{ 
			if(n>9) { 
				printf(\"echo \\\"Only 9 encodings are supported at once!\\\" >&2\n\");
				printf(\"exit 1\\n\");
			} else {
				printf(\"SRCDIR%d=%s\n\",n,$1);
				printf(\"SRCLANG%d=%s\n\",n,$2);
				printf(\"SRCENC%d=%s\n\",n,$3);
				printf(\"SRCMAP%d=%s\n\",n,$4);
				n++;
			}
		}'`"

# check whether we have the directories

mkdir $DSTDIR 2>/dev/null >/dev/null
[ -d $DSTDIR -a -r $DSTDIR -a -w $DSTDIR -a -x $DSTDIR ] || {
	echo "$MYSELF: can't access the directory $DSTDIR" >&2
	exit 1
}

# go to our destination directory

cd $DSTDIR || {
	echo "$MYSELF: can't chdir to $DSTDIR" >&2
	exit 1
}

rm -f ./* 2>/dev/null
>$LOG

for dirno in 0 1 2 3 4 5 6 7 8 9
do {
	
	SRCDIR=`eval "echo \\\$SRCDIR$dirno"`
	SRCLANG=`eval "echo \\\$SRCLANG$dirno"`
	SRCENC=`eval "echo \\\$SRCENC$dirno"`
	SRCMAP=`eval "echo \\\$SRCMAP$dirno"`
	DSTENC=`eval "echo \\\$DSTENC$SRCLANG"`

	echo $SRCDIR
	echo $SRCENC

	[ -z "$SRCDIR" ] && break;

	[ "`ls $SRCDIR/*.[tT][tT][fF] 2>/dev/null |wc -l`" -gt 0 ] || {
		echo "$MYSELF: no TTF files in $SRCDIR" >&2
		exit 1
	}

	# check whether we have the encoding tables

	[ -n "$SRCENC" ] || {
		echo "$MYSELF: you must specify some source encoding" >&2
		exit 1
	}

	[ unknown = "$SRCLANG" -o -n "$DSTENC" ] || {
		echo "$MYSELF: you must specify some destination encodings" >&2
		exit 1
	}

	# handle aliases of the destination encodings

	XDSTENC=
	DSTALIAS=

	[ -r $ENCDIR/$SRCLANG/encodings.alias ] && {
		for i in $DSTENC
		do {
			TO=`awk  '$1=="'$i'" { print $2; }' <$ENCDIR/$SRCLANG/encodings.alias`
			if [ -n "$TO" ]
			then {
				[ -f $ENCDIR/$SRCLANG/$i.tbl -a -r $ENCDIR/$SRCLANG/$i.tbl ] && {
					echo "WARNING: $SRCLANG encoding $i found as both table and alias" >&2
					echo "WARNING: The alias takes precedence" >&2
				}
				DSTALIAS="$TO $i
$DSTALIAS"
				XDSTENC="$TO
$XDSTENC"
			} else {
				XDSTENC="$i
$XDSTENC"
			} fi
		} done
		DSTENC=`echo "$XDSTENC" | sort -u | tr '
' ' '`
	}

	[ unknown != "$SRCLANG" ] && {
		for i in $SRCENC $DSTENC
		do {
			[ -f $ENCDIR/$SRCLANG/$i.tbl -a -r $ENCDIR/$SRCLANG/$i.tbl ] || {
				echo "$MYSELF: can't read $ENCDIR/$SRCLANG/$i.tbl" >&2
				exit 1
			}
		} done
	}

	# OK convert the files

	for file in $SRCDIR/*.[tT][tT][fF]
	do {
		name=`echo $file | tr A-Z a-z`
		name=`basename $name .ttf`

		echo "Converting $name"

		# generate the assembler code

		echo "******* $name -> t1a ************" >>$LOG
		
		if [ -n "$SRCMAP" ]
		then {
			$TTF2PT1 -L $MAPDIR/$SRCMAP $file ./$name.$SRCENC 2>>$LOG
		} else {
			$TTF2PT1 -l $SRCLANG $file ./$name.$SRCENC 2>>$LOG
		} fi

		[ -s ./$name.$SRCENC.t1a ] || {
			echo "$MYSELF: can't generate Type1 assembler code for $name" >&2
			continue;
		}

		[ -s ./$name.$SRCENC.afm ] || {
			echo "$MYSELF: can't generate AFM metrics file for $name" >&2
			continue;
		}

		mv ./$name.$SRCENC.afm ./$name.$SRCENC.xafm

		psname=`$T1FDIR -g $FOUNDRY " " -f ./$name.$SRCENC.t1a \
			| awk '{print substr($1,2);}'`

		# now for each destination encoding generate a .pfa/b file
		# and record for fonts.scale

		if [ unknown != "$SRCLANG" ]
		then {
			for enc in $DSTENC
			do {
				echo "******* $name -> $enc ************" >>$LOG

				sed 's|^\/FontName.*$|/FontName /'$psname$enc' def|' <./$name.$SRCENC.t1a \
					| $TRANS $ENCDIR/$SRCLANG/$SRCENC.tbl $ENCDIR/$SRCLANG/$enc.tbl \
					| $FORCEISO | $T1ASM >./$name.$enc.$SUFFIX
				[ -s ./$name.$enc.$SUFFIX ] || {
					echo "$MYSELF: can't convert/assemble Type1 file for $name.$enc" >&2
					continue;
				}

				sed 's|^FontName.*$|FontName '$psname$enc'|' <./$name.$SRCENC.xafm \
					| $TRANS $ENCDIR/$SRCLANG/$SRCENC.tbl $ENCDIR/$SRCLANG/$enc.tbl \
					| uniq | $FORCEISO >./$name.$enc.afm
				[ -s ./$name.$enc.afm ] || {
					echo "$MYSELF: can't convert AFM file for $name.$enc" >&2
				}

				aliases=`echo "$DSTALIAS" | grep "^$enc" | cut -d\  -f2`
				echo "******* aliases: $aliases" >>$LOG

				$T1FDIR -d fonts.ttf fonts.alias $FOUNDRY $enc $aliases -f ./$name.$enc.$SUFFIX
				echo "$psname$enc	($name.$enc.$SUFFIX)	;" >>Fontmap.ttf
			} done
		} else {
			enc="$SRCENC"
			echo "******* $name -> $enc ************" >>$LOG

			sed 's|^\/FontName.*$|/FontName '$psname$enc' def|' <./$name.$SRCENC.t1a \
				| $FORCEISO | $T1ASM >./$name.$enc.$SUFFIX
			[ -s ./$name.$enc.$SUFFIX ] || {
				echo "$MYSELF: can't convert/assemble Type1 file for $name.$enc" >&2
				continue;
			}

			sed 's|^FontName.*$|FontName '$psname$enc'|' <./$name.$SRCENC.xafm \
				| uniq | $FORCEISO >./$name.$enc.afm
			[ -s ./$name.$enc.afm ] || {
				echo "$MYSELF: can't convert AFM file for $name.$enc" >&2
			}

			$T1FDIR -d fonts.ttf fonts.alias $FOUNDRY $enc -f ./$name.$enc.$SUFFIX
			echo "$psname$enc	($name.$enc.$SUFFIX)	;" >>Fontmap.ttf
		} fi

		[ YES = "$REMOVET1A" ] && {
			rm -f ./$name.$SRCENC.t1a
			rm -f ./$name.$SRCENC.xafm
		}

	} done
} done

wc -l <fonts.ttf >fonts.scale
cat fonts.ttf >>fonts.scale
mkfontdir

[ YES = "$GENUID" ] && {
	echo "Checking for duplicate UniqueIDs..."
	for id in `find . -name "*.$SUFFIX" -exec grep UniqueID {} \; \
			| cut -d" " -f2 | sort | uniq -d`
	do {
		echo "Warning: duplicate UniqueID $id in files:" | tee -a $LOG
		find  . -name "*.$SUFFIX" -exec grep -l "UniqueID $id " {} \; 2>&1 | tee -a $LOG
	} done
}

[ -n "$GSDIR" ] || {
	echo "$MYSELF: The Ghostscript base directory is not specified.\n" >&2
	echo "$MYSELF: Installation of the Ghostscript fonts is deferred.\n" >&2
	echo "$MYSELF: You can do it later by running x2gs\n" >&2
	exit 0
}

echo "Installing the Ghostscript fonts"
cd $RUNDIR
$X2GS $CFGFILE || {
	echo "$MYSELF: Installation of the Ghostscript fonts has failed.\n" >&2
	echo "$MYSELF: You can correct the problem and run x2gs to repeat\n" >&2
	exit 0
}