#!/bin/sh

###############################################################################
# updmap: utility to maintain map files for outline fonts.
#
# Thomas Esser. Public domain.
# Usage: updmap [option] ... [command]
#
# Valid options:
#   --cnffile file             specify configuration file
#   --dvipsoutputdir directory   specify output directory (dvips syntax)
#   --pdftexoutputdir directory  specify output directory (pdftex syntax)
#   --dvipdfmoutputdir directory specify output directory (dvipdfm syntax)
#   --outputdir directory      specify output directory (for all files)
#   --nohash                   do not run texhash
#   --nomkmap                  do not recreate map files
#   --nostop                   do not stop even if a map files does not exist
#   --quiet                    reduce verbosity
#
# Valid commands:
#   --edit                     edit $cnfFileShort file
#   --help                     show this message
#   --showoptions item         show alternatives for options
#   --setoption option value   set option where option is one
#                              of dvipsPreferOutline, LW35, dvipsDownloadBase35,
#                              pdftexDownloadBase14, dvipdfmDownloadBase14,
#                              or kanjiEmbed
#   --setoption option=value   see above, just different syntax
#   --enable maptype mapfile   add or enable a Map, MixedMap or KanjiMap
#   --enable Map=mapfile       add or enable a Map
#   --enable MixedMap=mapfile  add or enable a MixedMap
#   --enable KanjiMap=mapfile  add or enable a KanjiMap
#   --disable mapfile          disable Map, MixedMap or KanjiMap for mapfile
#   --syncwithtrees            entries with unavailable map files will be
#                              disabled in the config file
#   --listmaps                 list all active and inactive maps
#   --listavailablemaps        same as --listmaps, but filter out
#                              unavailable map files
###############################################################################

###############################################################################
# program history:
#   Sa Jan 15 18:13:46 CET 2005
#      avoid multiple variable assignments in one statement
#   Sa Dez 25 12:44:23 CET 2004
#      implementaion adopted for teTeX-3.0 (tcfmgr)
#   Mi Nov  3 16:33:22 CET 2004
#      add "--setoption option=value" syntax
#   Fr Okt 29 21:05:53 CEST 2004
#      add --enable Map=... / --enable MixedMap=...
#   Mi Okt 20 19:17:19 CEST 2004
#      transcript added; even better warnings / error messages
#   So Okt 17 19:07:17 CEST 2004
#      improved warnings / error messages
#      added --syncwithtrees
#      added --listavailablemaps
#   Mi Okt  6 16:37:49 CEST 2004
#      commenty may start with any of *#;%
#   Sun Aug  1 11:42:14 CEST 2004, te
#      remove pdftex related hacks; pdftex no longer needs them!
#   Thu May 13 22:04:23 CEST 2004, te
#      support user with new map file locations
#   Fri Mar 19 15:22:55 CET 2004, te
#      output "xxx yyy" instead of " xxx yyy " (less blanks)
#   Fri Jan  2 22:41:11 CET 2004, te:
#      add version string
#   Wed Aug 20 18:51:47 CEST 2003
#      store map files to fonts/map, add options for dvipsoutputdir,
#      pdftexoutputdir, dvipdfmoutputdir
#   Mon Mar 24 13:40:55 CET 2003
#      fix fmex[789] for dvipdfm (no -r option)
#   Sun Mar 23 18:58:00 CET 2003
#      make the script robust against whitespace in filenames
#   Mon Jan 27 22:38:44 CET 2003
#      don't call a function before processOptions; we may loose our
#      argument list with some broken shells
#   Mon Jan 27 06:55:28 CET 2003
#      fix unportable egrep / sed
#   Sat Jan 18 10:10:26 CET 2003
#      use $tmp8 in dvips2dvipdfm()
#   Thu Jan  2 23:14:34 CET 2003
#      add umask 022, so generated files are always world-readable
#   Sun Oct 27 11:33:04 CET 2002
#      write output in normalized format
#      add support for dvipdfm
#   Mon Sep 22 19:18:57 CEST 2002
#      fix typo: buildin -> builtin
#   Mon Sep  2 19:18:57 CEST 2002
#      fix condensed <-> narrow mapping
#   Sun Sep  1 15:02:28 CEST 2002
#      add dvipsDownloadBase35 / pdftexDownloadBase14 options
#   Wed May 22 20:00:13 CEST 2002
#      listmaps option added by Gerben Wierda
#   Tue May 21 05:27:37 CEST 2002
#      now removing map files before rewriting them; fixes permission problems
#   Tue May 21 05:05:34 CEST 2002:
#      cli options added: showoptions, setoption, enable, disable, nomkmap
#      now even more verbose; now reporting non-existing map files
#      fixing trap for MAC OS/X compatibility
#   Fri May 17 22:38:37 CEST 2002:
#      rewritten from teTeX-1.0 version; Completely new designed.
###############################################################################

test -f /bin/ksh && test -z "$RUNNING_KSH" \
  && { UNAMES=`uname -s`; test "x$UNAMES" = xULTRIX; } 2>/dev/null \
  && { RUNNING_KSH=true; export RUNNING_KSH; exec /bin/ksh $0 ${1+"$@"}; }
unset RUNNING_KSH

test -f /bin/bsh && test -z "$RUNNING_BSH" \
  && { UNAMES=`uname -s`; test "x$UNAMES" = xAIX; } 2>/dev/null \
  && { RUNNING_BSH=true; export RUNNING_BSH; exec /bin/bsh $0 ${1+"$@"}; }
unset RUNNING_BSH

# hack around a bug in zsh:
test -n "${ZSH_VERSION+set}" && alias -g '${1+"$@"}'='"$@"'

# the version string
version=1107552857 # seconds since `00:00:00 1970-01-01 UTC'
                   # date '+%s' (with GNU date)

###############################################################################
# help()
#   display help message and exit
###############################################################################
help()
{
  cat <<eof
Usage: updmap [option] ... [command]

Valid options:
  --cnffile file             specify configuration file
  --dvipsoutputdir directory   specify output directory (dvips syntax)
  --pdftexoutputdir directory  specify output directory (pdftex syntax)
  --dvipdfmoutputdir directory specify output directory (dvipdfm syntax)
  --outputdir directory      specify output directory (for all files)
  --nohash                   do not run texhash
  --nomkmap                  do not recreate map files
  --nostop                   do not stop even if a map files does not exist
  --quiet                    reduce verbosity

Valid commands:
  --edit                     edit $cnfFileShort file
  --help                     show this message
  --showoptions item         show alternatives for options
  --setoption option value   set option where option is one
                             of dvipsPreferOutline, LW35, dvipsDownloadBase35,
                             pdftexDownloadBase14, dvipdfmDownloadBase14,
                             or kanjiEmbed
  --setoption option=value   see above, just different syntax
  --enable maptype mapfile   add or enable a Map, MixedMap or KanjiMap
  --enable Map=mapfile       add or enable a Map
  --enable MixedMap=mapfile  add or enable a MixedMap
  --enable KanjiMap=mapfile  add or enable a KanjiMap
  --disable mapfile          disable Map, MixedMap or KanjiMap for mapfile
  --syncwithtrees            entries with unavailable map files will be
                             disabled in the config file
  --listmaps                 list all active and inactive maps
  --listavailablemaps        same as --listmaps, but filter out
                             unavailable map files
eof
  (exit 0); exit
}

###############################################################################
# verboseMsg(msg)
#   print `msg' to stderr is $verbose is true
###############################################################################
verboseMsg() {
  test -n "$log" && echo ${1+"$@"} >> "$log"
  $verbose && echo ${1+"$@"} >&2
}

###############################################################################
# verboseCat()
#   cat to stderr is $verbose is true
###############################################################################
verboseCat() {
  if $verbose; then
    if test -n "$log"; then
      cat ${1+"$@"} | tee -a "$log" >&2
    else
      cat ${1+"$@"} >&2
    fi
  else
    test -n "$log" && cat ${1+"$@"} >> "$log"
  fi
}

###############################################################################
# warn(msg)
#   print `msg' to stderr
###############################################################################
warn()
{
  test -n "$log" && echo ${1+"$@"} >> "$log"
  echo ${1+"$@"} >&2
}

###############################################################################
# abort(errmsg)
#   print `errmsg' to stderr and exit with error code 1
###############################################################################
abort()
{
  warn "$progname: $1."
  false  # some systems need this to set nonzero $?
  cleanup
  (exit 1); exit 1
}

###############################################################################
# cfgval(variable)
#   read variable ($1) from config file, first match wins
###############################################################################
cfgval()
{
  cat "$cnfFile" | sed -n 's/^'"$1"'[	 =][	 =]*//p' | sed q
}

###############################################################################
# setupSymlinks()
#   set symlink for psfonts.map according to dvipsPreferOutline variable
###############################################################################
setupSymlinks()
{
  rm -f "$dvipsoutputdir/psfonts.map"
  if test "x$dvipsPreferOutline" = xtrue; then
    ln -s psfonts_t1.map "$dvipsoutputdir/psfonts.map"
  else
    ln -s psfonts_pk.map "$dvipsoutputdir/psfonts.map"
  fi

  rm -f "$pdftexoutputdir/pdftex.map"
  if test "x$pdftexDownloadBase14" = xtrue; then
    ln -s pdftex_dl14.map "$pdftexoutputdir/pdftex.map"
  else
    ln -s pdftex_ndl14.map "$pdftexoutputdir/pdftex.map"
  fi
  rm -f "$dvipdfmoutputdir/dvipdfm.map"
  if test "x$dvipdfmDownloadBase14" = xtrue; then
    ln -s dvipdfm_dl14.map "$dvipdfmoutputdir/dvipdfm.map"
  else
    ln -s dvipdfm_ndl14.map "$dvipdfmoutputdir/dvipdfm.map"
  fi
}

###############################################################################
# transLW35(args ...)
#   transform fontname and filenames according to transformation specified
#   by mode
###############################################################################
transLW35()
{
  case $mode in
    ""|URWkb)
      cat ${1+"$@"}
      ;;
    URW)
      fileURW ${1+"$@"}
      ;;
    ADOBE|ADOBEkb)
      psADOBE ${1+"$@"} | file$mode
      ;;
  esac
}

###############################################################################
# catMaps(regex)
#   filter config file by regex for map lines and extract the map filenames.
#   These are then looked up (by kpsewhich in locateMap) and the content of
#   all map files is send to stdout.
###############################################################################
catMaps()
{
  rm -f $catMapsFailed
  egrep "$1" "$cnfFile" \
    | sed 's@#.*@@' \
       | sed "s/@kanjiEmbed@/$kanjiEmbed/" \
       | awk '{print $2}' \
       | sort \
       | uniq \
       | grep . > $tmp4

  while read map; do
    file=`locateMap "$map"` || { touch $catMapsFailed; break; }

    # output the file; also output a newline, because the final newline
    # might be missing in the map file; Empty lines are filtered out later,
    # so it does not really hurt do do this here.
    test -n "$file" && { cat "$file"; echo; }
  done < $tmp4
  if test -f $catMapsFailed; then
    false; return 1
  else
    true; return 0
  fi
}

###############################################################################
# configReplace(file, pattern, line)
#   The first line in file that matches pattern gets replaced by line.
#   line will be added at the end of the file if pattern does not match.
###############################################################################
configReplace()
{
  file=$1; pat=$2; line=$3

  if grep "$pat" "$file" >/dev/null; then
    ed "$file" >/dev/null 2>&1 <<-eof
	/$pat/
	c
	$line
	.
	w
	q
eof
  else
    echo "$line" >> "$file"
  fi
}

###############################################################################
# setOption (option, value)
#   sets option to value in the config file (replacing the existing setting
#   or by adding a new line to the config file).
###############################################################################
setOption()
{
  opt=$1
  val=$2
  case "$opt" in
    LW35)
      case "$val" in
        URWkb|URW|ADOBE|ADOBEkb)
          ;;
        *)
          abort "invalid value $val for option $opt"
          ;;
      esac
      ;;
    dvipsPreferOutline|dvipsDownloadBase35|pdftexDownloadBase14|dvipdfmDownloadBase14)
      case "$val" in
        true|false)
          ;;
        *)
          abort "invalid value $val for option $opt"
      esac
      ;;
    kanjiEmbed)
      ;;
    *)
      abort "unsupported option $opt"
      ;;
  esac

  configReplace "$cnfFile" "^$opt[ 	]" "$opt $val"
}

###############################################################################
# enableMap (type, map)
#   enables an entry in the config file for map with a given type.
###############################################################################
enableMap()
{
  case $1 in
    Map|MixedMap|KanjiMap)
      ;;
    *)
      abort "invalid mapType $1"
      ;;
  esac

  # a map can only have one type, so we carefully disable everything
  # about map here:
  disableMap "$2"

  # now enable with the right type:
  configReplace "$cnfFile" "^#![ 	]*$1[ 	]*$2" "$1 $2"
}

###############################################################################
# disableMap (map)
#   disables map in config file (any type)
###############################################################################
disableMap()
{
  #mapType=`awk '($1 == "MixedMap" || $1 == "Map") && $2 == map { print $1 }' \
  #           map=$1 <"$cnfFile" | sort | uniq`
  map=$1
  mapType=`egrep "^(MixedMap|Map|KanjiMap)[ 	]*$map( |	|$)" "$cnfFile" | awk '{print $1}' | sort | uniq`
  for type in $mapType; do
    configReplace "$cnfFile" "^$type[ 	]*$1" "#! $type $1"
  done
}

###############################################################################
# psADOBE()
#   transform fontnames from URW to Adobe
###############################################################################
psADOBE()
{
  sed \
    -e 's/ URWGothicL-Demi / AvantGarde-Demi /' \
    -e 's/ URWGothicL-DemiObli / AvantGarde-DemiOblique /' \
    -e 's/ URWGothicL-Book / AvantGarde-Book /' \
    -e 's/ URWGothicL-BookObli / AvantGarde-BookOblique /' \
    -e 's/ URWBookmanL-DemiBold / Bookman-Demi /' \
    -e 's/ URWBookmanL-DemiBoldItal / Bookman-DemiItalic /' \
    -e 's/ URWBookmanL-Ligh / Bookman-Light /' \
    -e 's/ URWBookmanL-LighItal / Bookman-LightItalic /' \
    -e 's/ NimbusMonL-Bold / Courier-Bold /' \
    -e 's/ NimbusMonL-BoldObli / Courier-BoldOblique /' \
    -e 's/ NimbusMonL-Regu / Courier /' \
    -e 's/ NimbusMonL-ReguObli / Courier-Oblique /' \
    -e 's/ NimbusSanL-Bold / Helvetica-Bold /' \
    -e 's/ NimbusSanL-BoldCond / Helvetica-Narrow-Bold /' \
    -e 's/ NimbusSanL-BoldItal / Helvetica-BoldOblique /' \
    -e 's/ NimbusSanL-BoldCondItal / Helvetica-Narrow-BoldOblique /' \
    -e 's/ NimbusSanL-Regu / Helvetica /' \
    -e 's/ NimbusSanL-ReguCond / Helvetica-Narrow /' \
    -e 's/ NimbusSanL-ReguItal / Helvetica-Oblique /' \
    -e 's/ NimbusSanL-ReguCondItal / Helvetica-Narrow-Oblique /' \
    -e 's/ CenturySchL-Bold / NewCenturySchlbk-Bold /' \
    -e 's/ CenturySchL-BoldItal / NewCenturySchlbk-BoldItalic /' \
    -e 's/ CenturySchL-Roma / NewCenturySchlbk-Roman /' \
    -e 's/ CenturySchL-Ital / NewCenturySchlbk-Italic /' \
    -e 's/ URWPalladioL-Bold / Palatino-Bold /' \
    -e 's/ URWPalladioL-BoldItal / Palatino-BoldItalic /' \
    -e 's/ URWPalladioL-Roma / Palatino-Roman /' \
    -e 's/ URWPalladioL-Ital / Palatino-Italic /' \
    -e 's/ StandardSymL / Symbol /' \
    -e 's/ NimbusRomNo9L-Medi / Times-Bold /' \
    -e 's/ NimbusRomNo9L-MediItal / Times-BoldItalic /' \
    -e 's/ NimbusRomNo9L-Regu / Times-Roman /' \
    -e 's/ NimbusRomNo9L-ReguItal / Times-Italic /' \
    -e 's/ URWChanceryL-MediItal / ZapfChancery-MediumItalic /' \
    -e 's/ Dingbats / ZapfDingbats /' \
    ${1+"$@"}
}

###############################################################################
# fileADOBEkb()
#   transform filenames from URW to ADOBE (both berry names)
###############################################################################
fileADOBEkb()
{
  sed \
    -e 's/\([^A-Za-z]\)uagd8a.pfb/\1pagd8a.pfb/' \
    -e 's/\([^A-Za-z]\)uagdo8a.pfb/\1pagdo8a.pfb/' \
    -e 's/\([^A-Za-z]\)uagk8a.pfb/\1pagk8a.pfb/' \
    -e 's/\([^A-Za-z]\)uagko8a.pfb/\1pagko8a.pfb/' \
    -e 's/\([^A-Za-z]\)ubkd8a.pfb/\1pbkd8a.pfb/' \
    -e 's/\([^A-Za-z]\)ubkdi8a.pfb/\1pbkdi8a.pfb/' \
    -e 's/\([^A-Za-z]\)ubkl8a.pfb/\1pbkl8a.pfb/' \
    -e 's/\([^A-Za-z]\)ubkli8a.pfb/\1pbkli8a.pfb/' \
    -e 's/\([^A-Za-z]\)ucrb8a.pfb/\1pcrb8a.pfb/' \
    -e 's/\([^A-Za-z]\)ucrbo8a.pfb/\1pcrbo8a.pfb/' \
    -e 's/\([^A-Za-z]\)ucrr8a.pfb/\1pcrr8a.pfb/' \
    -e 's/\([^A-Za-z]\)ucrro8a.pfb/\1pcrro8a.pfb/' \
    -e 's/\([^A-Za-z]\)uhvb8a.pfb/\1phvb8a.pfb/' \
    -e 's/\([^A-Za-z]\)uhvb8ac.pfb/\1phvb8an.pfb/' \
    -e 's/\([^A-Za-z]\)uhvbo8a.pfb/\1phvbo8a.pfb/' \
    -e 's/\([^A-Za-z]\)uhvbo8ac.pfb/\1phvbo8an.pfb/' \
    -e 's/\([^A-Za-z]\)uhvr8a.pfb/\1phvr8a.pfb/' \
    -e 's/\([^A-Za-z]\)uhvr8ac.pfb/\1phvr8an.pfb/' \
    -e 's/\([^A-Za-z]\)uhvro8a.pfb/\1phvro8a.pfb/' \
    -e 's/\([^A-Za-z]\)uhvro8ac.pfb/\1phvro8an.pfb/' \
    -e 's/\([^A-Za-z]\)uncb8a.pfb/\1pncb8a.pfb/' \
    -e 's/\([^A-Za-z]\)uncbi8a.pfb/\1pncbi8a.pfb/' \
    -e 's/\([^A-Za-z]\)uncr8a.pfb/\1pncr8a.pfb/' \
    -e 's/\([^A-Za-z]\)uncri8a.pfb/\1pncri8a.pfb/' \
    -e 's/\([^A-Za-z]\)uplb8a.pfb/\1pplb8a.pfb/' \
    -e 's/\([^A-Za-z]\)uplbi8a.pfb/\1pplbi8a.pfb/' \
    -e 's/\([^A-Za-z]\)uplr8a.pfb/\1pplr8a.pfb/' \
    -e 's/\([^A-Za-z]\)uplri8a.pfb/\1pplri8a.pfb/' \
    -e 's/\([^A-Za-z]\)usyr.pfb/\1psyr.pfb/' \
    -e 's/\([^A-Za-z]\)utmb8a.pfb/\1ptmb8a.pfb/' \
    -e 's/\([^A-Za-z]\)utmbi8a.pfb/\1ptmbi8a.pfb/' \
    -e 's/\([^A-Za-z]\)utmr8a.pfb/\1ptmr8a.pfb/' \
    -e 's/\([^A-Za-z]\)utmri8a.pfb/\1ptmri8a.pfb/' \
    -e 's/\([^A-Za-z]\)uzcmi8a.pfb/\1pzcmi8a.pfb/' \
    -e 's/\([^A-Za-z]\)uzdr.pfb/\1pzdr.pfb/' \
    ${1+"$@"}
}

###############################################################################
# fileURW()
#   transform filenames from URWkb (berry names) to URW (vendor names)
###############################################################################
fileURW()
{
  sed \
    -e 's/\([^A-Za-z]\)uagd8a.pfb/\1a010015l.pfb/' \
    -e 's/\([^A-Za-z]\)uagdo8a.pfb/\1a010035l.pfb/' \
    -e 's/\([^A-Za-z]\)uagk8a.pfb/\1a010013l.pfb/' \
    -e 's/\([^A-Za-z]\)uagko8a.pfb/\1a010033l.pfb/' \
    -e 's/\([^A-Za-z]\)ubkd8a.pfb/\1b018015l.pfb/' \
    -e 's/\([^A-Za-z]\)ubkdi8a.pfb/\1b018035l.pfb/' \
    -e 's/\([^A-Za-z]\)ubkl8a.pfb/\1b018012l.pfb/' \
    -e 's/\([^A-Za-z]\)ubkli8a.pfb/\1b018032l.pfb/' \
    -e 's/\([^A-Za-z]\)ucrb8a.pfb/\1n022004l.pfb/' \
    -e 's/\([^A-Za-z]\)ucrbo8a.pfb/\1n022024l.pfb/' \
    -e 's/\([^A-Za-z]\)ucrr8a.pfb/\1n022003l.pfb/' \
    -e 's/\([^A-Za-z]\)ucrro8a.pfb/\1n022023l.pfb/' \
    -e 's/\([^A-Za-z]\)uhvb8a.pfb/\1n019004l.pfb/' \
    -e 's/\([^A-Za-z]\)uhvb8ac.pfb/\1n019044l.pfb/' \
    -e 's/\([^A-Za-z]\)uhvbo8a.pfb/\1n019024l.pfb/' \
    -e 's/\([^A-Za-z]\)uhvbo8ac.pfb/\1n019064l.pfb/' \
    -e 's/\([^A-Za-z]\)uhvr8a.pfb/\1n019003l.pfb/' \
    -e 's/\([^A-Za-z]\)uhvr8ac.pfb/\1n019043l.pfb/' \
    -e 's/\([^A-Za-z]\)uhvro8a.pfb/\1n019023l.pfb/' \
    -e 's/\([^A-Za-z]\)uhvro8ac.pfb/\1n019063l.pfb/' \
    -e 's/\([^A-Za-z]\)uncb8a.pfb/\1c059016l.pfb/' \
    -e 's/\([^A-Za-z]\)uncbi8a.pfb/\1c059036l.pfb/' \
    -e 's/\([^A-Za-z]\)uncr8a.pfb/\1c059013l.pfb/' \
    -e 's/\([^A-Za-z]\)uncri8a.pfb/\1c059033l.pfb/' \
    -e 's/\([^A-Za-z]\)uplb8a.pfb/\1p052004l.pfb/' \
    -e 's/\([^A-Za-z]\)uplbi8a.pfb/\1p052024l.pfb/' \
    -e 's/\([^A-Za-z]\)uplr8a.pfb/\1p052003l.pfb/' \
    -e 's/\([^A-Za-z]\)uplri8a.pfb/\1p052023l.pfb/' \
    -e 's/\([^A-Za-z]\)usyr.pfb/\1s050000l.pfb/' \
    -e 's/\([^A-Za-z]\)utmb8a.pfb/\1n021004l.pfb/' \
    -e 's/\([^A-Za-z]\)utmbi8a.pfb/\1n021024l.pfb/' \
    -e 's/\([^A-Za-z]\)utmr8a.pfb/\1n021003l.pfb/' \
    -e 's/\([^A-Za-z]\)utmri8a.pfb/\1n021023l.pfb/' \
    -e 's/\([^A-Za-z]\)uzcmi8a.pfb/\1z003034l.pfb/' \
    -e 's/\([^A-Za-z]\)uzdr.pfb/\1d050000l.pfb/' \
    ${1+"$@"}
}

###############################################################################
# fileADOBE()
#   transform filenames from URWkb (berry names) to ADOBE (vendor names)
###############################################################################
fileADOBE()
{
  sed \
    -e 's/\([^A-Za-z]\)uagd8a.pfb/\1agd_____.pfb/' \
    -e 's/\([^A-Za-z]\)uagdo8a.pfb/\1agdo____.pfb/' \
    -e 's/\([^A-Za-z]\)uagk8a.pfb/\1agw_____.pfb/' \
    -e 's/\([^A-Za-z]\)uagko8a.pfb/\1agwo____.pfb/' \
    -e 's/\([^A-Za-z]\)ubkd8a.pfb/\1bkd_____.pfb/' \
    -e 's/\([^A-Za-z]\)ubkdi8a.pfb/\1bkdi____.pfb/' \
    -e 's/\([^A-Za-z]\)ubkl8a.pfb/\1bkl_____.pfb/' \
    -e 's/\([^A-Za-z]\)ubkli8a.pfb/\1bkli____.pfb/' \
    -e 's/\([^A-Za-z]\)ucrb8a.pfb/\1cob_____.pfb/' \
    -e 's/\([^A-Za-z]\)ucrbo8a.pfb/\1cobo____.pfb/' \
    -e 's/\([^A-Za-z]\)ucrr8a.pfb/\1com_____.pfb/' \
    -e 's/\([^A-Za-z]\)ucrro8a.pfb/\1coo_____.pfb/' \
    -e 's/\([^A-Za-z]\)uhvb8a.pfb/\1hvb_____.pfb/' \
    -e 's/\([^A-Za-z]\)uhvb8ac.pfb/\1hvnb____.pfb/' \
    -e 's/\([^A-Za-z]\)uhvbo8a.pfb/\1hvbo____.pfb/' \
    -e 's/\([^A-Za-z]\)uhvbo8ac.pfb/\1hvnbo___.pfb/' \
    -e 's/\([^A-Za-z]\)uhvr8a.pfb/\1hv______.pfb/' \
    -e 's/\([^A-Za-z]\)uhvr8ac.pfb/\1hvn_____.pfb/' \
    -e 's/\([^A-Za-z]\)uhvro8a.pfb/\1hvo_____.pfb/' \
    -e 's/\([^A-Za-z]\)uhvro8ac.pfb/\1hvno____.pfb/' \
    -e 's/\([^A-Za-z]\)uncb8a.pfb/\1ncb_____.pfb/' \
    -e 's/\([^A-Za-z]\)uncbi8a.pfb/\1ncbi____.pfb/' \
    -e 's/\([^A-Za-z]\)uncr8a.pfb/\1ncr_____.pfb/' \
    -e 's/\([^A-Za-z]\)uncri8a.pfb/\1nci_____.pfb/' \
    -e 's/\([^A-Za-z]\)uplb8a.pfb/\1pob_____.pfb/' \
    -e 's/\([^A-Za-z]\)uplbi8a.pfb/\1pobi____.pfb/' \
    -e 's/\([^A-Za-z]\)uplr8a.pfb/\1por_____.pfb/' \
    -e 's/\([^A-Za-z]\)uplri8a.pfb/\1poi_____.pfb/' \
    -e 's/\([^A-Za-z]\)usyr.pfb/\1sy______.pfb/' \
    -e 's/\([^A-Za-z]\)utmb8a.pfb/\1tib_____.pfb/' \
    -e 's/\([^A-Za-z]\)utmbi8a.pfb/\1tibi____.pfb/' \
    -e 's/\([^A-Za-z]\)utmr8a.pfb/\1tir_____.pfb/' \
    -e 's/\([^A-Za-z]\)utmri8a.pfb/\1tii_____.pfb/' \
    -e 's/\([^A-Za-z]\)uzcmi8a.pfb/\1zcmi____.pfb/' \
    -e 's/\([^A-Za-z]\)uzdr.pfb/\1zd______.pfb/' \
    ${1+"$@"}
}

###############################################################################
# locateWeb2c (file ...)
#   apply kpsewhich with format 'web2c files'
###############################################################################
locateWeb2c()
{
  kpsewhich --format='web2c files' ${1+"$@"}
}

###############################################################################
# locateMap (file ...)
#   apply kpsewhich with format 'map'
###############################################################################
locateMap()
{

  # Always use the "migration checking version". This might become
  # consigurable, but for now, we always want to check.
  if :; then
    locateMapMigrate "$@"
    return $?
  fi

  # The old version of the code; not used at the moment.
  for map
  do
    file=`kpsewhich --format=map "$map"`
    if test -f "$file"; then
      verboseMsg "$progname: using map file \`$file'"
      echo "$file"
    else
      warn "map file \`$map' not found."
    fi
  done
}

###############################################################################
# locateMapMigrate (file ...)
#   look for map file; assist user with possibly files in wrong locations;
#   also watch out for possibly hidden conflicts
###############################################################################
locateMapMigrate()
{
  for map
  do
    # look up using the new search path
    file=`kpsewhich --format=map "$map"`

    # look up using the old search path
    file2=`kpsewhich --format='dvips config' "$map"`

    # Well, the search paths should not overlap. If they do, and if
    # file1 = file2, we don't want to complain about file2, so:
    test "x$file" = "x$file2" && file2=

    if test -f "$file"; then

      verboseMsg "$progname: using map file \`$file'"
      echo "$file"

      # we have found the file where we should. If we have additional
      # ones: complain!
      if test -f "$file2"; then
        mapCmp "$file" "$file2" \
          && mapWarn obsoleteCopy "$map" "$file2" \
          || mapWarn manualResolve "$map" "$file" "$file2"
      fi

    else
      if test -f "$file2"; then
        # bad: map file exists in the wrong location only
        mapWarn badLocation "$map" "$file2"
        false; return 1
      else
        # bad: map file does not exist at all
        mapWarn notFound "$map"
        if $stopEnabled; then false; return 1; fi
      fi
    fi
  done
  true; return 0
}

###############################################################################
# mapCmp(file, file2)
#   compare two map files like cmp; but less strict, so changes regarding
#   whitespaces, comments etc. are tolerated. Important is only the
#   return value here.
###############################################################################
mapCmp()
{
  file=$1; file2=$2

  # For reasons of speed only. Avoid normalizeLines if files are 100%
  # the same.
  cmp "$file" "$file2" >/dev/null 2>&1 && return 0

  normalizeLines <"$file" >"$tmpdir/mapCmp-1"
  normalizeLines <"$file2" >"$tmpdir/mapCmp-2"
  cmp "$tmpdir/mapCmp-1" "$tmpdir/mapCmp-2" >/dev/null 2>&1
}

###############################################################################
# mapWarn()
###############################################################################
mapWarn()
{

  case $1 in
  obsoleteCopy)
    warn "
!!! WARNING: Identical copy of used file for \`$2'
    exists in obsolete location
      $3
    Please, consider removing this file.
"
    touch $mapWarnCalled
    ;;
  manualResolve)
    warn "
!!! WARNING: While searching for the right map file for \`$2', we have
    found the file
      $3
    in the right location and we are using this
    file. However, a map file with different content has been found in
    the obsolete location
      $4
    Please, see which version you want to use and eventually update the
    content of the used map file. Additionally, consider removing the
    obsolete file.
"
    touch $mapWarnCalled
    ;;
  badLocation)
    map=$2
    location=$3

    m="
!!! ERROR: The right location for map files has been
    changed for this release and the map file \`$map' has
    not been found in the right location, but in the obsolete
    location
      $location
    instead.

    To fix this, please move this file into an appropriate
    subdirectory of fonts/map in one of your texmf trees.

    For more information about the changed search paths, see
    the release notes section in the teTeX manual. You probably
    can read this document by executing the command
      texdoc TETEXDOC
    else visit the web page
      http://tug.org/texlive/mapenc.html
"
    warn "$m"
    false; return 1
    ;;
  notFound)
    warn "
!!! ERROR! The map file \`$2' has not been found at all.

    Either put this file into the right place or remove the
    reference from the configuration file. An automatic way
    to disable unavailable map files is to call
      $progname --syncwithtrees

    For manual editing, call
      $progname --edit
"

    false; return 1
    ;;
  esac
}

###############################################################################
# cleanup()
#   clean up the temp area and exit with proper exit status
###############################################################################
cleanup()
{
  rc=$?
  $needsCleanup && test -n "$tmpdir" && test -d "$tmpdir" \
    && { cd / && rm -rf "$tmpdir"; }
  (exit $rc); exit $rc
}

###############################################################################
# setupTmpDir()
#   set up a temp directory and a trap to remove it
###############################################################################
setupTmpDir()
{
  $needsCleanup && return

  trap 'cleanup' 1 2 3 7 13 15
  needsCleanup=true
  tmpdir=`mktemp -d "${TMP-/tmp}/$progname.XXXXXX"` \
    || abort "could not create directory \`$tmpdir'"
}

###############################################################################
# showOptions(item)
#   show Options for an item
###############################################################################
showOptions()
{
  item=$1

  case "$item" in
    LW35)
      echo "URWkb URW ADOBE ADOBEkb"
      ;;
    dvipsPreferOutline|pdftexDownloadBase14|dvipdfmDownloadBase14|dvipsDownloadBase35)
      echo "true false"
      ;;
    *)
      abort "Unknown item \"$item\". Choose one of LW35, dvipsPreferOutline, dvipsDownloadBase35, pdftexDownloadBase14 or dvipdfmDownloadBase14"
      ;;
  esac

  (exit 0); exit 0
}

###############################################################################
# mktexdir(args)
#   call mktexdir script, disable all features (to prevent sticky directories)
###############################################################################
mktexdir()
{
  MT_FEATURES=none "$TEXMFMAIN/web2c/mktexdir" "$@" >&2
}

###############################################################################
# setupDestDir()
#   Assign output directories which are not yet assigned. Always use
#   the TEXMFVAR for this.
###############################################################################
setupDestDir()
{
  vartexmf=`kpsewhich -var-value=TEXMFVAR`

  # dvips
  if test -z "$dvipsoutputdir"; then
    dvipsoutputdir=$vartexmf/fonts/map/dvips/updmap
  fi

  # pdftex
  if test -z "$pdftexoutputdir"; then
    pdftexoutputdir=$vartexmf/fonts/map/pdftex/updmap
  fi

  # dvipdfm
  if test -z "$dvipdfmoutputdir"; then
    dvipdfmoutputdir=$vartexmf/fonts/map/dvipdfm/updmap
  fi

  mktexdir "$dvipsoutputdir" "$pdftexoutputdir" "$dvipdfmoutputdir" >/dev/null 2>&1
  for d in "$dvipsoutputdir" "$pdftexoutputdir" "$dvipdfmoutputdir"; do
    test -d "$d" || abort "output directory \`$d' does not exist"
    test -w "$d" || abort "output directory \`$d' is not writable"
  done

}

###############################################################################
# setupLog(cfgFile)
#   try to set up a log file in $TEXMFVAR/web2c
###############################################################################
setupLog()
{
  vartexmf=`kpsewhich -var-value=TEXMFVAR`
  dir=$vartexmf/web2c
  mktexdir "$dir" >/dev/null 2>&1
  log="$dir/$progname.log"
  if test -d "$dir" && test -w "$dir"; then
    rm -f "$log"
    > "$log"
  fi
  if test -w "$log"; then
    date > "$log"
    verboseMsg "$progname: This is $progname, version $version"
    verboseMsg "$progname: using transcript file \`$log'"
  else
    log=
    verboseMsg "$progname: This is $progname, version $version"
    verboseMsg "$progname: no permissions for writing $dir/$progname.log', so no transcript"
  fi
}

###############################################################################
# setupCfgFile()
#   find config file if none specified on cmd line.
###############################################################################
setupCfgFile()
{
  case "$cnfFile" in
    "") cnfFile=`locateWeb2c $cnfFileShort`
        case "$cnfFile" in
          "") abort "config file $cnfFileShort not found"
        esac;;
  esac
  setupLog
}

###############################################################################
# processOptions()
#   process cmd line options
###############################################################################
processOptions()
{
  while
    case $1 in
      --quiet|-q)
          verbose=false;;
      --cnffile)
          cfgparam=1; cnfFile=$2; shift;;
      --cnffile=*)
          cfgparam=1; cnfFile=`echo "$1" | sed 's/--cnffile=//'`;;
      --dvipsoutputdir)
          dvipsoutputdirparam=1; dvipsoutputdir=$2; shift;;
      --dvipsoutputdir=*)
          dvipsoutputdirparam=1; dvipsoutputdir=`echo "$1" | sed 's/--dvipsoutputdir=//'`;;
      --pdftexoutputdir)
          pdftexoutputdirparam=1; pdftexoutputdir=$2; shift;;
      --pdftexoutputdir=*)
          pdftexoutputdirparam=1; pdftexoutputdir=`echo "$1" | sed 's/--pdftexoutputdir=//'`;;
      --dvipdfmoutputdir)
          dvipdfmoutputdirparam=1; dvipdfmoutputdir=$2; shift;;
      --dvipdfmoutputdir=*)
          dvipdfmoutputdirparam=1; dvipdfmoutputdir=`echo "$1" | sed 's/--dvipdfmoutputdir=//'`;;
      --outputdir)
          dvipsoutputdirparam=1; dvipsoutputdir=$2
          pdftexoutputdirparam=1; pdftexoutputdir=$2
          dvipdfmoutputdirparam=1; dvipdfmoutputdir=$2
          shift
          ;;
      --outputdir=*)
          outputdir=`echo "$1" | sed 's/--outputdir=//'`
          dvipsoutputdirparam=1; dvipsoutputdir=$outputdir
          pdftexoutputdirparam=1; pdftexoutputdir=$outputdir
          dvipdfmoutputdirparam=1; dvipdfmoutputdir=$outputdir
          ;;
      --setoption)
          cfgmaint=1
          cmd=setoption
          case $2 in
            *=*)
              setoptionOpt=`echo $2 | sed 's@=.*@@'`
              setoptionVal=`echo $2 | sed 's@[^=]*=@@'`
              shift
            ;;
            *)
              test $# -ge 3 || { abort "--setoption needs two parameters: option value"; }
              setoptionOpt=$2; setoptionVal=$3; shift; shift
            ;;
          esac
          ;;
      --enable)
          cfgmaint=1
          cmd=enable
          case $2 in 
            Map=*)
              enableMapType=Map; enableMapFile=`echo "$2" | sed 's/Map=//'`; shift;;
            MixedMap=*)
              enableMapType=MixedMap; enableMapFile=`echo "$2" | sed 's/MixedMap=//'`; shift;;
            KanjiMap=*)
              enableMapType=KanjiMap; enableMapFile=`echo "$2" | sed 's/KanjiMap=//'`; shift;;
            Map|MixedMap|KanjiMap)
              test $# -ge 3 || { abort "--enable needs two parameters: mapType mapFile"; }
              enableMapType=$2; enableMapFile=$3; shift; shift;;
            *)
              abort "--enable needs two parameters mapType mapFile";;
          esac
          ;;
      --syncwithtrees)
          cfgmaint=1
          cmd=syncwithtrees;;
      --disable)
          cfgmaint=1
          cmd=disable; disableMapFile=$2; shift
          ;;
      --disable=*)
          cfgmaint=1
          disableMapFile=`echo "$1" | sed 's/--disable=//'`
          cmd=disable
          ;;
      --edit|-e)
          cfgmaint=1
          cmd=edit;;
      --listmaps|-l)
          cmd=listmaps;;
      --listavailablemaps)
          cmd=listavailablemaps;;
      --showoptions)
          cmd=showoptions; showoptionsItem=$2; shift;;
      --showoptions=*)
          cmd=showoptions; showoptionsItem=`echo "$1" | sed 's/--showoptions=//'`;;
      --nohash)
          texhashEnabled=false;;
      --nomkmap)
          mkmapEnabled=false;;
      --nostop)
          stopEnabled=false;;
      --help|-help|-h)
          help;;
      --version)
          echo "$progname version $version."; (exit 0); exit 0;;
      "") break;;
      *) abort "$progname: unknown option \`$1'. Try $progname --help for help";;
    esac
  do test $# -gt 0 && shift; done

  if test -n "$cfgparam"; then
    if test -z "$cnfFile" || test ! -f "$cnfFile"; then
      abort "config file \`$cnfFileShort' not found"
    fi
  fi

  if test -n "$dvipdoutputdirparam"; then
    if test -z "$dvipdoutputdir" || test ! -d "$dvipdoutputdir"; then
      abort "dvips output directory \`$1' not found"
    fi
  fi
  if test -n "$pdftexoutputdirparam"; then
    if test -z "$pdftexoutputdir" || test ! -d "$pdftexoutputdir"; then
      abort "pdftex output directory \`$1' not found"
    fi
  fi
  if test -n "$dvipdfmoutputdirparam"; then
    if test -z "$dvipdfmoutputdir" || test ! -d "$dvipdfmoutputdir"; then
      abort "dvipdfm output directory \`$1' not found"
    fi
  fi

}

###############################################################################
# listMaps()
#   list all maps mentioned in the config file
###############################################################################
listMaps()
{
  egrep '^(#! *)?(Mixed|Kanji)?Map' $cnfFile
}

###############################################################################
# listAvailableMaps()
#   list maps mentioned in the config file if they are available
###############################################################################
listAvailableMaps()
{
  # loop over each possible line in the config file (-> $line)
  # extract the name of the map file (-> $m)
  # check if the map file exists and in that case echo the line
  OLDIFS=$IFS
  IFS='
'
  for line in `egrep '^(#! *)?(Mixed|Kanji)?Map' $cnfFile`; do
    m=`echo "$line" | sed 's@[ 	]*$@@; s@.*[ 	]@@'`
    kpsewhich --format=map "$m" >/dev/null && echo "$line"
  done
  IFS=$OLDIFS
}

###############################################################################
# syncWithTrees()
#   update the config file: uncomment all lines which refer to map files that
#   are unavailabe in the texmf trees
###############################################################################
syncWithTrees()
{
  kanjiEmbed=`cfgval kanjiEmbed`
  for i in `egrep '^(Mixed|Kanji)?Map' "$cnfFile" | sed 's@.* @@'`; do
    kpsewhich --format=map `echo "$i" | sed "s/@kanjiEmbed@/$kanjiEmbed/"` >/dev/null || echo "$i"
  done > $tmp1
  {
    sed 's@/@\\/@g; s@^@/^MixedMap[ 	]*@; s@$@$/s/^/#! /@' <$tmp1
    sed 's@/@\\/@g; s@^@/^KanjiMap[ 	]*@; s@$@$/s/^/#! /@' <$tmp1
    sed 's@/@\\/@g; s@^@/^Map[ 	]*@; s@$@$/s/^/#! /@' <$tmp1
  } > $tmp2
  sed -f $tmp2 "$cnfFile" > $tmp3 && cat $tmp3 > "$cnfFile"
}

###############################################################################
# normalizeLines()
#   remove comments, whitespace is exactly one space, no empty lines,
#   no whitespace at end of line, one space before and after "
###############################################################################
normalizeLines()
{
  sed \
      -e '/^[*#;%]/d' \
      -e 's@[ 	][ 	]*@ @g' \
      -e '/^ *$/d' \
      -e 's@ $@@' \
      -e 's@ *" *@ " @g' \
      -e 's@" \([^"]*\) "@"\1"@g' | sort | uniq
}

###############################################################################
# dvips2dvipdfm()
#   reads from stdin, writes to stdout. It transforms "dvips"-like syntax into
#   "dvipdfm"-like syntax. It is a very ugly hack.
###############################################################################
dvips2dvipdfm()
{
  sed -e 's@$@ %@' \
      -e 's@^\(\([^ ]*\).*\)@\1\2@' \
      -e 's@\(.*<\[* *\([^ ]*\)\.enc\(.*\)\)@\1 \2@' \
      -e '/%[^ ]*$/s@$@ default@' \
      -e 's@\(.*<<* *\([^ ]*\)\.pf[ab].*\)@\1 \2@' \
      -e '/%[^ ]* [^ ]*$/s@\( \([^ ]*\).*\)$@\1 \2@' \
      -e 's@\(.*[" ]\([.0-9-][.0-9-]*\) *ExtendFont.*\)@\1 -e \2@' \
      -e 's@\(.*[" ]\([.0-9-][.0-9-]*\) *SlantFont.*\)@\1 -s \2@' \
      -e 's@.*%@@' |
  awk '$1 == $3 && $2 == "default" {$2=""; $3=""} {print}' > $tmp8

  egrep '^(cm|eu|lasy|lcircle|lcmss|line|msam|xy)' $tmp8 \
    | sed 's@$@ -r@; s@\(fmex[789]\) -r$@\1@'
  egrep -v '^(cm|eu|lasy|lcircle|lcmss|line|msam|xy)' $tmp8
}

###############################################################################
# cidx2dvips()
#   reads from stdin, writes to stdout. It transforms "cid-x"-like syntax into
#   "dvips"-like syntax.
###############################################################################
cidx2dvips()
{
  sed -e 's/,BoldItalic/ -s .3/' \
      -e 's/,Bold//' \
      -e 's/,Italic/ -s .3/' \
      -e '/.*[@:/,]/d' \
      -e '/^[^ ][^ ]* unicode /d' \
      -e 's/^\([^ ][^ ]* [^ ][^ ]* [^ ][^ ]*\)\.[Oo][Tt][Ff]/\1/' \
      -e 's/^\([^ ][^ ]* [^ ][^ ]* [^ ][^ ]*\)\.[Tt][Tt][FfCc]/\1/' \
      -e 's/$/ %/' \
      -e 's/^\(\([^ ]*\).*\)/\1\2/' \
      -e 's/^\([^ ][^ ]* \([^ ][^ ]*\) !*\([^ ][^ ]*\).*\)/\1 \3-\2/' \
      -e 's/^\(.* -e \([.0-9-][.0-9-]*\).*\)/\1 "\2 ExtendFont"/' \
      -e 's/^\(.* -s \([.0-9-][.0-9-]*\).*\)/\1 "\2 SlantFont"/' \
      -e 's/.*%//'
}


###############################################################################
# mkMaps()
#   the main task of this script: create the output files
###############################################################################
mkMaps()
{
  newline='
'
  mode=`cfgval LW35`
  dvipsPreferOutline=`cfgval dvipsPreferOutline`
  dvipsDownloadBase35=`cfgval dvipsDownloadBase35`
  pdftexDownloadBase14=`cfgval pdftexDownloadBase14`
  dvipdfmDownloadBase14=`cfgval dvipdfmDownloadBase14`
  kanjiEmbed=`cfgval kanjiEmbed`

  # defaults
  test -z "$mode" && mode=URWkb
  test -z "$dvipsPreferOutline" && dvipsPreferOutline=true
  test -z "$dvipsDownloadBase35" && dvipsDownloadBase35=false
  test -z "$pdftexDownloadBase14" && pdftexDownloadBase14=false
  test -z "$dvipdfmDownloadBase14" && dvipdfmDownloadBase14=false

  outputFiles="$dvipsoutputdir/download35.map
$dvipsoutputdir/builtin35.map
$dvipsoutputdir/psfonts_t1.map
$dvipsoutputdir/psfonts_pk.map
$pdftexoutputdir/pdftex_dl14.map
$pdftexoutputdir/pdftex_ndl14.map
$dvipdfmoutputdir/dvipdfm_dl14.map
$dvipdfmoutputdir/dvipdfm_ndl14.map
$dvipdfmoutputdir/kanjix.map
$dvipsoutputdir/ps2pk.map"

  outputLinks="$dvipsoutputdir/psfonts.map
$pdftexoutputdir/pdftex.map
$dvipdfmoutputdir/dvipdfm.map"

  allOutput="$outputFiles
$outputLinks"

  notfound=false
  OLDIFS=$IFS; IFS=$newline
  for o in $allOutput; do
    test -f "$o" || notfound=true
  done
  IFS=$OLDIFS
  $notfound || texhashEnabled=false

  verboseCat <<-eof

	updmap is creating new map files using the following configuration:

	  config file: \`$cnfFile'
	  dvips output directory: \`$dvipsoutputdir'
	  pdftex output directory: \`$pdftexoutputdir'
	  dvipdfm output directory: \`$dvipdfmoutputdir'

	  prefer outlines: \`$dvipsPreferOutline'
	  texhash enabled: \`$texhashEnabled'
	  download standard fonts (dvips): \`$dvipsDownloadBase35'
	  download standard fonts (pdftex): \`$pdftexDownloadBase14'
	  download standard fonts (dvipdfm): \`$dvipdfmDownloadBase14'
eof

  verboseMsg
  verboseMsg "$progname: Scanning for LW35 support files"
  dvips35=`locateMap dvips35.map` || cleanup
  pdftex35=`locateMap pdftex35.map` || cleanup
  dvipdfm35=`locateMap dvipdfm35.map` || cleanup
  ps2pk35=`locateMap ps2pk35.map` || cleanup

  verboseMsg
  verboseMsg "$progname: Scanning for MixedMap entries:"
  catMaps '^MixedMap' > $tmp1 || cleanup

  verboseMsg
  verboseMsg "$progname: Scanning for Map entries:"
  catMaps '^Map' > $tmp2 || cleanup
  verboseMsg

  verboseMsg
  verboseMsg "$progname: Scanning for KanjiMap entries:"
  catMaps '^KanjiMap' | normalizeLines > $tmpkanji1 || cleanup
  cidx2dvips < $tmpkanji1 > $tmpkanji2 || cleanup
  verboseMsg

  # files should be world-readable
  umask 022

  OLDIFS=$IFS; IFS=$newline
  for file in $allOutput; do
    rm -f "$file"
    cat > "$file" <<-eof
	% $file: maintained by the script updmap.
	%   Don't change this file directly. Edit texmf/web2c/$cnfFileShort
	%   and run updmap to recreate this file.
eof
  done
  IFS=$OLDIFS

  verboseMsg "$progname: Generating output for ps2pk..."
  { transLW35 "$ps2pk35"; cat $tmp1 $tmp2; } \
    | normalizeLines >> "$dvipsoutputdir/ps2pk.map"

  verboseMsg "$progname: Generating output for dvips..."
  { transLW35 "$ps2pk35"; } \
    | normalizeLines >> "$dvipsoutputdir/download35.map"
  { transLW35 "$dvips35"; } \
    | normalizeLines >> "$dvipsoutputdir/builtin35.map"

  if test "x$dvipsDownloadBase35" = xtrue; then
    dftdvips=$ps2pk35
  else
    dftdvips=$dvips35
  fi
  { transLW35 "$dftdvips"; cat $tmp1 $tmp2 $tmpkanji2; } \
    | normalizeLines >> "$dvipsoutputdir/psfonts_t1.map"
  { transLW35 "$dftdvips"; cat $tmp2 $tmpkanji2; } \
    | normalizeLines >> "$dvipsoutputdir/psfonts_pk.map"

  cat $tmpkanji1 >> "$dvipdfmoutputdir/kanjix.map"

  verboseMsg "$progname: Generating output for pdftex..."
  # remove PaintType due to Sebastian's request
  { transLW35 "$pdftex35"; cat $tmp1 $tmp2; } \
    | grep -v PaintType | grep . > $tmp3
  { transLW35 "$dvipdfm35"; cat $tmp1 $tmp2; } \
    | grep -v PaintType | grep . > $tmp6
  { transLW35 "$ps2pk35"; cat $tmp1 $tmp2; } \
    | grep -v PaintType | grep . > $tmp7

  <$tmp3 normalizeLines >> "$pdftexoutputdir/pdftex_ndl14.map"
  <$tmp7 normalizeLines >> "$pdftexoutputdir/pdftex_dl14.map"

  verboseMsg "$progname: Generating output for dvipdfm..."
  <$tmp7 normalizeLines | dvips2dvipdfm | normalizeLines >> "$dvipdfmoutputdir/dvipdfm_dl14.map"
  <$tmp6 normalizeLines | dvips2dvipdfm | normalizeLines >> "$dvipdfmoutputdir/dvipdfm_ndl14.map"
  verboseMsg "$progname: All output generated!"
  verboseMsg

  setupSymlinks
  $texhashEnabled && { $verbose && texhash || texhash >/dev/null 2>&1; }

  verboseMsg
  verboseMsg "$progname: Files generated:"
  OLDIFS=$IFS; IFS=$newline
  ls -l $outputFiles | verboseCat
  IFS=$OLDIFS
  verboseMsg

  verboseMsg "$progname: Map file links:"
  { cd "$dvipsoutputdir"; ls -l psfonts.map;
    cd "$pdftexoutputdir"; ls -l pdftex.map;
    cd "$dvipdfmoutputdir"; ls -l dvipdfm.map; } \
      | awk '{print $(NF-2), $(NF-1), $NF}' | verboseCat
  verboseMsg

  if test -f $mapWarnCalled; then
    m="

!!! NOTICE:

    With this release, the search paths for map files have been changed
    and we have found that some files exist in the new path as well as
    in the obsolete path.

    This is not an error per se, but please consider removing duplicates
    from the old location and search the above output"

    test -n "$log" && m="$m or the transcript file
      $log
   "

    m="$m for warnings.

    For more information about the changed search paths, see
    the release notes section in the teTeX manual. You probably
    can read this document by executing the command
      texdoc TETEXDOC
    else visit the web page
      http://tug.org/texlive/mapenc.html
"
    warn "$m"
  fi
  return 0
}

###############################################################################
# main()
#   execution starts here
###############################################################################
main()
{
  # initialize global variables
  progname=updmap
  cmd=
  log=

  cfgparam=
  outputdirparam=
  cfgmaint=
  texhashEnabled=true
  mkmapEnabled=true
  stopEnabled=true
  verbose=true
  needsCleanup=false

  cnfFileShort=updmap.cfg
  cnfFile=
  dvipsoutputdir=; pdftexoutputdir=; dvipdfmoutputdir=
  : ${TEXMFMAIN=`kpsewhich -var-value=TEXMFMAIN`}

  setupTmpDir;
  tmp1=$tmpdir/a
  tmp2=$tmpdir/b
  tmp3=$tmpdir/c
  tmp4=$tmpdir/d
  tmp5=$tmpdir/e
  tmp6=$tmpdir/f
  tmp7=$tmpdir/g
  tmp8=$tmpdir/h
  tmpkanji1=$tmpdir/ka
  tmpkanji2=$tmpdir/kb
  mapWarnCalled=$tmpdir/mapWarnCalled
  catMapsFailed=$tmpdir/catMapsFailed

  processOptions ${1+"$@"}

  case "$cmd" in
    showoptions) showOptions "$showoptionsItem"; (exit 0); exit;;
    help)        help; (exit 0); exit;;
  esac

  setupCfgFile
  case "$cmd" in
    listmaps) listMaps; (exit 0); exit;;
    listavailablemaps) listAvailableMaps; (exit 0); exit;;
  esac

  # keep a copy of config file, so that we can see if the file was modified
  setupTmpDir

  if test -n "$cfgmaint"; then
    if test -z "$cfgparam"; then
      co=`$TEXMFMAIN/texconfig/tcfmgr --tmp $tmpdir --cmd co --file $cnfFileShort`
      test $? = 0 || cleanup
      set x $co; shift
      id=$1; cnfFile=$3; orig=$4
      verboseMsg "$progname: initial config file is \`$orig'"
    else
      verboseMsg "$progname: config file is \`$cnfFile'"
      cp "$cnfFile" $tmp5
    fi

    case "$cmd" in
      edit)
        ${VISUAL-${EDITOR-vi}} $cnfFile;;
      setoption)
        setOption "$setoptionOpt" "$setoptionVal";;
      enable)
        enableMap "$enableMapType" "$enableMapFile";;
      disable)
        disableMap "$disableMapFile";;
      syncwithtrees)
        syncWithTrees;;
    esac

    unchanged=true
    if test -z "$cfgparam"; then
      ci=`$TEXMFMAIN/texconfig/tcfmgr --tmp $tmpdir --cmd ci --id $id`
      test $? = 0 || cleanup
      case "$ci" in
        "") : ;;
        *) unchanged=false
           cnfFile=$ci
           ;;
      esac
    else
      cmp "$cnfFile" $tmp5 >/dev/null 2>&1 || unchanged=false
    fi
    case $unchanged in
      true) if $mkmapEnabled; then
              verboseMsg "$progname: configuration (updmap.cfg) unchanged. Map files will not be recreated."
              mkmapEnabled=false
            else
              verboseMsg "$progname: configuration (updmap.cfg) unchanged."
            fi;;
      *)
           verboseMsg "$progname: configuration file updated: \`$cnfFile'";;
    esac
  fi

  $mkmapEnabled || return
  setupDestDir
  mkMaps
}

main ${1+"$@"}
cleanup
