#! /bin/sh
# This file is part of LaTeX2e CJK package.
#
# Generating the Fontmap and the psfonts.map files for temporary use with
# Ghostscript when making PostScript fonts for CJK package. Alternatively, 
# the generated file can be appended to the officially released Fontmap and
# psfonts.map to simulate printer resident PS fonts.
#
# Note: you'd better backup the original Fontmap and psfonts.map.

execfile=`basename $0`
fontname=$1
fontfile=$2
psfontmap=$3
gsfontmap=$4

Fontmap_line() { # Fontmap_line fontname-prefix fontfile-prefix plane
  echo "/$1$3 ($2$3.pfb) ;" 
}

psfonts_line() { # psfonts_line fontname-prefix fontfile-prefix plane
  echo "$2$3 $1$3"
}

gen_map0() {	# gen_map map_filename map_line_function
  test -f $1 &&
    grep "`$2_line ${fontname} ${fontfile} 01`" $1 > /dev/null
  if [ $? -gt 0 ]; then 
    echo "%" >> $1
    cnt=1
    while [ $cnt -le 9 ]; do
      ($2_line $fontname $fontfile 0$cnt) >> $1
      cnt=`expr $cnt + 1`
    done
    while [ $cnt -le 55 ]; do
      ($2_line $fontname $fontfile $cnt) >> $1
      cnt=`expr $cnt + 1`
    done
  fi
}

if [ -z "$1" ]; then
  echo "usage: $execfile fontname-prefix fontfile-prefix [psfonts.map [Fontmap]]"
  echo "  If the 3rd argument is absent, none appended to psfonts.map."
  echo "  if the 4th argument is absent, none appended to Fontmap."
  echo "  e.g. $execfile FunSong b5fs"
else
  if [ -n "${gsfontmap}" ]; then
    gen_map0 ${gsfontmap} Fontmap
  fi
  if [ -n "${psfontmap}" ]; then
    gen_map0 ${psfontmap} psfonts
  fi
fi
