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

VERSION = "0.3"

--[[
     musixtex.lua: processes a MusiXTeX file and then deletes intermediate files

     (c) Copyright 2011 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  2011-04-25 RDT
       Add -d (dvipdfm)  and -s (stop at dvi) options.

     version 0.2  2011-04-21 RDT
       Allow basename.tex as filename.
       Add -p option for pdfetex processing.
       Add standard -v -h options.

--]]

function usage()
  print("Usage:  [texlua] musixtex.lua [option] [basename[.tex]]")
  print("options: -v  version")
  print("         -h  help")
  print("         -p  pdfetex")
  print("         -d  dvipdfm")
  print("         -s  stop at dvi")
end

function whoami ()
  print("This is musixtex.lua version ".. VERSION .. ".")
end

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

narg = 1

if arg[narg] == "-v" then
  whoami()
  os.exit(0)
elseif arg[narg] == "-h" then
  usage()
  os.exit(0)
end

-- defaults:
tex = "etex"  
musixflx = "musixflx"
dvi = "dvips -q"
ps2pdf = "ps2pdf"

if narg < #arg then
  if arg[narg] == "-p" then
    tex = "pdfetex"; dvi = ""; ps2pdf = ""
  elseif arg[narg] == "-d" then
    tex = "etex"; dvi = "dvipdfm"; ps2pdf = ""
  elseif arg[narg] == "-s" then
    tex = "etex"; dvi = ""; ps2pdf = ""
  else
    print("Argument \"".. arg[narg] .. "\" ignored.")
  end
  narg = narg+1
end 


filename = arg[narg] 
if not filename then
  usage()
  os.exit(1)
end

extension = string.sub(filename, -4, -1)
if extension == ".tex" then
  basename = string.sub(filename, 1, -5)
else
  basename = filename
end

if not io.open(basename .. ".tex", "r") then
  print("Non-existent file: ", basename .. ".tex")
  os.exit(2)
end

whoami()
print("Processing ".. basename .. ".tex")
os.remove( basename .. ".mx2" )
if (os.execute(tex .. " " .. basename) == 0) and
   (os.execute(musixflx .. " " .. basename) == 0) and
   (os.execute(tex .. " " .. basename) == 0) and
   (dvi == "" or  (os.execute(dvi .. " " .. basename) == 0)) and
   (ps2pdf == "" or (os.execute(ps2pdf .. " " .. basename .. ".ps") == 0) )
then
  if dvi ~= "" then
    print(basename .. ".pdf generated.")
  end
  exit_code = 0
else
  print("Musixtex processing fails.")
--[[ uncomment for debugging
  print("tex = ", tex)
  print("dvi = ", dvi)
  print("ps2pdf = ", ps2pdf)
--]]
  exit_code = 3
end

-- clean-up:
os.remove( basename .. ".mx1" )
os.remove( basename .. ".mx2" )
if dvi ~= "" then 
  os.remove( basename .. ".dvi" )
end
os.remove( basename .. ".ps" )

os.exit( exit_code )