summaryrefslogtreecommitdiff
path: root/Master/tlpkg/archive/texconf.tlu
blob: e41500cc4477f87ce7f16fee4e4d4b6cc83d62ca (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
#! /usr/bin/env texlua
--*- Lua -*-
-- $Id$

-- Copyright (C) 2008 Reinhard Kotucha, Norbert Preining.
-- You may freely use, modify and/or distribute this file.

-- texconf is a subset of Thomas Esser's texconfig shell script.

if string.find(arg[0], '/') then -- UNIX path
   filename=select(1, string.gsub(arg[0], '.*/', ''))
elseif string.find(arg[0], '\\') then -- Windows path
   filename=select(1, string.gsub(arg[0], '.*\\', ''))
else -- no path
   filename=arg[0]
end

basename=select(1, string.gsub(filename, '\.tlu$', ''))

usageprogname = basename

domktexlsr = false
doformat = false
sys=false
noformatopt = false

if string.find(basename, '-sys$') then
   basename=select(1, string.gsub(basename, '-sys$', ''))
   sys=true
end

-- this script does NOT appear in the patch, so we cannot use its basename
-- for kpse.set_program_name
-- thus we use simply mktexlsr, which always is in the path and which
-- does not have any special settings in texmf.cnf
-- kpse.set_program_name(filename, basename)
kpse.set_program_name( "mktexlsr" )

-- set the search path for lua libraries
texmfmain = kpse.var_value('TEXMFMAIN')
texmfdist = kpse.var_value('TEXMFDIST')
package.path = texmfmain.."/scripts/texlive/lua/?.tlu"
require("texlive.getopt")
require("texlive.utils")

-- do argument processing
options = getopt ( { sys = 0, prog = 1, noformat = 0 } )
if (options["sys"]) then
  sys = true
end
if (options["prog"]) then
  usageprogname = options["prog"]
end
if (options["noformat"]) then
  noformatopt = true
end
 
if (sys) then
  texmfconfig=kpse.var_value('TEXMFSYSCONFIG')
  os.setenv('TEXMFCONFIG', texmfconfig)
  texmfsysvar=kpse.var_value('TEXMFSYSVAR')
  os.setenv('TEXMFVAR', texmfsysvar)
else
  texmfconfig=kpse.var_value('TEXMFCONFIG')
  if string.match(texmfconfig, '^~') then
    -- workaround for old texlua, new kpathsea which expands '~' not
    -- integrated yet.
    if os.type == 'windows' then
      home = os.getenv('USERPROFILE')
    else
      home = os.getenv('HOME')
    end
    texmfconfig = string.gsub(texmfconfig, '^~', home)
  end
end


pdftex_papersize={}
pdftex_papersize['letter']={'8.5 true in', '11 true in'}
pdftex_papersize['a4']={'210 true mm', '297 true mm'}

xdvi_papersize={}
xdvi_papersize['us']='8.5x11'
xdvi_papersize['usr']='11x8.5'
xdvi_papersize['legal']='8.5x14'
xdvi_papersize['foolscap']='13.5x17.0'
xdvi_papersize['a1']='59.4x84.0cm'
xdvi_papersize['a2']='42.0x59.4cm'
xdvi_papersize['a3']='29.7x42.0cm'
xdvi_papersize['a4']='21.0x29.7cm'
xdvi_papersize['a5']='14.85x21.0cm'
xdvi_papersize['a6']='10.5x14.85cm'
xdvi_papersize['a7']='7.42x10.5cm'
xdvi_papersize['a1r']='84.0x59.4cm'
xdvi_papersize['a2r']='59.4x42.0cm'
xdvi_papersize['a3r']='42.0x29.7cm'
xdvi_papersize['a4r']='29.7x21.0cm'
xdvi_papersize['a5r']='21.0x14.85cm'
xdvi_papersize['a6r']='14.85x10.5cm'
xdvi_papersize['a7r']='10.5x7.42cm'
xdvi_papersize['b1']='70.6x100.0cm'
xdvi_papersize['b2']='50.0x70.6cm'
xdvi_papersize['b3']='35.3x50.0cm'
xdvi_papersize['b4']='25.0x35.3cm'
xdvi_papersize['b5']='17.6x25.0cm'
xdvi_papersize['b6']='13.5x17.6cm'
xdvi_papersize['b7']='8.8x13.5cm'
xdvi_papersize['b1r']='100.0x70.6cm'
xdvi_papersize['b2r']='70.6x50.0cm'
xdvi_papersize['b3r']='50.0x35.3cm'
xdvi_papersize['b4r']='35.3x25.0cm'
xdvi_papersize['b5r']='25.0x17.6cm'
xdvi_papersize['b6r']='17.6x13.5cm'
xdvi_papersize['b7r']='13.5x8.8cm'
xdvi_papersize['c1']='64.8x91.6cm'
xdvi_papersize['c2']='45.8x64.8cm'
xdvi_papersize['c3']='32.4x45.8cm'
xdvi_papersize['c4']='22.9x32.4cm'
xdvi_papersize['c5']='16.2x22.9cm'
xdvi_papersize['c6']='11.46x16.2cm'
xdvi_papersize['c7']='8.1x11.46cm'
xdvi_papersize['c1r']='91.6x64.8cm'
xdvi_papersize['c2r']='64.8x45.8cm'
xdvi_papersize['c3r']='45.8x32.4cm'
xdvi_papersize['c4r']='32.4x22.9cm'
xdvi_papersize['c5r']='22.9x16.2cm'
xdvi_papersize['c6r']='16.2x11.46cm'
xdvi_papersize['c7r']='11.46x8.1cm'

dvipdfm_papersize={'letter', 'legal', 'ledger', 'tabloid', 'a4', 'a3'}

function mkdirhier(path)
  local dirs={}
  local dir=path
  while string.match(dir, '/') do
    dirs[#dirs+1]=dir
    dir=select(1, string.gsub(dir, '/[^/]*$', ''))
  end
  -- reverse the list
  local dirlist={}
  for i=1, #dirs do
    dirlist[#dirlist+1]=dirs[#dirs-i+1]
  end
  for i=1, #dirlist do
    if not lfs.isdir(dirlist[i]) then
      lfs.mkdir(dirlist[i])
    end
  end
end

function set_dvips_papersize(papersize)
  -- If the argument is 'list' then a list of available paper sizes is
  -- returned to stdout.  It can be used by dialog(1) or */Tk
  -- interfaces in order to build up the menu.  The topmost entry is
  -- the one which is actually used by dvips.
  local i, key, val
  local partab={}
  local par=1
  local line
  local lin=1
  partab[par]={}
  -- Read the file.  partab{} contains all paragraphs.
  kpse.set_program_name('dvips')
  local filename=kpse.find_file('config.ps', 'dvips config')
  -- print ('< '..filename)
  local infile=assert(io.open(filename, 'r'))
  while true do
    line=infile:read('*line')
    if line == nil then break end
    if string.find(line, '^\s*$') then
      par=par+1
      partab[par]={}
      lin=1
    else
      partab[par][lin]=line
      lin=lin+1
    end
  end
  -- par_papersize{} is a list which associates papersizes with
  -- paragraphs. Paregraphs containing no papersize information are
  -- denoted by 'NO_SIZE_PAR'.
  local par_papersize={}
  for i=1, #partab do
    par_papersize[i]='NO_SIZE_PAR'
    for k=1, #partab[i] do
      if string.find(partab[i][k], '^@%s') then
        par_papersize[i]=string.match(partab[i][k], '^@%s+(%w+)%s')
      end
    end
  end
  infile:close()

  local index=-1  --the papagraph wich contains the desired size.
  if papersize=='--list' then
    -- print a list of available sizes to stdout.
    -- the topmost item is the one currently used by dvips.
    for key, val in ipairs(par_papersize) do
      if val ~= 'NO_SIZE_PAR' and val ~= 'unknown' then
        io.stdout:write(val..'\n')
      end
    end
  elseif papersize=='help' then
    io.stdout:write('Usage: '..usageprogname..' dvips paper PAPER\n\n'..
                    'Valid PAPER settings:\n ')
    local buffer=''
    for key, val in ipairs(par_papersize) do
      if val ~= 'NO_SIZE_PAR' and val ~= 'unknown' then
        if #buffer + #val < 77 then
          buffer=buffer..' '..val
        else 
          io.stdout:write(buffer..'\n')
          buffer='  '..val
        end
      end
    end
    io.stdout:write(buffer..'\n')
  else --change paper size
    for key, val in ipairs(par_papersize) do
      if string.find(val, '^'..papersize..'$') then
        index=key
      end
    end
    if index == -1 then 
      io.stderr:write('Error: Paper Size "'..papersize..
                      '" undefined.  Aborting...\n')
      os.exit(1)
    end
    -- print stuff which doesn't contain paper size information.
    local outfile={}
    for key, val in ipairs(par_papersize) do
      if val == 'NO_SIZE_PAR' then
        for i=1, #partab[key] do
          outfile[#outfile+1]=partab[key][i]..'\n'
        end
        outfile[#outfile+1]='\n'
      end   
    end
    -- print selected par
    for i=1, #partab[index] do
      outfile[#outfile+1]=partab[index][i]..'\n'
    end
    outfile[#outfile+1]='\n'    
    -- print other pars to outfile table
    for key, val in ipairs(par_papersize) do
      if val ~= 'NO_SIZE_PAR' then
        if key~=index then
          for i=1, #partab[key] do
            outfile[#outfile+1]=partab[key][i]..'\n'
          end
          outfile[#outfile+1]='\n'
        end
      end
    end
    -- write file
    mkdirhier(texmfconfig..'/dvips/config')
    local config=assert(io.open(texmfconfig..'/dvips/config/config.ps', 'w'))
    for i=1, #outfile do
      if not (string.match(outfile[i],'^%s*$') 
            and string.match(outfile[i-1],'^%s*$')) then
        config:write(outfile[i])
      end
    end    
    config:close()
    print ('> '..texmfconfig..'/dvips/config/config.ps')
    domktexlsr = true
  end
end -- set_dvips_papersize


function set_xdvi_papersize(papersize)
  local current_setting
  -- Read the file.
  local filename
  if os.type == 'windows' then
    return
  end

  kpse.set_program_name('xdvi')
  filename=kpse.find_file('XDvi', 'other text files')

  -- print ('< '..filename)
  local infile=assert(io.open(filename, 'r'))
  local lines={}
  while true do
    line=infile:read('*line')
    if line == nil then break end
    lines[#lines+1]=line
  end
  infile:close()
  
  if papersize=='--list' then
    current_setting='a4'
    for n, line in ipairs(lines) do
      if string.find(line, '^\*paper:') then
        current_setting=string.match(line, '^\*paper:%s+(%S+)')
        for ptype, psize in pairs(xdvi_papersize) do
          if current_setting==psize then
            current_setting=ptype
          end
        end
      end
    end
    print (current_setting)
    for key, val in pairs(xdvi_papersize) do
      if key ~= current_setting then
        print(key)
      end
    end
  elseif papersize=='help' then
    io.stdout:write('Usage: '..usageprogname..' xdvi paper PAPER\n\n'..
                    'Valid PAPER settings:\n ')
    local buffer=''
    for ptype, psize in pairs(xdvi_papersize) do
      if #buffer + #ptype < 77 then
        buffer=buffer..' '..ptype
      else 
        io.stdout:write(buffer..'\n')
        buffer='  '..ptype
      end
    end
  io.stdout:write(buffer..'\n')
  else --change paper size
    mkdirhier(texmfconfig..'/xdvi')
    local config=assert(io.open(texmfconfig..'/xdvi/XDvi', 'w'))
    for n=1, #lines do
      if string.find(lines[n], '^!*\*paper:') then
        config:write('*paper: '..papersize..'\n')
      else 
        config:write(lines[n]..'\n')
      end
    end
    config:close()
    print ('> '..texmfconfig..'/xdvi/XDvi')
    domktexlsr = true
  end
end


function set_pdftex_papersize(papersize)
  local current_setting
  --Read the file.
  kpse.set_program_name('pdftex')
  local filename=kpse.find_file('pdftexconfig.tex', 'tex')
  
  -- print ('< '..filename)
  local infile=assert(io.open(filename, 'r'))
  local lines={}
  while true do
    line=infile:read('*line')
    if line == nil then break end
    if string.find(line, '^\\pdfpagewidth') then
      pagewidth=string.match(line, '^\\pdfpagewidth%s*=?%s*(.+)%s*$')
    end
    if string.find(line, '^\\pdfpageheight') then
      pageheight=string.match(line, '^\\pdfpageheight%s*=?%s*(.+)%s*$')
    end
    lines[#lines+1]=line
  end
  infile:close()
  
  current_setting='a4'
  for ptype, sizes in pairs(pdftex_papersize) do
    if pagewidth == pdftex_papersize[ptype][1] then
      ptype_x=ptype
    end
    if pageheight == pdftex_papersize[ptype][2] then
      ptype_y=ptype
    end
  end
  if ptype_x == ptype_y then
    current_setting=ptype_x
  end
  
  if papersize=='--list' then
    print(current_setting)
    for ptype, sizes in pairs(pdftex_papersize) do
      if ptype ~= current_setting then
        print(ptype)
      end
    end
  elseif papersize=='help' then
    io.stdout:write('Usage: '..usageprogname..' pdftex paper PAPER\n\n'..
                    'Valid PAPER settings:\n ')
    local buffer=''
    for ptype, sizes in pairs(pdftex_papersize) do
      if #buffer + #ptype < 77 then
        buffer=buffer..' '..ptype
      else 
        io.stdout:write(buffer..'\n')
        buffer='  '..ptype
      end
    end
    io.stdout:write(buffer..'\n')
  else --change paper size
    mkdirhier(texmfconfig..'/tex/generic/config')
    local outfile=texmfconfig..'/tex/generic/config/pdftexconfig.tex'
    local config=assert(io.open(outfile, 'w'))
    for n=1, #lines do
      line=lines[n]
      if string.find(line, '^\\pdfpagewidth') then
        config:write('\\pdfpagewidth='..pdftex_papersize[papersize][1]..'\n')
      elseif string.find(line, '^\\pdfpageheight') then
        config:write('\\pdfpageheight='..pdftex_papersize[papersize][2]..'\n')
      else
        config:write(line..'\n')
      end
    end
    config:close()
    print ('> '..outfile)
    domktexlsr = true
    doformat = true
  end
end


function set_dvipdfm_papersize(papersize, program)
  local current_setting
  -- Read the file.
  local filename
  if program == 'dvipdfm' then
    kpse.set_program_name('dvipdfm')
    filename=kpse.find_file('config', 'other text files')
  elseif program == 'dvipdfmx' then
    kpse.set_program_name('dvipdfmx')
    filename=kpse.find_file('dvipdfmx.cfg', 'other text files')
  end
  if not(filename) then
    print ("Cannot find config file for "..program..", exiting!")
    os.exit(1)
  end
  -- print ('< '..filename)
  local infile=assert(io.open(filename, 'r'))
  local lines={}
  local current_setting='a4'
  while true do
    line=infile:read('*line')
    if line == nil then break end
    if string.find(line, '^p ') then
      current_setting=string.match(line, '^p%s+(%S+)')
    end
    lines[#lines+1]=line
  end
  infile:close()
  
  if papersize=='--list' then
    print(current_setting)
    for i, ptype in ipairs(dvipdfm_papersize) do
      if ptype ~= current_setting then
        print (ptype)
      end
    end
  elseif papersize=='help' then
    io.stdout:write('Usage: '..usageprogname..' '..program..' paper PAPER\n\n'..
                    'Valid PAPER settings:\n ')
    local buffer=''
    for i, ptype in pairs(dvipdfm_papersize) do
      if #buffer + #ptype < 77 then
        buffer=buffer..' '..ptype
      else 
        io.stdout:write(buffer..'\n')
        buffer='  '..ptype
      end
    end
    io.stdout:write(buffer..'\n')
  else --change paper size
    local outfile
    mkdirhier(texmfconfig..'/dvipdfm/config')
    if program == 'dvipdfm' then
      outfile=texmfconfig..'/dvipdfm/config/config'
      mkdirhier(texmfconfig..'/dvipdfm/config')
    else
      outfile=texmfconfig..'/dvipdfmx/dvipdfmx.cfg'
      mkdirhier(texmfconfig..'/dvipdfmx')
    end
    print ('> '..outfile)
    local config=assert(io.open(outfile, 'w'))
    for n=1, #lines do
      line=lines[n]
      if string.find(line, '^p ') then
        config:write('p '..papersize..'\n')
      else
        config:write(line..'\n')
      end
    end
    config:close()
    domktexlsr = true
  end
end


function set_context_papersize(papersize)
  local current_setting

  -- we cannot guarantee that context is installed, so we check for the
  -- file we would need for creating the configuration, and if that is
  -- missing we just return (with a warning).
  if not(lfs.isfile(texmfdist..'/tex/context/user/cont-sys.rme')) then
    io.stderr:write('context not installed, skipping paper setting for it\n')
    return
  end

  -- Read the file.
  local filename

  kpse.set_program_name('tex')
  filename=kpse.find_file('cont-sys.tex', 'tex') 
  if not(filename) then
    filename=kpse.find_file('cont-sys.rme', 'other text files') 
  end
  if not(filename) then
    print ("Cannot find config file for context, exiting!")
    os.exit(1)
  end
  -- print ('< '..filename)

  local infile=assert(io.open(filename, 'r'))
  local lines={}
  local current_setting='A4'
  local papersize_regexp='^%s*\\setuppapersize%s*%[([^%[]*)%]'
  while true do
    line=infile:read('*line')
    if line == nil then break end
    if string.find(line, '^%s*\\setuppapersize') then
      current_setting=string.match(line, papersize_regexp)
    end
    lines[#lines+1]=line
  end
  infile:close()

  -- print ('current setting: '..current_setting)
 
  if papersize=='--list' then
    print(current_setting)
    if (string.match (current_setting, '[aA]4')) then
      print ('letter')
    else 
      print ('A4')
    end 
  elseif papersize=='help' then
    io.stdout:write('Usage: '..usageprogname..' context paper PAPER\n\n'..
                    'Valid PAPER settings:\n\nA4 letter\n')
  else --change paper size
    -- make sure we have uppercase A4 for paper size
    if (string.match (papersize, 'a4')) then
      papersize = 'A4'
    end
    local outfile
    mkdirhier(texmfconfig..'/tex/context/user')
    outfile=texmfconfig..'/tex/context/user/cont-sys.tex'
    print ('> '..outfile)
    local config=assert(io.open(outfile, 'w'))
    for n=1, #lines do
      line=lines[n]
      if string.find(line, '\setuppapersize') then
        config:write('\\setuppapersize['..papersize..']['..papersize..']\n')
      else
        config:write(line..'\n')
      end
    end
    config:close()
    domktexlsr = true
  end
end


function set_papersize(papersize)
  if papersize == '--list' then
    set_pdftex_papersize('--list')
  elseif papersize == 'help' then
    io.stdout:write('Usage: '..usageprogname..' paper PAPER\n\n'..
                    'Valid PAPER settings:\n  a4 letter\n')
  end
  if papersize ~= 'a4' and papersize ~= 'letter' then
    io.stdout:write('Invalid paper size "'..papersize..'".'..
                    '  Valid sizes: a4 letter\n')
    os.exit(1)
  else
    set_dvips_papersize(papersize)
    set_pdftex_papersize(papersize)
    set_xdvi_papersize(papersize)
    set_context_papersize(papersize)
    set_dvipdfm_papersize(papersize, 'dvipdfm')
    set_dvipdfm_papersize(papersize, 'dvipdfmx')
  end
end


function usage_main()
  local usage={
    'help',
    'xdvi paper PAPER      (xdvi paper size)',
    'dvips paper PAPER     (dvips paper size)',
    'pdftex paper PAPER    (pdftex paper size)',
    'dvipdfm paper PAPER   (dvipdfm paper size)',
    'dvipdfmx paper PAPER  (dvipdfmx paper size)',
    'paper [a4|letter]     (paper size for all programs)',
    'init [FORMAT] ...     (rebuild FORMATs, or all formats plus run updmap)',
    'rehash                (rebuild ls-R files with mktexlsr)'
  }
  io.stdout:write('Usage:\n')
  for i, msg in ipairs(usage) do 
    io.stdout:write('      '..usageprogname..' '..msg..'\n')
  end
  os.exit(0)
end


function usage_paper(progname)
  io.stdout:write('Usage: '..usageprogname..' '..progname..' paper PAPER\n')
  io.stdout:write('       '..usageprogname..' '..progname..' help\n')
  os.exit(0)
end


function set_prog_papersize(prog,size)
  if (prog == 'dvips') then
    set_dvips_papersize(size)
  elseif (prog == 'xdvi') then
    set_xdvi_papersize(size)
  elseif (prog == 'pdftex') then
    set_pdftex_papersize(size)
  elseif (prog == 'dvipdfm') then
    set_dvipdfm_papersize(size, "dvipdfm")
  elseif (prog == 'dvipdfmx') then
    set_dvipdfm_papersize(size, "dvipdfmx")
  end
end


if arg[1] == 'help' then
  usage_main()
elseif arg[1] == 'paper' then
  if arg[2] == 'help' or arg[2] == nil then
    usage_main()
  else
    set_papersize(arg[2])
  end
elseif arg[1] == 'rehash' then
  command = { "mktexlsr" }
  command = fixwin(command)
  os.exec(command)
  -- does not return
elseif arg[1] == 'init' then
  if arg[2] == nil then
    -- we have set TEXMFCONFIG and TEXMFVAR to the SYS variants above,
    -- so we can run the normal fmtutil/updmap program
    os.execute('fmtutil --all')
    os.execute('updmap')
  else
    for i=2, #arg do
      os.execute('fmtutil --byfmt "'..arg[i]..'"')
    end
  end
elseif arg[1] == 'paper' then
  set_papersize(arg[2])
elseif arg[1] == 'dvips' then
  if arg[2] == 'help' or arg[3]==nil then
    usage_paper('dvips')
  elseif arg[2]=='paper' then
    set_dvips_papersize(arg[3])
  end
elseif arg[1] == 'xdvi' then
  if arg[2] == 'help' or arg[3]==nil then
    usage_paper('xdvi')
  elseif arg[2]=='paper' then
    set_xdvi_papersize(arg[3])
  end
elseif arg[1] == 'pdftex' then
  if arg[2] == 'help' or arg[3]==nil then
    usage_paper(pdftex)
  elseif arg[2]=='paper' then
    set_pdftex_papersize(arg[3])
  end
elseif arg[1] == 'dvipdfm' then
  if arg[2] == 'help' or arg[3]==nil then
    usage_paper('dvipdfm')
  elseif arg[2]=='paper' then
    set_dvipdfm_papersize(arg[3], 'dvipdfm')
  end
elseif arg[1] == 'dvipdfmx' then
  if arg[2] == 'help' or arg[3]==nil then
    usage_paper('dvipdfmx')
  elseif arg[2]=='paper' then
    set_dvipdfm_papersize(arg[3], 'dvipdfmx')
  end
elseif arg[1] == 'context' then
  if arg[2] == 'help' or arg[3]==nil then
    usage_paper('context')
  elseif arg[2]=='paper' then
    set_context_papersize(arg[3])
  end
else
  usage_main()
end


if sys and domktexlsr then
  command = { "mktexlsr", texmfconfig }
  command = fixwin(command)
  os.spawn(command)
end

if noformatopt then
  doformat = false
end

if doformat then
  -- we have to update the formats
  -- I don't know how to select only those formats for which pdftexconfig.tex
  -- is necessary, so we call fmtutil-sys --refresh
  -- that will recreate a lot, sorry
  --
  -- on win32 texlua's os.spawn does not find fmtutil-sys.texlua as executable
  -- even if I give it the full path. So we have to work around that
  if sys then
    command = 'fmtutil-sys --refresh'
  else
    command = 'fmtutil --refresh'
  end
  os.execute(command)
end

-- Local Variables:
-- lua-indent-level: 2
-- tab-width: 2
-- indent-tabs-mode: nil
-- End:
-- vim:set tabstop=2 expandtab: #