summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/texlive-extra/texconfig.sh
blob: 07c7ac05c4c5256fbab7b5293133be44e6f45b36 (plain)
1
2
3
4
5
6
7
8
9
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
#!/bin/sh
# $Id$
# texconfig version 3.0
# Originally written by Thomas Esser. Public domain.
# Now maintained as part of TeX Live; correspondence to tex-live@tug.org.

# invoke the right shell:

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+"$@"}'='"$@"'

# preferentially use subprograms from our own directory.
mydir=`echo "$0" | sed 's,/[^/]*$,,'`
mydir=`cd "$mydir" && pwd`
PATH="$mydir:$PATH"; export PATH

# initializations...
progname=texconfig

# the version string
version='$Id$'

envVars="
  AFMFONTS BIBINPUTS BSTINPUTS CMAPFONTS CWEBINPUTS ENCFONTS GFFONTS
  GLYPHFONTS INDEXSTYLE LIGFONTS MFBASES MFINPUTS MFPOOL MFTINPUTS
  MISCFONTS MPINPUTS MPMEMS MPPOOL MPSUPPORT OCPINPUTS OFMFONTS
  OPENTYPEFONTS OPLFONTS OTPINPUTS OVFFONTS OVPFONTS PDFTEXCONFIG PKFONTS
  PSHEADERS SFDFONTS T1FONTS T1INPUTS T42FONTS TEXBIB TEXCONFIG TEXDOCS
  TEXFONTMAPS TEXFONTS TEXFORMATS TEXINDEXSTYLE TEXINPUTS TEXMFCNF
  TEXMFDBS TEXMFINI TEXMFSCRIPTS TEXPICTS TEXPKS TEXPOOL TEXPSHEADERS
  TEXSOURCES TFMFONTS TRFONTS TTFONTS VFFONTS WEB2C WEBINPUTS
"
tmpdir=${TMPDIR-${TEMP-${TMP-/tmp}}}/tctmp.$$
needsCleanup=false
lastUpdatedFile=

# 
###############################################################################
# setupFMT(void) - find a suitable version of fmt / adjust
#
setupFMT()
{
  case $FMT in
    "") 
      FMT=fmt
      test ! -x /bin/fmt && test ! -f /usr/bin/fmt &&
        { test -x /bin/adjust || test -x /usr/bin/adjust; } && FMT=adjust
      ;;
    *)
      return
      ;;
  esac
}

###############################################################################
# myFmt(args) - run $FMT
#
myFmt()
{
  setupFMT
  $FMT ${1+"$@"}
}

###############################################################################
# echoShowVariable(args ...)
#   show environment variables which names are as args and their values
#
echoShowVariable()
{
  for esv
  do
    var=$esv
    eval val=\"\${$var+=}\${$var- is unset}\"
    echo "$var$val"
  done | grep -v 'is unset$'
}

###############################################################################
# echoShowKpseVariable(args ...)
#   show kpathsea variables which names are as args and their values
#
echoShowKpseVariable()
{
  for eskv
  do
    var=$eskv
    val=`kpsewhich -var-value="$eskv"`
    echo "$var=$val"
  done
}

###############################################################################
# echoLocateBinary(args ...) - show where programs actually exist
#
echoLocateBinary()
{
  for elb
  do
    elbLoc=`checkForBinary "$elb"`
    if test -n "$ELB_PATH_ONLY"; then
      test -n "$elbLoc" && echo "$elbLoc"
    else
      case $elbLoc in
        "") echo "$elb: not found";;
        *) echo "$elb: $elbLoc";;
      esac
    fi
  done
}

###############################################################################
# echoLocateCfgfile(args ...) - show where files actually exist
#
echoLocateCfgfile()
{
  for elc
  do
    case $elc in
      texmf.cnf) elcLoc=`kpsewhich $elc`;;
      *) elcLoc=`tcfmgr --cmd find --file "$elc"`;;
    esac
    case $elcLoc in
      "") echo "$elc: not found";;
      *)  echo "$elcLoc";;
    esac
  done
}

###############################################################################
# checkForBinary(prog) - echo full path of prog
#
checkForBinary()
{
  cfbBinary=$1

  OLDIFS=$IFS
  IFS=:
  set x `echo "$PATH" | sed 's/^:/.:/; s/:$/:./; s/::/:.:/g'`; shift
  found=false
  for pathElem
  do
    case $pathElem in
      "") continue;;
      *) test -f "$pathElem/$cfbBinary" && { echo "$pathElem/$cfbBinary"; found=true; break; }
    esac
  done
  IFS=$OLDIFS
  case $found in
    true) (exit 0); return 0;;
    false) (exit 1); return 1;;
  esac
}

###############################################################################
# cleanup() - clean up the temp area and exit with proper exit status
#
cleanup()
{
  rc=$1
  $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()
{
  case $needsCleanup in
    true) return;;
  esac

  trap 'cleanup 1' 1 2 3 7 13 15
  needsCleanup=true
  (umask 077; mkdir "$tmpdir") \
    || abort "could not create directory \`$tmpdir'"
}

###############################################################################
# setupTexmfroot() - get value for MT_TEXMFROOT (with caching)
#
setupTexmfroot()
{
  case $MT_TEXMFROOT in
    "") MT_TEXMFROOT=`kpsewhich -var-value=TEXMFROOT`;;
    *) return;;
  esac
}


###############################################################################
# setupTexmfmain() - get value for MT_TEXMFMAIN (with caching)
#
setupTexmfmain()
{
  case $MT_TEXMFMAIN in
    "") MT_TEXMFMAIN=`kpsewhich -var-value=TEXMFMAIN`;;
    *) return;;
  esac
}

###############################################################################
# setupTexmfmain() - get value for MT_TEXMFDIST (with caching)
#
setupTexmfdist()
{
  case $MT_TEXMFDIST in
    "") MT_TEXMFDIST=`kpsewhich -var-value=TEXMFDIST`;;
    *) return;;
  esac
}

###############################################################################
# setupTexmfvar() - get value for MT_TEXMFVAR (with caching)
#
setupTexmfvar()
{
  case $MT_TEXMVAR in
    "") MT_TEXMVAR=`kpsewhich -var-value=TEXMFVAR`;;
    *) return;;
  esac
}

###############################################################################
# setupSystexmf() - get value for MT_SYSTEXMF (with caching)
#
setupSystexmf()
{
  case $MT_SYSTEXMF in
    "") MT_SYSTEXMF=`kpsewhich -var-value=SYSTEXMF`;;
    *) return;;
  esac
}

###############################################################################
# abort(errmsg)
#   print `errmsg' to stderr and exit with error code 1
#
abort()
{
  echo "$progname: $1." >&2
  cleanup 1
}

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

###############################################################################
# tcfmgr(args) - call tcfmgr script
#
tcfmgr()
{
  setupTexmfmain
  "$MT_TEXMFMAIN/texconfig/tcfmgr" "$@"
}

###############################################################################
# mktexupd(args) - call mktexupd script
#
mktexupd()
{
  setupTexmfmain
  "$MT_TEXMFMAIN/web2c/mktexupd" "$@"
}

###############################################################################
# getRelDir(file)
#   matches file against SYSTEXMF. Returns relative directory of file within
#   a texmf tree in variable relPart.
#
getRelDir()
{
  file=$1
  relPart=

  setupSystexmf
  OLDIFS=$IFS
  IFS='
'
  set x `echo "$MT_SYSTEXMF" | tr : '
'`; shift
  IFS=$OLDIFS

  # now loop over all components of SYSTEXMF
  for dir
  do
    test -n "$dir" || continue
    case "$file" in
      $dir/*)
        relPart=`echo "$file" | sed "s%$dir/*%%"`
        break
        ;;
    esac
  done

  # now check for success / failure
  case $relPart in
    ""|$file)
      # empty or full filename -> getRelDir failed!
      (exit 1); return 1
      ;;
    *)
      # relPart should just have the "dirname" part:
      relPart=`echo "$relPart" | sed 's%/*[^/]*$%%'`
      (exit 0); return 0
      ;;
  esac
}

###############################################################################
# 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()
{
  configReplaceFile=$1; configReplacePat=$2; configReplaceLine=$3

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

###############################################################################
# fmgrConfigReplace (file regex value)
#   replaces line matching regex by value in file
#
fmgrConfigReplace()
{
  fmgrConfigReplaceChanged=false

  moreArgs=""
  while
    case $1 in
      --*) moreArgs="$moreArgs $1 $2";;
      *) break;;
    esac
  do shift; shift; done
  fmgrConfigReplaceFile=$1
  fmgrConfigReplaceRegex=$2
  fmgrConfigReplaceValue=$3

  setupTmpDir
  co=`tcfmgr $moreArgs --tmp $tmpdir --cmd co --file $fmgrConfigReplaceFile`
  if test $? != 0; then
    echo "$progname: fmgrConfigReplace co failed for \`$fmgrConfigReplaceFile'" >&2
    (exit 1); return 1
  fi
  set x $co; shift
  fmgrConfigReplaceID=$1; fmgrConfigReplaceCfgFile=$3; fmgrConfigReplaceOrigFile=$4
  configReplace "$fmgrConfigReplaceCfgFile" "$fmgrConfigReplaceRegex" "$fmgrConfigReplaceValue"
  ci=`tcfmgr --tmp $tmpdir --cmd ci --id "$fmgrConfigReplaceID"`
  if test $? != 0; then
    echo "$progname: fmgrConfigReplace ci failed for \`$fmgrConfigReplaceFile'" >&2
    (exit 1); return 1
  fi
  case $ci in
    "") :;;
    $lastUpdatedFile)
      fmgrConfigReplaceChanged=true;;
    *) echo "$progname: updated configuration saved as file \`$ci'" >&2
       fmgrConfigReplaceChanged=true
       lastUpdatedFile=$ci;;
  esac
  (exit 0); return 0
}

###############################################################################
# setupModesMfFile(void) - find modes.mf file (with caching)
#
setupModesMfFile()
{
  case $modesMfFile in
    "")
      modesMfFile=`tcfmgr --cmd find --file modes.mf`
      ;;
    *)
      return
      ;;
  esac
}

###############################################################################
# locateConfigPsFile(void) - find config.ps file (with caching)
#
locateConfigPsFile()
{
  case $configPsFile in
    "")
      configPsFile=`tcfmgr --cmd find --file config.ps`
      ;;
    *)
      return
      ;;
  esac
}

###############################################################################
# listMfModes(file) - list modes from modes.mf file
#
listMfModes()
{
  grep mode_def "$modesMfFile" |
  sed -e "s/mode_def //" \
      -e "s/ .*%[^ ]* / '/" \
      -e "s/\$/' /" |
  egrep -v "^(help|%)" | sort
}

###############################################################################
# listDvipsPapers(void) - list paper definitions from config.ps
#
listDvipsPapers()
{
  grep '@ ' $configPsFile | sed "s/..//;s/ / '/;s/\$/' /"
}

###############################################################################
# getFormatsForHyphen(void)
#   list all formats which have customizable hyphenation
#
getFormatsForHyphen()
{
  fmtutil --catcfg | awk '$3 != "-" {print $1}' | sort
}

###############################################################################
# getRes(mode) - print resolution (both X and Y axis) to metafont mode
#
getRes()
{
  getResMode=$1
  (
    cd $tmpdir
    cat >mftmp.mf <<-'eof'
	let myexit = primitive_end_;
	mode_setup;
	string xdpi;
	xdpi := decimal round pixels_per_inch;
	message "XDPI = " & xdpi;
	string ydpi;
	ydpi := decimal round (pixels_per_inch * aspect_ratio);
	message "YDPI = " & ydpi;
	fontmaking := 0;
	myexit;
eof
    mf '\mode='"$getResMode"';  \input ./mftmp' </dev/null \
     | awk '$1 == "XDPI" || $1 == "YDPI" { print $3 }'
  )
}

###############################################################################
# checkElemInList(elem, list)
#   check if element exists in list
###############################################################################
checkElemInList()
{
  checkElemInListElem=$1; shift
  checkElemInListFound=false
  for checkElemInListIter
  do
    case "x$checkElemInListElem" in
      x$checkElemInListIter)
        checkElemInListFound=true
        break
        ;;
    esac
  done
  case $checkElemInListFound in
    true) (exit 0); return 0;;
  esac
  (exit 1); return 1
}


# show version information from the distribution, if we have any.
showDistVersionInfo()
{
  # TeX Live file.
  setupTexmfroot
  test -f $MT_TEXMFROOT/release-texlive.txt \
  && sed 1q $MT_TEXMFROOT/release-texlive.txt

  # no harm in continuing to look for the teTeX files.
  test -f $MT_TEXMFMAIN/release-tetex-src.txt \
  && "teTeX-src release:   `cat $MT_TEXMFMAIN/release-tetex-src.txt`"
  test -f $MT_TEXMFDIST/release-tetex-texmf.txt \
  && "teTeX-texmf release: `cat $MT_TEXMFDIST/release-tetex-texmf.txt`"
}

# 
###############################################################################
# tcBatch(args)
#   handle batch mode
###############################################################################
tcBatch()
{
  help="texconfig supports adjusting and updating many aspects of
the TeX installation.

Usage: $progname conf                  (show configuration information)
       $progname dvips [OPTION...]     (dvips options)
       $progname faq                   (show pointer to TeX Live docs)
       $progname findprog PROG...      (show locations of PROGs, a la which)
       $progname font vardir DIR
       $progname font ro
       $progname font rw
       $progname formats               (edit fmtutil.cnf)
       $progname help                  (or --help; show this help)
       $progname hyphen FORMAT         (edit hyphenation config for FORMAT)
       $progname init [FORMAT]...      (rebuild FORMATs, or all formats
                                        plus run texlinks and updmap)
       $progname mode MODE             (set Metafont MODE)
       $progname pdftex [OPTION]...    (pdftex options)
       $progname rehash                (rebuild ls-R files with mktexlsr)
       $progname version               (or --version; show version info)

Get more help with:
       $progname dvipdfmx
       $progname dvips
       $progname font
       $progname hyphen
       $progname mode
       $progname pdftex
       $progname xdvi

Report bugs to: tex-k@tug.org
TeX Live home page: <http://tug.org/texlive/>
"

  case $1 in
    # texconfig conf
    conf|confall)
      setupTexmfmain
      setupTexmfdist
      echo '=========================== version information =========================='
      showDistVersionInfo
      echo
      echo '==================== binaries found by searching $PATH ==================='
      echo "PATH=$PATH"
      echoLocateBinary kpsewhich updmap fmtutil texconfig tex pdftex mktexpk dvips dvipdfmx
      echo
      echo '=========================== active config files =========================='
      echoLocateCfgfile texmf.cnf updmap.cfg fmtutil.cnf config.ps mktex.cnf XDvi pdftexconfig.tex | sort -k 2
      echo
      echo '============================= font map files ============================='
      for m in psfonts.map pdftex.map ps2pk.map; do
        echo "$m: `kpsewhich $m`"
      done
      echo
      echo '=========================== kpathsea variables ==========================='
      echoShowKpseVariable TEXMFMAIN TEXMFDIST TEXMFLOCAL TEXMFSYSVAR TEXMFSYSCONFIG TEXMFVAR TEXMFCONFIG TEXMFHOME VARTEXFONTS TEXMF SYSTEXMF TEXMFDBS WEB2C TEXPSHEADERS TEXCONFIG ENCFONTS TEXFONTMAPS

      echo
      echo '==== kpathsea variables from environment only (ok if no output here) ===='
      echoShowVariable $envVars
      ;;

    # texconfig dvipdfmx
    dvipdfmx)
      abort "Use tlmgr paper dvipdfmx"
      rc=1
      ;;

    # texconfig dvips
    dvips)
      shift
      help="Usage: $progname dvips add PRINTER
       $progname dvips del PRINTER
       $progname dvips [-P PRINTER] mode MODE
       $progname dvips [-P PRINTER] offset OFFSET
       $progname dvips [-P PRINTER] printcmd CMD"
      case $1 in
        -P)
          case $2 in
            "")
              echo "$progname: missing arg for parameter -P" >&2
              rc=1; (exit $rc); return $rc
              ;;
            *)
              otherPrinter=true
              otherPrinterName=$2
              otherPrinterFile=`kpsewhich -format='dvips config' "config.$otherPrinterName"`
              case $otherPrinterFile in
                "")
                  echo "$progname: configuration file \`config.$otherPrinterName' for printer \`$otherPrinterName' not found" >&2
                  rc=1; (exit $rc); return $rc
                  ;;
                *) shift; shift;;
              esac
              ;;
          esac
          ;;
        *)
          otherPrinter=false
          ;;
      esac
      case $otherPrinter in
        true)
          tcBatchDvipsPrinter=$otherPrinterName
          moreFmgrArgs="--reldir dvips/config --infile $otherPrinterFile"
          ;;
        *)
          tcBatchDvipsPrinter=ps
          ;;
      esac
      case $1 in
        add)
          case $2 in
            "")
              echo "Usage: $progname dvips add PRINTER" >&2
              rc=1
              ;;
            *)
              printerName=$2
              pFile=`kpsewhich -format='dvips config' "config.$printerName"`
              case $pFile in
                "")
                  setupTmpDir
                  tcfRet=`tcfmgr --emptyinfile --reldir dvips/config --cmd co --tmp $tmpdir --file "config.$printerName"`
                  if test $? != 0; then
                    echo "$progname: failed to add new configuration file \`config.$printerName'" >&2
                    rc=1
                  else
                    set x $tcfRet; shift
                    tcBatchDvipsAddID=$1; tcBatchDvipsAddFile=$3
                    echo "% file config.$printerName; added by texconfig" > "$tcBatchDvipsAddFile"
                    tcfRet=`tcfmgr --tmp $tmpdir --id "$tcBatchDvipsAddID" --cmd ci`
                    if test $? != 0; then
                      echo "$progname: failed to add new configuration file \`config.$printerName'" >&2
                      rc=1
                    else
                      echo "$progname: file $tcfRet added" >&2
                    fi
                  fi
                  ;;
                *)
                  echo "$progname: configuration file for printer \`$printerName' already exists (\`$pFile')" >&2
                  rc=1
                  ;;
              esac
              ;;
          esac
          ;;
        del)
          case $2 in
            "")
              echo "Usage: $progname dvips del PRINTER" >&2
              rc=1
              ;;
            *)
              printerName=$2
              pFile=`kpsewhich -format='dvips config' "config.$printerName"`
              case $pFile in
                "")
                  echo "$progname: configuration file for printer \`$printerName' (config.$printerName) not found" >&2
                  rc=1
                  ;;
                *)
                  if rm "$pFile"; then
                    echo "$progname: file \`$pFile' removed" >&2
                  else
                    echo "$progname: failed to remove file \`$pFile'" >&2
                    rc=1
                  fi
                  ;;
              esac
              ;;  
          esac
          ;;
        paper-list)
          locateConfigPsFile
          listDvipsPapers
          ;;
        paper)
          abort "Use tlmgr paper dvips"
          ;;
        mode)
          case $2 in
            "")
              echo "Usage: $progname dvips mode MODE

Valid MODE settings:"
              setupModesMfFile
              listMfModes | sed 's@ .*@@; s@^@  @' | myFmt
              rc=1
              ;;
            *)
              tcBatchDvipsMode=$2
              setupTmpDir
              setupModesMfFile
              if checkElemInList "$tcBatchDvipsMode" `listMfModes | sed 's@ .*@@'`; then
                set x `getRes "$tcBatchDvipsMode"`; shift
                resX=$1; resY=$2
                fmgrConfigReplace $moreFmgrArgs config.$tcBatchDvipsPrinter '^M' "M $tcBatchDvipsMode"
                fmgrConfigReplace $moreFmgrArgs config.$tcBatchDvipsPrinter '^D' "D $resX"
                fmgrConfigReplace $moreFmgrArgs config.$tcBatchDvipsPrinter '^X' "X $resX"
                fmgrConfigReplace $moreFmgrArgs config.$tcBatchDvipsPrinter '^Y' "Y $resY"
              else
                echo "$progname: unknown MODE \`$tcBatchDvipsMode' given as argument for \`$progname dvips mode'" >&2
                echo "$progname: try \`$progname dvips mode' for help" >&2
                rc=1
              fi
              ;;
          esac
          ;;
        offset)
          offset=$2
          case $offset in
            "")
              echo "Usage: $progname dvips offset OFFSET"
              rc=1
              ;;
            *)
              fmgrConfigReplace $moreFmgrArgs config.$tcBatchDvipsPrinter '^O' "O $offset"
          esac
          ;;
        printcmd)
          printcmd=$2
          case $printcmd in
            "")
              echo "Usage: $progname dvips printcmd CMD"
              rc=1
              ;;
            -)
              fmgrConfigReplace $moreFmgrArgs config.$tcBatchDvipsPrinter '^o' o
              ;;
            *)
              fmgrConfigReplace $moreFmgrArgs config.$tcBatchDvipsPrinter '^o' "o |$printcmd"
              ;;
          esac
          ;;
        "")
          echo "$help" >&2; rc=1
          ;;
        *)
          echo "$progname: unknown option \`$1' given as argument for \`$progname dvips'" >&2
          echo "$progname: try \`$progname dvips' for help" >&2
          rc=1
          ;;
      esac
      ;;

    faq)
      echo "Please see https://tug.org/texlive/doc/ for the documentation"
      echo "available in TeX Live."
      ;;

    findprog)
      shift
      ELB_PATH_ONLY=1 echoLocateBinary "$@"
      ;;

    # handle "texconfig font"
    font)
      setupTexmfroot
      help="Usage: $progname font vardir DIR
       $progname font ro
       $progname font rw

The vardir option changes the VARTEXFONTS variable in the texmf.cnf file.

The rw option makes the VARTEXFONTS directory (and subtrees pk, tfm,
source) world writable and sets the features appendonlydir:varfonts
in mktex.cnf.

The ro option makes the VARTEXFONTS directory (and subtrees pk, tfm,
source) writable for the owner only and sets the feature texmfvar in
mktex.cnf.

For more information about these \`features', consult the teTeX manual
(e.g. by running \`texdoc TETEXDOC')."

      case $2 in
        vardir)
          case $3 in
            "")
              echo "$help" >&2
              rc=1
              ;;
            *)
              tcBatchFontVardir=$3
              # tfc=`kpsewhich texmf.cnf`
              tfc="$MT_TEXMFROOT/texmf.cnf"
              touch "$tfc"
              if test -n "$tfc"; then
                if test -w "$tfc"; then
                  configReplace "$tfc" '^VARTEXFONTS' "VARTEXFONTS  = $tcBatchFontVardir"
                else
                  echo "$progname: setting up vardir failed. Reason: no permission to write file \`$tfc'" >&2
                  rc=1
                fi
              else
                echo "$progname: setting up vardir failed. Reason: failed to find file texmf.cnf" >&2
                rc=1
              fi
              ;;
          esac
          ;;
        rw)
          MT_VARTEXFONTS=`kpsewhich -var-value VARTEXFONTS`
          if test -z "$MT_VARTEXFONTS"; then
            echo "$progname: failed to set \`font rw'; reason: could not determine VARTEXFONTS variable." >&2; rc=1
            return
          fi
          test -d "$MT_VARTEXFONTS" || mktexdir "$MT_VARTEXFONTS"
          if test ! -d "$MT_VARTEXFONTS"; then
            echo "$progname: failed to set \`font rw'; reason: directory \`$MT_VARTEXFONTS' does not exist." >&2; rc=1
            return
          fi
          chmod 1777 "$MT_VARTEXFONTS" || {
            echo "$progname: failed to modify permissions in \`$MT_VARTEXFONTS'." >&2; rc=1
            return;
          }
          (
            cd "$MT_VARTEXFONTS" || exit
            echo "$progname: modifying permissions in \`$MT_VARTEXFONTS' ..." >&2
            for d in pk tfm source; do
              test -d "$d" && find $d -type d -exec chmod 1777 '{}' \;
            done
            echo "$progname: all permissions set." >&2
          )
          setupTmpDir
          fmgrConfigReplace mktex.cnf '^: ..MT_FEATURES=' ": \${MT_FEATURES=appendonlydir:varfonts}"
          ;;
        ro)
          MT_VARTEXFONTS=`kpsewhich -var-value VARTEXFONTS`
          if test -z "$MT_VARTEXFONTS"; then
            echo "$progname: failed to set \`font ro'; reason: could not determine VARTEXFONTS variable." >&2; rc=1
            return
          fi
          test -d "$MT_VARTEXFONTS" || mktexdir "$MT_VARTEXFONTS"
          if test ! -d "$MT_VARTEXFONTS"; then
            echo "$progname: failed to set \`font ro'; reason: directory \`$MT_VARTEXFONTS' does not exist." >&2; rc=1
            return
          fi
          chmod 755 "$MT_VARTEXFONTS" || {
            echo "$progname: failed to modify permissions in \`$MT_VARTEXFONTS'." >&2; rc=1
            return;
          }
          (
            cd "$MT_VARTEXFONTS" || exit
            echo "$progname: modifying permissions in \`$MT_VARTEXFONTS' ..." >&2
            for d in pk tfm source; do
              test -d "$d" && find "$d" -type d -exec chmod 755 '{}' \;
            done
            echo "$progname: all permissions set." >&2
          )
          setupTmpDir
          fmgrConfigReplace mktex.cnf '^: ..MT_FEATURES=' ": \${MT_FEATURES=texmfvar}"
          ;;
        "") echo "$help" >&2; rc=1;;
        *) echo "$progname: unknown option \`$2' given as argument for \`$progname font'" >&2
           echo "$progname: try \`$progname font' for help" >&2
           rc=1
           ;;
      esac
      ;;

    formats)
      cat >&2 <<EOM
texconfig formats is no longer supported, because manual edits of
fmtutil.cnf will be overwritten by the new TeX Live package manager,
tlmgr, which regenerates that file as needed upon package changes.
Thus, to add or remove formats, the recommended method is to use tlmgr
to add or remove the appropriate package.

If you need to make manual additions, you can edit the file
fmtutil-local.cnf under TEXMFLOCAL.  Further information with
tlmgr --help and at http://tug.org/texlive/tlmgr.html.

Exiting.
EOM
      exit 1  # but leave the real code for posterity

      setupTmpDir
      echo "$progname: analyzing old configuration..." >&2
      fmtutil --catcfg > $tmpdir/pre
      fmtutil --edit
      echo "$progname: analyzing new configuration..." >&2
      fmtutil --catcfg > $tmpdir/post

      if cmp $tmpdir/pre $tmpdir/post >/dev/null 2>&1; then
        echo "$progname: no new/updated formats available ..." >&2
      else
      echo "$progname: updating formats ..." >&2
        comm -13 $tmpdir/pre $tmpdir/post > $tmpdir/addOrChange
        for i in `awk '{print $1}' $tmpdir/addOrChange`; do
          fmtutil --byfmt "$i" || rc=1
        done
        texlinks --multiplatform || rc=1
      fi
      ;;

    help|--help|-h)
      echo "$help"
      ;;

    # "hyphen FORMAT"
    hyphen)
      cat >&2 <<EOM
texconfig hyphen is no longer supported, because manual edits of
language.dat (or language.def) will be overwritten by the new TeX Live
package manager, tlmgr, which regenerates those configuration files as
needed upon package changes.  Thus, to add or remove hyphenation
patterns, the recommended method is to use tlmgr to add or remove the
appropriate package.

If you need to make manual additions, you can edit the files
language-local.dat and language-local.def under TEXMFLOCAL.  Further
information with tlmgr --help and at http://tug.org/texlive/tlmgr.html.

Exiting.
EOM
      exit 1  # but leave the real code for posterity

      tcBatchHyphenFormat=$2
      formatsForHyphen=`getFormatsForHyphen`
      formatsForHyphenFmt=`echo "$formatsForHyphen" | myFmt | sed 's@^@  @'`
      help="Usage: $progname hyphen FORMAT

Valid FORMATs are:
$formatsForHyphenFmt"
      case $tcBatchHyphenFormat in
        "")
          echo "$help" >&2; rc=1
          ;;
        *)
          if checkElemInList "$tcBatchHyphenFormat" $formatsForHyphen; then

            tcBatchHyphenFile=`fmtutil --showhyphen "$tcBatchHyphenFormat"`
            case $tcBatchHyphenFile in
              "")
                echo "$progname: could not find hyphen setup file for format \`$tcBatchHyphenFormat'" >&2
                rc=1
                return
                ;;
            esac

            getRelDir "$tcBatchHyphenFile"
            case $relPart in
              "")
                # edit tcBatchHyphenFile directly
                tcBatchHFID=
                setupTmpDir
                tcBatchHFEdit=$tcBatchHyphenFile
                tcBatchHFOrig=$tmpdir/hforig
                cp "$tcBatchHyphenFile" "$tcBatchHFOrig"
                ;;
              *)
                # use tcfmgr
                tcBatchHyphenFileBasename=`echo "$tcBatchHyphenFile" | sed 's@.*/@@'`
                setupTmpDir
                co=`tcfmgr --tmp $tmpdir --cmd co --file "$tcBatchHyphenFileBasename" --reldir "$relPart" --infile "$tcBatchHyphenFile"`
                if test $? != 0; then
                  echo "$progname: failed to check out file \`$tcBatchHyphenFile'" >&2
                  rc=1
                  return 1
                else
                  set x $co; shift
                  tcBatchHFID=$1; tcBatchHFEdit=$3; tcBatchHFOrig=$4
                fi
                ;;
            esac
            ${VISUAL-${EDITOR-vi}} "$tcBatchHFEdit"
            if cmp "$tcBatchHFEdit" "$tcBatchHFOrig" >/dev/null 2>&1; then
              echo "$progname: configuration unchanged." >&2
            else
              case $tcBatchHFID in
                "")
                  tcBatchHFOut=$tcBatchHFEdit
                  echo "$progname: updated configuration saved as file \`$tcBatchHFOut'" >&2
                  lastUpdatedFile=$ci
                  ;;
                *)
                  ci=`tcfmgr --tmp $tmpdir --cmd ci --id "$tcBatchHFID"`
                  if test $? != 0; then
                    echo "$progname: failed to check in file \`$tcBatchHyphenFileBasename'" >&2
                    rc=1
                    return
                  else
                    tcBatchHFOut=$ci
                    echo "$progname: updated configuration saved as file \`$tcBatchHFOut'" >&2
                    lastUpdatedFile=$ci
                  fi
                  ;;
              esac
              fmtutil --byhyphen "$tcBatchHFOut"
            fi
          else
            echo "$progname: invalid format \`$tcBatchHyphenFormat' specified as argument for \`$progname hyphen'" >&2
            echo "$progname: for getting help, try \`$progname hyphen'" >&2
            rc=1
          fi
          ;;
      esac
      ;;

    hyphen-list)
      getFormatsForHyphen
      ;;

    init)
      case $2 in
        "")
          if fmtutil --all \
             && texlinks --multiplatform \
             && updmap; then
            :
          else
            rc=1
          fi
          ;;
        *)
          shift 1
          for i in "$@"; do
            fmtutil --byfmt "$i" || rc=1
          done
          ;;
      esac
      ;;

    mode-list)
      setupModesMfFile
      listMfModes
      ;;

    mode)
      case $2 in
        "")
          echo "Usage: $progname mode MODE

Valid MODE settings:"
          setupModesMfFile
          listMfModes | sed 's@ .*@@; s@^@  @' | myFmt
          rc=1
          ;;
        *)
          tcBatchMode=$2
          setupModesMfFile
          if checkElemInList $tcBatchMode `listMfModes | sed 's@ .*@@'`; then

            # modify mktex.cnf
            setupTmpDir
            fmgrConfigReplace mktex.cnf '^: ..MODE=' ": \${MODE=$tcBatchMode}"
            set x `getRes "$tcBatchMode"`; shift
            tcBatchRes=$1
            fmgrConfigReplace mktex.cnf '^: ..BDPI=' ": \${BDPI=$tcBatchRes}"

            if checkForBinary dvips >/dev/null && tcfmgr --cmd find --file config.ps >/dev/null 2>&1; then
              tcBatch dvips mode "$tcBatchMode"
            fi
            if checkForBinary pdftex >/dev/null && tcfmgr --cmd find --file pdftexconfig.tex >/dev/null 2>&1; then
              tcBatch pdftex mode "$tcBatchMode"
            fi
          else
            echo "$progname: unknown mode \`$tcBatchMode' specified as argument for \`$progname mode'" >&2; rc=1
          fi
          ;;
      esac
      ;;

    paper)
      abort "Use tlmgr paper"
      ;;

    pdftex)
      abort "Use tlmgr paper pdftex"
      ;;

    rehash)
      mktexlsr
      ;;
    
    # 
    version|--version)
      echo "$progname version $version"
      setupTexmfmain
      setupTexmfdist
      showDistVersionInfo
      (exit 0); exit 0;;

    # handle "xdvi paper PAPER"
    xdvi)
      abort "Use tlmgr paper xdvi"
      ;;
    *)
      echo "$progname: unknown option \`$1' given as argument for \`$progname'" >&2
      echo "$progname: try \`$progname help' for help" >&2
      rc=1
  esac
}

###############################################################################
# tcInteractive(void)
#   handle interactive mode
###############################################################################
tcInteractive()
{
  texconfig-dialog
}

###############################################################################
# main()
###############################################################################
rc=0
case $# in
  0) tcInteractive;;
  *) tcBatch "$@";;
esac

cleanup $rc