#!/bin/sh
# This file is part of the CJK package for LaTeX2e.
#
# This file is part of the NTU2CJK package.
#
# $Id: gen_map,v 1.5 1997/06/18 05:23:07 d791013 Exp $
#
# Generate the Fontmap and the psfonts.map typed files. Fontmap-like output
# is temporarily used with Ghostscript, while NTU2CJK is making PostScript
# fonts for CJK package. psfonts.map-like output is used when invoking
# DVIPS. Alternatively, the generated file can be appended to the official
# released Fontmap and psfonts.map to simulate printer resident PS fonts.
#
# Note: you'd better backup the original Fontmap and psfonts.map.

usage () {
  cat << EOU
  Usage:
    $execfile [-p] [-d] [-x extend]
        [-F fontname-prefix] [-f fontfile-prefix]
        [-m psfontmap] [-F gsfontmap]

    -p  font sequential number in hexidecimal format (pmC font encoding).
    -d  (for psfonts.map) setup as downloadable fonts.
    -x  extended font filename prefix.
    -f  fontfile prefix
    -F  fontname prefix
    -m  psfont.map-like output
    -M  Fontmap-like output

  Example:
    $execfile -F FunSong -f b5fs -m CJK.map -M Fontmap
EOU
  exit 2
}

Opts () {
  args=`getopt 'pdx:f:F:m:M:' $*`
  [ $# -lt 1 -o $? -ne 0 ] && usage
  set -- $args
  for arg; do
    case $arg in
      -p) pmc=1; shift;;
      -d) psline=partial_$psline; shift;;
      -x) xfontfile=$2; shift 2;;
      -f) fontfile=$2; shift 2;;
      -F) fontname=$2; shift 2;;
      -m) psfontmap=$2; shift 2;;
      -M) gsfontmap=$2; shift 2;;
      --) shift; break;;
    esac
  done
}

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

partial_psfonts_line() { # partial_psfonts_line name-prefix file-prefix plane
  echo "$2$3 $1$3 <$2$3.pfb"
}

extend_psfonts_line() { 
  echo "$xfontfile$3 $1$3 \"1.2 ExtendFont\""
}

extend_partial_psfonts_line() {
  echo "$xfontfile$3 $1$3 \"1.2 ExtendFont\" <$2$3.pfb"
}

gen_map0() {	# gen_map map_filename map_line_function
  if [ $pmc -eq 0 ]; then
    [ -f $1 ] &&
      grep "`$2_line ${fontname} ${fontfile} 01`" $1 > /dev/null
  else
    [ -f $1 ] &&
      grep "`$2_line ${fontname} ${fontfile} A1`" $1 > /dev/null
  fi
  if [ $? -gt 0 ]; then 
    #echo "%" >> $1
    for cnt in $flist; 
    do
      ($2_line $fontname $fontfile $cnt) >> $1
    done
  fi
}

execfile=`basename $0`
pmc=0

Opts $*

if [ $pmc -eq 1 ]; then
  flist=" \
       A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF \
    B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF \
    C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF \
    D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF \
    E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF \
    F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE"
else
  flist=" \
    01 02 03 04 05 06 07 08 09 10 \
    11 12 13 14 15 16 17 18 19 20 \
    21 22 23 24 25 26 27 28 29 30 \
    31 32 33 34 35 36 37 38 39 40 \
    41 42 43 44 45 46 47 48 49 50 \
    51 52 53 54 55"
fi

# generate Fontmap-like map
if [ -n "$gsfontmap" ]; then
  gen_map0 $gsfontmap Fontmap
fi

# psfonts.map-like map
if [ -n "$psfontmap" ]; then
  if [ -s $psfontmap ]; then
    cp $psfontmap ${psfontmap}.bak
    if [ -z "$xfontfile" ]; then
      grep -v "`psfonts_line ${fontname} ${fontfile} '..'`" \
	${psfontmap}.bak > ${psfontmap}
    else
      grep -v "`psfonts_line ${fontname} ${fontfile} '..'`" ${psfontmap}.bak| \
      grep -v "`psfonts_line ${fontname} ${xfontfile} '..'`" > ${psfontmap}
    fi
  fi
  gen_map0 ${psfontmap} ${psline}psfonts
  [ -n "$xfontfile" ] && gen_map0 ${psfontmap} extend_${psline}psfonts
fi

exit 0
