#!/bin/sh
# 
#  Copyright (c) 1998
#   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)
# 

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

# the directories with source TTF files
SRCDIR1=/SOME_DIR_WITH_WIN-1251_FONTS
SRCDIR2=/SOME_DIR_WITH_KOI-8_FONTS
SRCDIR3=
SRCDIR4=
SRCDIR5=
SRCDIR6=
SRCDIR7=
SRCDIR8=
SRCDIR9=

# languages for the TTF files in these directories
SRCLANG1=russian
SRCLANG2=russian
SRCLANG3=
SRCLANG4=
SRCLANG5=
SRCLANG6=
SRCLANG7=
SRCLANG8=
SRCLANG9=

# the encodings of TTF files in these directories
SRCENC1=ibm-1251
SRCENC2=koi8-r
SRCENC3=
SRCENC4=
SRCENC5=
SRCENC6=
SRCENC7=
SRCENC8=
SRCENC9=

# the directory for converted fonts
DSTDIR=/usr/X11R6/lib/X11/fonts/ttf

# the encodings of generated files by languages
DSTENCrussian="koi8-r ibm-1251 iso8859-1"
DSTENClatin1="iso8859-1"

# name of foundry for generated fonts
# (for HP-UX change to "misc")
FOUNDRY=ttf

# path to the directory with descriptions of encodings
ENCDIR=`pwd`/../encodings

# paths to various utilities

T1ASM=`pwd`/../t1asm
TTF2PT1=`pwd`/../ttf2pt1
TRANS=`pwd`/trans
T1FDIR=`pwd`/t1fdir
FORCEISO=`pwd`/forceiso

LOG=$DSTDIR/convert.log

MYSELF=convert

# 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
}

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

for dirno in 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"`
	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" -a -n "$DSTENC" ] || {
		echo "$MYSELF: encodings must be not empty" >&2
		exit 1
	}

	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
		
		$TTF2PT1 -l $SRCLANG $file - 2>>$LOG >./$name.$SRCENC.t1a 2>>$LOG

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

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

		for enc in $DSTENC
		do {
			echo "******* $name -> $enc ************" >>$LOG

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

			$T1FDIR $FOUNDRY $enc ./$name.$enc.pfa >>fonts.ttf
			$T1FDIR -g $FOUNDRY $enc ./$name.$enc.pfa >>Fontmap.ttf
		} done

	} done
} done

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