#!/bin/sh

debug_echo=true

prog=`basename $0`

if [ X"$1" = X"-help" ]; then
	sed -e "s/@PROG@/$prog/g" <<END_OF_MESSAGE
usage: @PROG@ [ base=BASE ] [ dpi=DPI ] [ mag=MAG | maglist=MAGLIST ] FONT ..
MAGLIST is 'MAG1:MAG2:..:MAGX', 'magstep[0-5]', or 'magstep[6-8]'
examples: % @PROG@ base=cmmf dpi=400 mag="magstep(0)" cmr10
          % @PROG@ base=cmmf dpi=400x200 maglist=0.8:0.9 cmb10
          % @PROG@ base=cmmf dpi=400half maglist="magstep[0-5]" cmcsc10
END_OF_MESSAGE
	exit 0
fi

BASE=""
DPI=""
MAGLIST=""

if [ -s $prog.defaults ]; then
	. $prog.defaults
fi

while [ $# -gt 0 ]; do
	case "$1" in
	  base=*)
		BASE=`echo "$1" | sed -e 's/^base=//'`
		shift;;
	  dpi=*)
		DPI=`echo "$1" | sed -e 's/^dpi=//'`
		shift;;
	  mag=*)
		MAGLIST=`echo "$1" | sed -e 's/^mag=//'`
		shift;;
	  maglist=*)
		MAGLIST=`echo "$1" | sed -e 's/^maglist=//'`
		shift;;
	  *)
		break;;
	esac
done

# BASE=
if [ X"$BASE" = X"" ]; then
	if [ ! -t 0 -o ! -t 1 -o ! -t 2 ]; then
		echo 1>&2 "$prog: no base file specified."
		exit 9
	fi

	while [ X"$BASE" = X"" ]; do
		echo 1>&2 -n "base file name: "
		read BASE
	done
fi

case "$BASE" in
  *.base)
	BASE=`echo "$BASE" | sed -e 's/\.base$//'`;;
esac

$debug_echo 1>&2 "### base file name = \"$BASE.base\""

# DPI=
if [ X"$DPI" = X"" ]; then
	if [ ! -t 0 -o ! -t 1 -o ! -t 2 ]; then
		echo 1>&2 "$prog: no dpi specified."
		exit 9
	fi

	while [ X"$DPI" = X"" ]; do
		echo 1>&2 -n "dpi: "
		read DPI
	done
fi

case $DPI in
  *x*)
	x_dpi=`echo "$DPI" | sed -e 's/x.*$//'`
	y_dpi=`echo "$DPI" | sed -e 's/^.*x//'`
	if [ `expr $x_dpi = $y_dpi` -ne 0 ]; then
		DPI=$x_dpi
		HALF=""
	elif [ `expr $x_dpi = $y_dpi \* 2` -ne 0 ]; then
		DPI=$x_dpi
		HALF=half
	else
		echo 1>&2 "Sorry. I'm not supported $x_dpi x $y_dpi dpi."
		exit 1
	fi;;
  *half)
	DPI=`echo "$DPI" | sed -e 's/half$//'`
	HALF=half;;
  *)
	DPI="$DPI"
	HALF="";;
esac

$debug_echo 1>&2 "### dots per inchi = $DPI$HALF"

# MAGLIST=
if [ X"$MAGLIST" = X"" ]; then
	if [ ! -t 0 -o ! -t 1 -o ! -t 2 ]; then
		echo 1>&2 "$prog: no magnifications specified."
		exit 9
	fi

	while [ X"$MAGLIST" = X"" ]; do
		echo 1>&2 -n "magnification list: "
		read MAGLIST
	done
fi

case "$MAGLIST" in
  *"magstep[0-5]"*)
	MAGLIST=`echo "$MAGLIST" | sed -e 's/magstep\[0-5\]/magstep(0):magstep(0.5):magstep(1):magstep(2):magstep(3):magstep(4):magstep(5)/g'`
esac

case "$MAGLIST" in
  *"magstep[6-8]"*)
	MAGLIST=`echo "$MAGLIST" | sed -e 's/magstep\[6-8\]/magstep(6):magstep(7):magstep(8)/g'`
esac

$debug_echo 1>&2 "### magnification list = $MAGLIST"

# MODENAME=
MODENAME=`modename $DPI $HALF`

# font list
if [ $# -lt 1 ]; then
	if [ ! -t 0 -o ! -t 1 -o ! -t 2 ]; then
		echo 1>&2 "$prog: no fonts specified."
		exit 9
	fi

	while [ $# -eq 0 ]; do
		echo 1>&2 -n "font list: "
		set `line`
	done
fi

FONT=$1
shift
for font do
	FONT=$FONT":"$font
done

$debug_echo 1>&2 "### font list = $FONT"

echo "+ $prog base=$BASE dpi=$DPI maglist=$MAGLIST `echo $FONT | tr ':' ' '`"

# main
DIST=${DIST-.}
TFMDIST=${TFMDIST-$DIST}
PKDIST=${PKDIST-$DIST}

case $TFMDIST in
  */tfm)
	TFMDIST=`dirname $TFMDIST`
esac

GFFONTS=.
export GFFONTS

IFS=:

for font in $FONT; do
	for mag in $MAGLIST; do
		dpi=`dpi $DPI $mag`
		pkdir=$PKDIST/pk$dpi$HALF
		tfmdir=$TFMDIST/tfm

		if [ ! -d $tfmdir ]; then
			rm -f $tfmdir; mkdir $tfmdir
		fi

		if [ ! -d $pkdir ]; then
			rm -f $pkdir; mkdir $pkdir
		fi

		case "$font" in
		  circle*)
			if [ ! -s $tfmdir/$font.tfm ]; then
				rm -f $tfmdir/$font.tfm
				( cd $tfmdir && ln -s ./l$font.tfm ./$font.tfm )
			fi
			if [ ! -s $pkdir/$font.$dpi"pk" ]; then
				rm -f $tfmdir/$font.$dpi"pk"
				( cd $pkdir && ln -s ./l$font.$dpi"pk" ./$font.$dpi"pk" )
			fi
			continue;;
		esac

		if [ -s $pkdir/$font.$dpi"pk" ]; then
			continue
		fi

		set -e
		virmf "&$BASE \mode:=$MODENAME; \mag:=$mag; \scrollmode; \input $font;"
		gftopk $font.$dpi"gf" $pkdir/$font.$dpi"pk"
		set +e

		mv -f $font.tfm $tfmdir

		rm $font.log $font.$dpi"gf"
	done
done

exit 0

