1#!/bin/sh
2# maketexnam -- find PK and TFM names.
3# Primarily written by Thomas Esser, Karl Berry, and Olaf Weber.
4# Public domain.
5
6version='$Id: mktexnam 34656 2014-07-18 23:38:50Z karl $'
7
8# preferentially use subprograms from our own directory.
9mydir=`echo "$0" | sed 's,/[^/]*$,,'`
10mydir=`cd "$mydir" && pwd`
11PATH="$mydir:$PATH"; export PATH
12
13usage="Usage: $0 NAME [DPI MODE] [DESTDIR].
14  Output the PK, TFM, and MF names for a font NAME.
15
16Report bugs to: tex-k@tug.org
17TeX Live home page: <http://tug.org/texlive/>
18"
19mt_max_args=4
20
21# Common code for all scripts.
22: ${MT_TEXMFMAIN=`kpsewhich --expand-path='$TEXMFMAIN'`}
23: ${MT_MKTEX_OPT=`kpsewhich --format='web2c files' mktex.opt`}
24test -n "$MT_MKTEX_OPT" || MT_MKTEX_OPT="$MT_TEXMFMAIN/web2c/mktex.opt"
25if test ! -f "$MT_MKTEX_OPT"; then
26  echo "$0: Cannot find mktex.opt; check your installation." >&2
27  exit 1
28fi
29
30. "$MT_MKTEX_OPT"
31
32NAME=$1
33case $# in
34  1|2) DPI=$BDPI; DEST=$2;;
35  3|4) DPI=$2; MODE=$3; DEST=$4;;
36    *) help;;
37esac
38
39: ${MT_PKDESTDIR='$MT_DESTROOT/$MT_PKDESTREL'}
40: ${MT_TFMDESTDIR='$MT_DESTROOT/$MT_TFMDESTREL'}
41: ${MT_MFDESTDIR='$MT_DESTROOT/$MT_MFDESTREL'}
42: ${MT_PKBASE='$NAME.${DPI}pk'}
43: ${MT_TFMBASE='$NAME.tfm'}
44: ${MT_MFBASE='$NAME.mf'}
45: ${MT_DEFAULT_SUPPLIER=unknown}
46: ${MT_DEFAULT_TYPEFACE=unknown}
47: ${MT_DEFAULT_NAMEPART='$MT_SUPPLIER/$MT_TYPEFACE'}
48: ${MT_DEFAULT_PKDESTREL='pk/modeless/$MT_NAMEPART'}
49: ${MT_DEFAULT_TFMDESTREL='tfm/$MT_NAMEPART'}
50: ${MT_DEFAULT_MFDESTREL='source/$MT_NAMEPART'}
51: ${MT_DEFAULT_DESTROOT=$KPSE_DOT}
52
53# Find the font: test tfm first, then mf, then possible sauterized mf.
54fullname=`kpsewhich "$NAME.tfm" 2>/dev/null`
55test -z "$fullname" && fullname=`kpsewhich "$NAME.mf" 2>/dev/null`
56if test -z "$fullname"; then
57  rootname=`echo $NAME | sed 's/[0-9]*$//'`
58  fullname=`kpsewhich "b-$rootname.mf" 2>/dev/null`
59  # LH fonts get special treatment:
60  if test -z "$fullname"; then
61    case $rootname in
62      # Czech/Slovak fonts get special treatment:
63      cs*|lcsss*|icscsc*|icstt*|ilcsss*)
64           fullname=`kpsewhich cscode.mf`
65           ;;
66      # LH fonts get special treatment:
67      wn[bcdfirstuv]*|rx[bcdfiorstuvx][bcfhilmostx]|l[abcdhl][bcdfiorstuvx]*)
68           lhprefix=`echo $NAME | sed 's/^\(..\).*/\1/'`
69           fullname=`kpsewhich "${lhprefix}codes.mf" 2>/dev/null`;;
70      *)   fullname=`kpsewhich "$rootname.mf" 2>/dev/null`
71           ;;
72    esac
73  fi
74fi
75
76# After all this, do we _have_ a font?
77if test -z "$fullname"; then
78  : ${MT_DESTROOT="$MT_VARTEXFONTS"}
79else
80  # Normalize $fullname.
81  fullname=`echo "$fullname" | sed 's%//*%/%g'`
82  # See if $fullname comes from a standard location.
83  OIFS=$IFS;IFS=$SEP
84  set x `kpsewhich --expand-path='$TEXMF/fonts'"$SEP$MT_VARTEXFONTS"`
85  shift; IFS=$OIFS
86  for i
87  do
88    test -z "$i" && continue
89    case "$fullname" in
90      $i/*)
91         # We now have a preliminary value for the destination root.
92         : ${MT_DESTROOT="$i"}
93         # When we're done, relfmt contains one of these:
94         # "/source/$MT_NAMEPART/"
95         # "/$MT_NAMEPART/source/"
96         # while namepart contains the actual namepart.
97         relfmt=`echo "$fullname" | \
98           sed -e 's%^'"$i"'\(/.*/\)[^/]*$%\1%' \
99               -e 's%^/tfm/%/source/%' \
100               -e 's%/tfm/$%/source/%'`
101         namepart=`echo "$relfmt" | sed -e 's%/source/%/%'`
102         # See if namepart is empty.
103         if test "$namepart" != "/"; then
104           relfmt=`echo "$relfmt" | \
105             sed -e 's%'"$namepart"'%/$MT_NAMEPART/%'`
106         else
107           # Assume TDS.
108           relfmt='/source/$MT_NAMEPART/'
109           namepart=''
110         fi
111         # Determine supplier and typeface from namepart.  If there is
112         # only one part in the namepart, we take it to be the typeface.
113         MT_SUPPLIER=`echo $namepart | sed 's%^/\([^/]*\)/\(.*\)$%\1%'`
114         MT_TYPEFACE=`echo $namepart | sed 's%^/\([^/]*\)/\(.*\)$%\2%'`
115         if test -z "$MT_TYPEFACE"; then
116           MT_TYPEFACE="$MT_SUPPLIER";
117           MT_SUPPLIER=''
118         fi
119         # Phew.  Now we create the relative paths for pk, tfm and source.
120         : ${MT_NAMEPART='$MT_SUPPLIER/$MT_TYPEFACE'}
121         : ${MT_PKDESTREL=`echo "$relfmt" | sed 's%/source/%/pk/$MT_MODE/%'`}
122         : ${MT_TFMDESTREL=`echo "$relfmt" | sed 's%/source/%/tfm/%'`}
123         : ${MT_MFDESTREL=`echo "$relfmt"`}
124         break
125    esac
126  done
127fi
128
129# In case some variables remain unset.
130: ${MT_SUPPLIER=$MT_DEFAULT_SUPPLIER}
131: ${MT_TYPEFACE=$MT_DEFAULT_TYPEFACE}
132: ${MT_NAMEPART=$MT_DEFAULT_NAMEPART}
133: ${MT_PKDESTREL=$MT_DEFAULT_PKDESTREL}
134: ${MT_TFMDESTREL=$MT_DEFAULT_TFMDESTREL}
135: ${MT_MFDESTREL=$MT_DEFAULT_MFDESTREL}
136
137# We have found nothing, so force using the fontmaps as a last resort.
138# This also means mktexnam can be queried for advice on where to
139# place fonts.
140if test "x$MT_SUPPLIER" = xunknown \
141  || test "x$MT_TYPEFACE" = xunknown; then
142    MT_FEATURES="$MT_FEATURES:fontmaps"
143fi
144
145# Handle the options.
146test -n "$MT_MKTEXNAM_OPT" && . "$MT_MKTEXNAM_OPT"
147
148# Adjust MT_DESTROOT, if necessary.
149if test -n "${MT_DESTROOT}"; then
150  # We distinguish system trees from the rest.
151  systexmf="`kpsewhich --expand-path='{$SYSTEXMF}/fonts'`$SEP$MT_VARTEXFONTS"
152  case "$SEP$systexmf$SEP" in
153    *$SEP$MT_DESTROOT$SEP*) # A system tree.
154      # If the texmfvar option is set, use TEXMFVAR tree.
155      # Some additional uglyness handles the case where $TEXMFVAR exists
156      # but $TEXMFVAR/fonts doesn't (yet).
157      if test "x$USE_TEXMFVAR" = x1; then
158        : ${MT_TEXMFVAR=`kpsewhich --var-value=TEXMFVAR`}
159        MT_DESTROOT=$MT_TEXMFVAR/fonts
160        if test -z "$MT_TEXMFVAR"; then
161          # Path expansion of $TEXMFVAR was empty.
162          FALLBACK=1
163        elif test -d "$MT_DESTROOT"; then
164          # Do we have write access and permission?
165          kpseaccess -w "$MT_DESTROOT" || FALLBACK=1
166          test -w "$MT_DESTROOT" || FALLBACK=1
167        elif test -d "$MT_TEXMFVAR"; then
168          # De we have write access and permission?
169          kpseaccess -w "$MT_TEXMFVAR" || FALLBACK=1
170          test -w "$MT_TEXMFVAR" || FALLBACK=1
171        elif test -e "$MT_TEXMFVAR"; then
172          # TEXMFVAR defined and exists, but it is not a directory?
173          FALLBACK=1
174        else
175          # TEXMFVAR defined but does not exist, try to create it
176          MT_FEATURES=none "$MT_MKTEXDIR" "$MT_TEXMFVAR" || FALLBACK=1
177        fi
178      else
179        # Do we have write access and permission?
180        kpseaccess -w "$MT_DESTROOT" || FALLBACK=1
181        test -w "$MT_DESTROOT" || FALLBACK=1
182      fi
183      # Fall back on VARTEXFONTS if the varfonts option is set, or
184      # we cannot write in the given tree.
185      if test "x$USE_VARTEXFONTS" = x1 || test "x$FALLBACK" = x1; then
186        MT_DESTROOT=$MT_VARTEXFONTS
187      fi ;;
188    *) # A non-system tree.
189      # Do we have write access and permission?
190      kpseaccess -w "$MT_DESTROOT" || FALLBACK=1
191      test -w "$MT_DESTROOT" || FALLBACK=1
192      # If we cannot write fall back of defaults.
193      test "x$FALLBACK" = x1 && MT_DESTROOT=;;
194  esac
195fi
196
197if test -z "$MT_DESTROOT"; then
198  MT_DESTROOT=$MT_DEFAULT_DESTROOT
199  MT_PKDESTDIR=$MT_DEFAULT_DESTROOT
200  MT_TFMDESTDIR=$MT_DEFAULT_DESTROOT
201  MT_MFDESTDIR=$MT_DEFAULT_DESTROOT
202fi
203
204case "$DEST" in
205  "") ;;
206  /* | [A-z]:/*) # Absolute, explicit destdir => use it.
207      MT_PKDESTDIR=$DEST
208      MT_TFMDESTDIR=$DEST
209      MT_MFDESTDIR=$DEST
210      MT_NAMEPART=;;
211   *) # Relative destdir => append to the default.
212      MT_NAMEPART=$DEST;;
213esac
214
215eval MT_MODE=\"$MODE\"
216eval MT_NAMEPART=\"$MT_NAMEPART\"
217eval MT_DESTROOT=\"$MT_DESTROOT\"
218eval MT_PKDESTREL=\"$MT_PKDESTREL\"
219eval MT_TFMDESTREL=\"$MT_TFMDESTREL\"
220eval MT_MFDESTREL=\"$MT_MFDESTREL\"
221eval MT_PKDESTDIR=\"$MT_PKDESTDIR\"
222eval MT_TFMDESTDIR=\"$MT_TFMDESTDIR\"
223eval MT_MFDESTDIR=\"$MT_MFDESTDIR\"
224eval MT_PKNAME=\"$MT_PKDESTDIR/$MT_PKBASE\"
225eval MT_TFMNAME=\"$MT_TFMDESTDIR/$MT_TFMBASE\"
226eval MT_MFNAME=\"$MT_MFDESTDIR/$MT_MFBASE\"
227
228echo "$MT_PKNAME$SEP$MT_TFMNAME$SEP$MT_MFNAME" | sed 's%//*%/%g'
229
230