summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/context/lua/mtx-install.lua
blob: d66278dc83fc098e45f46607b4e4cc4b7b564b9b (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
if not modules then modules = { } end modules ['mtx-install'] = {
    version   = 1.002,
    comment   = "companion to mtxrun.lua",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- todo: initial install from zip

local helpinfo = [[
<?xml version="1.0"?>
<application>
 <metadata>
  <entry name="name">mtx-install</entry>
  <entry name="detail">ConTeXt Installer</entry>
  <entry name="version">2.01</entry>
 </metadata>
 <flags>
  <category name="basic">
   <subcategory>
    <flag name="platform" value="string"><short>platform</short></flag>
    <flag name="server" value="string"><short>repository url (rsync://contextgarden.net)</short></flag>
    <flag name="modules" value="string"><short>extra modules (can be list or 'all')</short></flag>
    <flag name="fonts" value="string"><short>additional fonts (can be list or 'all')</short></flag>
    <flag name="goodies" value="string"><short>extra binaries (like scite and texworks)</short></flag>
    <flag name="install"><short>install context</short></flag>
    <flag name="update"><short>update context</short></flag>
    <flag name="erase"><short>wipe the cache</short></flag>
    <flag name="identify"><short>create list of files</short></flag>
    <flag name="secure"><short>use curl for https</short></flag>
   </subcategory>
  </category>
 </flags>
</application>
]]

local type, tonumber = type, tonumber
local gsub, find, escapedpattern = string.gsub, string.find, string.escapedpattern
local round = math.round
local savetable, loadtable, sortedhash = table.save, table.load, table.sortedhash
local copyfile, joinfile, filesize, dirname, addsuffix, basename = file.copy, file.join, file.size, file.dirname, file.addsuffix, file.basename
local isdir, isfile, walkdir, pushdir, popdir, currentdir = lfs.isdir, lfs.isfile, lfs.dir, dir.push, dir.pop, dir.current
local mkdirs, globdir = dir.mkdirs, dir.glob
local osremove, osexecute, ostype, resultof = os.remove, os.execute, os.type, os.resultof
local savedata, loaddata = io.savedata, io.loaddata
local formatters = string.formatters
local httprequest = socket.http.request

local usecurl  = false
local protocol = "http"

local function checkcurl()
    local s = resultof("curl --version")
    return type(s) == "string" and find(s,"libcurl") and find(s,"rotocols")
end

local function fetch(url)
    local data   = nil
    local detail = nil
    if usecurl and find(url,"^https") then
        data = resultof("curl " .. url)
    else
        data, detail = httprequest(url)
    end
    if type(data) ~= "string" then
        data   = false
        detail = "download failed"
    elseif #data == 0 then
        data   = false
        detail = "download failed, zero length"
    elseif #data < 2048 then
        local n, t = find(data,"<head>%s*<title>%s*(%d+)%s(.-)</title>")
        if tonumber(n) then
            data   = false
            detail = n .. " " .. t
        end
    end
    return data, detail
end

local application = logs.application {
    name     = "mtx-install",
    banner   = "ConTeXt Installer 2.01",
    helpinfo = helpinfo,
}

local report = application.report

scripts         = scripts         or { }
scripts.install = scripts.install or { }
local install   = scripts.install

local texformats = {
    "cont-en",
    "cont-nl",
    "cont-cz",
    "cont-de",
    "cont-fa",
    "cont-it",
    "cont-ro",
    "cont-uk",
    "cont-pe",
}

local platforms = {
    ["mswin"]          = "mswin",
 -- ["windows"]        = "mswin",
    ["win32"]          = "mswin",
    ["win"]            = "mswin",
    --
    ["windows"]        = "win64",
    ["windows-64"]     = "win64",
    ["win64"]          = "win64",
    --
    ["linux"]          = "linux",
    ["linux-32"]       = "linux",
    ["linux32"]        = "linux",
    --
 -- ["linux"]          = "linux-64",
    ["linux-64"]       = "linux-64",
    ["linux64"]        = "linux-64",
    --
    ["linuxmusl"]      = "linuxmusl",
    ["linuxmusl-64"]   = "linuxmusl-64",
    --
 -- ["linux-armhf"]    = "linux-armhf",
    --
    ["openbsd"]        = "openbsd-amd64",
 -- ["openbsd-i386"]   = "openbsd7.3",
    ["openbsd-amd64"]  = "openbsd-amd64",
    --
    ["freebsd"]        = "freebsd-amd64",
 -- ["freebsd-i386"]   = "freebsd",
    ["freebsd-amd64"]  = "freebsd-amd64",
    --
 -- ["kfreebsd"]       = "kfreebsd-i386",
 -- ["kfreebsd-i386"]  = "kfreebsd-i386",
 -- ["kfreebsd-amd64"] = "kfreebsd-amd64",
    --
 -- ["linux-ppc"]      = "linux-ppc",
 -- ["ppc"]            = "linux-ppc",
    --
 -- ["osx"]            = "osx-intel",
 -- ["macosx"]         = "osx-intel",
 -- ["osx-intel"]      = "osx-intel",
 -- ["osxintel"]       = "osx-intel",
    --
 -- ["osx-ppc"]        = "osx-ppc",
 -- ["osx-powerpc"]    = "osx-ppc",
 -- ["osxppc"]         = "osx-ppc",
 -- ["osxpowerpc"]     = "osx-ppc",
    --
    ["macosx"]         = "osx-64",
    ["osx"]            = "osx-64",
    ["osx-intel"]      = "osx-64",
    ["osx-64"]         = "osx-64",
    ["osx-arm"]        = "osx-arm64",
    ["osx-arm64"]      = "osx-arm64",
    --
 -- ["solaris-intel"]  = "solaris-intel",
    --
 -- ["solaris-sparc"]  = "solaris-sparc",
 -- ["solaris"]        = "solaris-sparc",
    --
    ["unknown"]        = "unknown",
}

function install.identify()

    -- We have to be in "...../tex" where subdirectories are prefixed with
    -- "texmf". We strip the "tex/texm*/" from the name in the list.

    local hashdata = sha2 and sha2.HASH256 or md5.hex

    local function collect(root,tree)

        local path = root .. "/" .. tree

        if isdir(path) then

            local prefix  = path .. "/"
            local files   = globdir(prefix .. "**")
            local pattern = escapedpattern("^" .. prefix)

            local details = { }
            local total   = 0

            for i=1,#files do
                local name  = files[i]
                local size  = filesize(name)
                local base  = gsub(name,pattern,"")
                local data  = loaddata(name)
                if data and #data > 0 then
                    local stamp = hashdata(data)
                    details[i]  = { base, size, stamp }
                    total       = total + size
                else
                    report("%-24s : bad file %a",tree,name)
                end
            end
            report("%-24s : %4i files, %3.0f MB",tree,#files,total/(1000*1000))

            savetable(path .. ".tma",details)

        end

    end

    local sourceroot = file.join(dir.current(),"tex")

    for d in walkdir("./tex") do
        if find(d,"%texmf") then
            collect(sourceroot,d)
        end
    end

    savetable("./tex/status.tma",{
        name    = "context",
        version = "lmtx",
        date    = os.date("%Y-%m-%d"),
    })

    os.exit()

end

local function disclaimer()
    report("ConTeXt LMTX with LuaMetaTeX is still experimental and when you get a crash this")
    report("can be due to a mismatch between Lua bytecode and the engine. In that case you can")
    report("try the following:")
    report("")
    report("  - wipe the texmf-cache directory")
    report("  - run: mtxrun --generate")
    report("  - run: context --make")
    report("")
    report("When that doesn't solve the problem, ask on the mailing list (ntg-context@ntg.nl).")
end

function install.update()

    local hashdata = sha2 and sha2.HASH256 or md5.hex

    local function validdir(d)
        local ok = isdir(d)
        if not ok then
            mkdirs(d)
            ok = isdir(d)
        end
        return ok
    end

    local function download(what,url,target,total,done,hash)
        local data = fetch(url .. "/" .. target)
        if type(data) == "string" and #data > 0  then
            if total and done then
                report("%-8s : %3i %% : %8i : %s",what,round(100*done/total),#data,target)
            else
                report("%-8s : %8i : %s",what,#data,target)
            end
            if hash and hash ~= hashdata(data) then
                return "different hash value"
            elseif not validdir(dirname(target)) then
                return "wrong target directory"
            else
                savedata(target,data)
            end
        else
            return "unable to download"
        end
    end

    local function remove(what,target)
        report("%-8s : %8i : %s",what,filesize(target),target)
        osremove(target)
    end

    local function ispresent(target)
        return isfile(target)
    end

    local function hashed(list)
        local hash = { }
        for i=1,#list do
            local l = list[i]
            hash[l[1]] = l
        end
        return hash
    end

    local function run(fmt,...)
        local command = formatters[fmt](...)
     -- command = gsub(command,"/","\\")
        report("running: %s",command)
        osexecute(command)
    end

    local function prepare(tree)
        tree = joinfile("tex",tree)
        mkdirs(tree)
    end

    local function update(url,what,zipfile,skiplist)

        local tree = joinfile("tex",what)

        local ok = validdir(tree)
        if not validdir(tree) then
            report("invalid directory %a",tree)
            return
        end

        local lua = tree .. ".tma"
        local all = url .. "/" .. lua
        local old = loadtable(lua)
        local new = fetch(all)

        if new then
            new = loadstring(new)
            if new then
                new = new()
            end
        end

        if not new then
            report("invalid database %a",all)
            return
        end

        local total = 0
        local done  = 0
        local count = 0

        if not old then

            if zipfile then
                zipfile = addsuffix(what,"zip")
            end
            if zipfile then
                local zipurl = url .. "/" .. zipfile
                report("fetching %a",zipurl)
                local zipdata = fetch(zipurl)
                if zipdata then
                    savedata(zipfile,zipdata)
                else
                    osremove(zipfile)
                    zipfile = false
                end
            end

            if type(zipfile) == "string" and isfile(zipfile) then

                -- todo: pcall

                report("unzipping %a",zipfile)

                local specification = {
                    zipname = zipfile,
                    path    = ".",
                 -- verbose = true,
                    verbose = "steps",
                    collect = true, -- sort of a check
                }

                if utilities.zipfiles.unzipdir(specification) then
                    osremove(zipfile)
                    goto done
                else
                    osremove(zipfile)
                end
            end

            count = #new

            report("installing %s, %i files",tree,count)

            for i=1,count do
                total = total + new[i][2]
            end

            for i=1,count do
                local entry  = new[i]
                local name   = entry[1]
                local size   = entry[2]
                local target = joinfile(tree,name)
                done = done + size
                if not skiplist or not skiplist[basename(name)] then
                    local message = download("new",url,target,total,done)
                    if message then
                        report("%s",message)
                    end
                else
                    report("skipping %s",target)
                end
            end

            ::done::

        else

            report("updating %s, %i files",tree,#new)

            local hold = hashed(old)
            local hnew = hashed(new)
            local todo = { }

            for newname, newhash in sortedhash(hnew) do
                local target = joinfile(tree,newname)
                if not skiplist or not skiplist[basename(newname)] then
                    local oldhash = hold[newname]
                    local action  = nil
                    if not oldhash then
                        action = "added"
                    elseif oldhash[3] ~= newhash[3] then
                        action = "changed"
                    elseif not ispresent(joinfile(tree,newname)) then
                        action = "missing"
                    end
                    if action then
                        local size = newhash[2]
                        total = total + size
                        todo[#todo+1] = {
                            action = action,
                            target = target,
                            size   = size,
                            hash   = newhash[3],
                        }
                    end
                else
                    report("skipping %s",target)
                end
            end

            count = #todo

            for i=1,count do
                local entry = todo[i]
                for i=1,5 do
                    local target  = entry.target
                    local message = download(entry.action,url,target,total,done,entry.hash)
                    if message then
                        if i == 5 then
                            report("%s, try again later: %s",target)
                            os.exit()
                        else
                            report("%s, trying again: %s",target)
                            os.sleep(2)
                        end
                    else
                        break
                    end
                end
                done = done + entry.size
            end

            for oldname, oldhash in sortedhash(hold) do
                local newhash = hnew[oldname]
                local target  = joinfile(tree,oldname)
                if not newhash and ispresent(target) then
                    remove("removed",target)
                end
            end

        end

        savetable(lua,new)

        return { tree, count, done }

    end

    local targetroot = dir.current()

    local server     = environment.arguments.server   or ""
    local instance   = environment.arguments.instance or ""
    local osplatform = environment.arguments.platform or nil
    local platform   = platforms[osplatform or os.platform or ""]

    if (platform == "unknown" or platform == "" or not platform) and osplatform then
        -- catches openbsdN.M kind of specifications
        platform = osplatform
    elseif not osplatform then
        osplatform = platform
    end

    if server == "" then
        server = "lmtx.contextgarden.net,lmtx.pragma-ade.com,lmtx.pragma-ade.nl,dmz.pragma-ade.nl"
    end
    if instance == "" then
        instance = "install-lmtx"
    end
    if not platform then
        report("unknown platform")
        return
    end

    local list   = utilities.parsers.settings_to_array(server)
    local server = false

    for i=1,#list do
        local host = list[i]
        local data, status, detail = fetch(protocol .. "://" .. host .. "/" .. instance .. "/tex/status.tma")
        if status == 200 and type(data) == "string" then
            local t = loadstring(data)
            if type(t) == "function" then
                t = t()
            end
            if type(t) == "table" and t.name == "context" and t.version == "lmtx" then
                server = host
                break
            end
        end
    end

    if not server then
        report("provide valid server and instance")
        return
    end

    local url = protocol .. "://" .. server .. "/" .. instance .. "/"

    local texmfplatform = "texmf-" .. platform

    report("server   : %s",server)
    report("instance : %s",instance)
    report("platform : %s",osplatform)
    report("system   : %s",ostype)

    local status   = { }
    local skiplist = {
        ["mtxrun"]      = true,
        ["context"]     = true,
        ["mtxrun.exe"]  = true,
        ["context.exe"] = true,
    }

    status[#status+1] = update(url,"texmf",true)
    status[#status+1] = update(url,"texmf-context",true)
    status[#status+1] = update(url,texmfplatform,false,skiplist)

    prepare("texmf-cache")
    prepare("texmf-project")
    prepare("texmf-fonts")
    prepare("texmf-local")
    prepare("texmf-modules")

    local binpath = joinfile(targetroot,"tex",texmfplatform,"bin")

    local luametatex = "luametatex"
    local luatex     = "luatex"
    local mtxrun     = "mtxrun"
    local context    = "context"

    if ostype == "windows" then
        luametatex = addsuffix(luametatex,"exe")
        luatex     = addsuffix(luatex,"exe")
        mtxrun     = addsuffix(mtxrun,"exe")
        context    = addsuffix(context,"exe")
    end

    local luametatexbin = joinfile(binpath,luametatex)
    local luatexbin     = joinfile(binpath,luatex)
    local mtxrunbin     = joinfile(binpath,mtxrun)
    local contextbin    = joinfile(binpath,context)

    local cdir = currentdir()
    local pdir = pushdir(binpath)

    report("current  : %S",cdir or "<unable to check the curent path>")
    report("target   : %S",pdir or "<unable to change to the binary path>")

    if pdir ~= cdir then

        report("removing : %s",mtxrun)
        report("removing : %s",context)

        osremove(mtxrun)
        osremove(context)

        if isfile(luametatex) then
            lfs.symlink(luametatex,mtxrun)
            lfs.symlink(luametatex,context)
        end

        if isfile(mtxrun) then
            report("linked   : %s",mtxrun)
        else
            copyfile(luametatex,mtxrun)
            report("copied   : %s",mtxrun)
        end
        if isfile(context) then
            report("linked   : %s",context)
        else
            copyfile(luametatex,context)
            report("copied   : %s",context)
        end

    end

    popdir()

    if lfs.setexecutable(luametatexbin) then
        report("xbit set : %s",luametatexbin)
    else
     -- report("xbit bad : %s",luametatexbin)
    end
    if lfs.setexecutable(luatexbin) then
        report("xbit set : %s",luatexbin)
    else
     -- report("xbit bad : %s",luatexbin)
    end
    if lfs.setexecutable(mtxrunbin) then
        report("xbit set : %s",mtxrunbin)
    else
     -- report("xbit bad : %s",mtxrunbin)
    end
    if lfs.setexecutable(contextbin) then
        report("xbit set : %s",contextbin)
    else
     -- report("xbit bad : %s",contextbin)
    end

    run("%s --generate",mtxrunbin)
    if environment.argument("erase") then
        run("%s --script cache --erase",mtxrunbin)
        run("%s --generate",mtxrunbin)
    end
    run("%s --make en", contextbin)

    -- in case we also install luatex:

    run("%s --luatex --generate",contextbin)
    run("%s --luatex --make en", contextbin)

    -- in calling script: update mtxrun.exe and mtxrun.lua

    report("")
    for i=1,#status do
        report("%-20s : %4i files with %9i bytes installed",unpack(status[i]))
    end
    report("")
    disclaimer()
    report("")

    report("update, done")

end

function install.modules()
    report("--modules in not yet implemented")
end

function install.fonts()
    report("--fonts in not yet implemented")
end

function install.goodies()
    report("--goodies in not yet implemented")
end

if environment.argument("secure") then
    usecurl = checkcurl()
    if usecurl then
        protocol = "https"
    else
        report("no curl installed, quitting")
        os.exit()
    end
end

io.output():setvbuf("no")

if environment.argument("identify") then
    install.identify()
elseif environment.argument("install") then
    install.update()
elseif environment.argument("update") then
    install.update()
elseif environment.argument("modules") then
    install.modules()
elseif environment.argument("fonts") then
    install.fonts()
elseif environment.argument("goodies") then
    install.goodies()
elseif environment.argument("exporthelp") then
    application.export(environment.argument("exporthelp"),environment.files[1])
else
    application.help()
    report("")
    disclaimer()
end