summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/context/lua/mtx-install.lua
blob: 46580bb20345adf149fcd8094996e58d5ff653f4 (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
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.00</entry>
 </metadata>
 <flags>
  <category name="basic">
   <subcategory>
    <flag name="platform" value="string"><short>platform (windows, linux, linux-64, osx-intel, osx-ppc, linux-ppc)</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="identify"><short>create list of files</short></flag>
   </subcategory>
  </category>
 </flags>
</application>
]]

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 joinfile, filesize, dirname, addsuffix = file.join, file.size, file.dirname, file.addsuffix
local isdir, isfile, walkdir = lfs.isdir, lfs.isfile, lfs.dir
local mkdirs, globdir = dir.mkdirs, dir.glob
local osremove, osexecute, ostype = os.remove, os.execute, os.type
local savedata = io.savedata
local formatters = string.formatters

local fetch = socket.http.request

local application = logs.application {
    name     = "mtx-install",
    banner   = "ConTeXt Installer 2.00",
    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",
    --
    ["mswin-64"]       = "win64",
    ["windows-64"]     = "win64",
    ["win64"]          = "win64",
    --
    ["linux"]          = "linux",
    ["linux-32"]       = "linux",
    ["linux32"]        = "linux",
    --
    ["linux-64"]       = "linux-64",
    ["linux64"]        = "linux-64",
    --
    ["linuxmusl-64"]   = "linuxmusl-64",
    --
    ["linux-armhf"]    = "linux-armhf",
    --
    ["freebsd"]        = "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",
    --
    ["osx-64"]         = "osx-64",
    --
    ["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 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 stamp = md5.hex(io.loaddata(name))
                details[i] = { base, size, stamp }
                total = total + size
            end
            report("%-20s : %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

end

function install.update()

    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)
        local data = fetch(url .. "/" .. target)
        if data 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 validdir(dirname(target)) then
                savedata(target,data)
            else
                -- message
            end
        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,tree)

        tree = joinfile("tex",tree)

        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

        if not old then

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

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

            for i=1,#new do
                local entry  = new[i]
                local name   = entry[1]
                local size   = entry[2]
                local target = joinfile(tree,name)
                done = done + size
                download("new",url,target,total,done)
            end

        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)
                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, target, size }
                end
            end

            for i=1,#todo do
                local entry = todo[i]
                download(entry[1],url,entry[2],total,done)
                done = done + entry[3]
            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)

    end

    local targetroot = dir.current()

    local server   = environment.arguments.server   or ""
    local port     = environment.arguments.port     or ""
    local instance = environment.arguments.instance or ""

    if server == "" then
        report("provide server")
        return
    end

    local url = "http://" .. server

    if port ~= "" then
        url = url .. ":" .. port
    end

    url = url .. "/"

    if instance ~= "" then
        url = url .. instance .. "/"
    end

    local osplatform = os.platform
    local platform   = platforms[osplatform]

    if not platform then
        report("unknown platform")
        return
    end

    local texmfplatform = "texmf-" .. platform

    report("server   : %s",server)
    report("port     : %s",port == "" and 80 or "80")
    report("instance : %s",instance)
    report("platform : %s",osplatform)
    report("system   : %s",ostype)

    update(url,"texmf")
    update(url,"texmf-context")
    update(url,texmfplatform)

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

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

    if ostype == "unix" then
        osexecute(formatters["chmod +x %s/*"](binpath))
    end

    local mtxrun  = joinfile(binpath,"mtxrun")
    local context = joinfile(binpath,"context")

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

    run("%s --generate",mtxrun)
    run("%s --make en", context)
    run("%s --make nl", context)

 -- local mtxrun  = joinfile(binpath,"mtxrunjit")
 -- local context = joinfile(binpath,"contextjit")
 --
 -- if ostype == "windows" then
 --     addsuffix(mtxrun,"exe")
 --     addsuffix(context,"exe")
 -- end
 --
 -- run("%s --generate",mtxrun)
 -- run("%s --make en", context)
 -- run("%s --make nl", context)

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

    report("update, done")
end

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