summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/m-tx/m-tx.lua
blob: 3ed578563edcd97595b2ae6be9ba63d98b26d29c (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
#!/usr/bin/env texlua  

VERSION = "0.3"

--[[
     m-tx.lua: processes MusiXTeX files using prepmx and pmxab as pre-processors (and deletes intermediate files)

     (c) Copyright 2011-2012 Bob Tennent rdt@cs.queensu.ca

     This program is free software; you can redistribute it and/or modify it
     under the terms of the GNU General Public License as published by the
     Free Software Foundation; either version 2 of the License, or (at your
     option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
     Public License for more details.

     You should have received a copy of the GNU General Public License along
     with this program; if not, write to the Free Software Foundation, Inc.,
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

--]]

--[[

  ChangeLog:
     version 0.3   2012-04-09 RDT
       Change name to m-tx to avoid clash with another program.

     version 0.2   2011-11-28 RDT
       Added -i (retain intermediate files) option

     version 0.1.1 2011-07-18 RDT

       Removed a file check that caused it to fail (only on Windows!)

     version 0.1   2011-07-15 RDT

--]]

function usage()
  print("Usage:  [texlua] m-tx.lua { option | basename[.mtx] } ... ")
  print("options: -v  version")
  print("         -h  help")
  print("         -l  latex (or pdflatex)")
  print("         -p  pdfetex (or pdflatex)")
  print("         -d  dvipdfm")
  print("         -s  stop at dvi")
  print("         -t  stop at tex/mid")
  print("         -m  stop at pmx")
  print("         -i  retain intermediate files")
  print("         -f  restore default processing")
end

function whoami ()
  print("This is m-tx.lua version ".. VERSION .. ".")
end

whoami()
if #arg == 0 then
  usage()
  os.exit(0)
end

-- defaults:
prepmx = "prepmx"
pmx = "pmxab"
tex = "etex"  
musixflx = "musixflx"
dvi = "dvips"
ps2pdf = "ps2pdf"
intermediate = 1

exit_code = 0
narg = 1
repeat
  this_arg = arg[narg]
  if this_arg == "-v" then
    os.exit(0)
  elseif this_arg == "-h" then
    usage()
    os.exit(0)
  elseif this_arg == "-l" then
    if tex == "pdfetex" then
      tex = "pdflatex"
    else
      tex = "latex"
    end
  elseif this_arg == "-p" then
    if tex == "latex" then
      tex = "pdflatex"
    else
      tex = "pdfetex"
    end
    dvi = ""; ps2pdf = ""
  elseif this_arg == "-d" then
    dvi = "dvipdfm"; ps2pdf = ""
  elseif this_arg == "-s" then
    dvi = ""; ps2pdf = ""
  elseif this_arg == "-i" then
    intermediate = 0
  elseif this_arg == "-f" then
    pmx = "pmxab"; tex = "etex"; dvi = "dvips"; ps2pdf = "ps2pdf"; intermediate = 1
  elseif this_arg == "-t" then
    tex = ""; dvi = ""; ps2pdf = ""
  elseif this_arg == "-m" then
    pmx = ""; tex = ""; dvi = ""; ps2pdf = ""
  else
    filename = this_arg 
    if filename ~= "" and string.sub(filename, -4, -1) == ".mtx" then
        filename = string.sub(filename, 1, -5)
    end
    print("Processing ".. filename .. ".mtx.")
    OK = true
    if ( os.execute(prepmx .. " " .. filename ) ~= 0 ) then
      OK = false
    end
    if  (OK and pmx ~= "") then 
      os.execute(pmx .. " " .. filename)  -- does pmx return an error code?
      pmxaerr = io.open("pmxaerr.dat", "r")
      if (not pmxaerr) then
        print("No log file.")
        OK = false
      else
        linebuf = pmxaerr:read()
        err = tonumber(linebuf)
        if (err ~= 0) then
          OK = false
        end
        pmxaerr:close()
      end
    end
    os.remove( filename .. ".mx2" )
    if ( OK ) and
       ((tex == "") or os.execute(tex .. " " .. filename) == 0) and
       ((tex == "") or os.execute(musixflx .. " " .. filename) == 0) and
       ((tex == "") or os.execute(tex .. " " .. filename) == 0) and
       ((tex ~= "latex" and tex ~= "pdflatex") 
         or (os.execute(tex .. " " .. filename) == 0)) and
       (dvi == "" or  (os.execute(dvi .. " " .. filename) == 0)) and
       (ps2pdf == "" or (os.execute(ps2pdf .. " " .. filename .. ".ps") == 0) )
    then 
      if ps2pdf ~= "" then 
        print(filename .. ".pdf generated by " .. ps2pdf .. ".")
      end
      if intermediate == 1 then -- clean-up:
        os.remove( "pmxaerr.dat" )
        os.remove( filename .. ".mx1" )
        os.remove( filename .. ".mx2" )
        if pmx ~= "" then
          os.remove( filename .. ".pmx" )
        end
        if dvi ~= "" then
          os.remove( filename .. ".dvi" )
        end
        if ps2pdf ~= "" then 
          os.remove( filename .. ".ps" )
        end
      end
    else
      print("M-Tx/pmx/MusiXTeX processing of " .. filename .. ".mtx fails.\n")
      exit_code = 2
      --[[ uncomment for debugging
      print("tex = ", tex)
      print("dvi = ", dvi)
      print("ps2pdf = ", ps2pdf)
      --]]
    end
  end --if this_arg == ...
  narg = narg+1
until narg > #arg 
os.exit( exit_code )