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

--
-- This is file `pfarrei.tlu',
-- generated with the docstrip utility.
--
-- The original source files were:
--
-- pfarrei.dtx  (with options: `pfarrei,lua')
-- 
-- Copyright (c) 2013 Markus Kohm
--                    komascript at gmx info
-- 
-- This file was generated from file(s) of the work `pfarrei'.
-- ------------------------------------------------------------------
-- 
-- It may be distributed under the conditions of the
-- LaTeX Project Public License in the version distributed together
-- with the work `pfarrei'.  You may however distribute the work
-- `pfarrei' without all such generated files.  See also
-- <http://www.latex-project.org/lppl.txt> for additional
-- information.
-- 
-- This work has the LPPL maintenance status `maintained'.
-- 
-- The Current Maintainer of this work is Markus Kohm.
-- 
-- The list of files belonging to the work `pfarrei' is given in
-- the file `pfarrei.dtx'.
-- 
local version_number = string.sub( '$Revision: 36 $', 12, -2 )
local action_version = ' r' .. version_number .. '\n' .. [[

Copyright (c) 2013 Markus Kohm.
License: lppl 1.3c or later. See <http://www.latex-project.org/lppl.txt>.
]]
local action_help = [[
action options:

  -h, --help            Print this help message.
  -V, --version         Print the version information.

processing options:
  -b, --booklet         Generate a booklet instead of only two pages side by
                        side onto one page.  The whole booklet will be one
                        signature.
  -s, --sidebyside      Generate only two pages side by side onto one page
                        instead of a booklet.
  -o, --overwrite       Write the output to the <PDF file> instead of appending
                        "-sidebyside.pdf" or "--booklet.pdf" to the basename
                        of <PDF file>
]]
local action_opts = {
   ['-h']           = 'help',
   ['--help']       = 'help',
   ['-V']           = 'version',
   ['--version']    = 'version',
}
local processing_opts = {
   ['-b']           = 'booklet',
   ['--booklet']    = 'booklet',
   ['-s']           = 'sidebyside',
   ['--sidebyside'] = 'sidebyside',
   ['-o']           = 'overwrite',
   ['--overwrite']  = 'overwrite',
   ['-d']           = 'debug',
   ['--debug']      = 'debug',
}

-- detect action options and do action
local i = 1
local action
while arg[i] do
   action = action_opts[arg[i]]
   i = i+1
   if     action == 'help' then
      print( arg[0]..action_version );
      print( 'Usage: ' .. arg[0] .. ' <action option>' )
      print( '       ' .. arg[0] .. ' [<processing options>] <PDF file> ...\n' )
      print( action_help );
      os.exit( 0 );
   elseif action == 'version' then
      print( arg[0] .. action_version );
      os.exit( 0 );
   end
end

-- process options and parameters
local booklet = false
local overwrite = false
local debug = false
i = 1
while arg[i] do
   action = processing_opts[arg[i]]
   if     action == 'booklet' then booklet = true
   elseif action == 'sidebyside' then booklet = false
   elseif action == 'overwrite' then overwrite = true
   elseif action == 'debug' then debug = true
   elseif action == nil then
      -- build the temporary tex file
      local tmpdir = os.tmpdir("pfarrei.XXXXXX" )
      local tmpfile = string.match( arg[i], '.*/(.*)$') or arg[i]
      local basename = string.match( tmpfile,'(.*)%.[^.]*$') or tmpfile
      tmpfile = tmpdir..'/'..basename..'.tex'
      local file = assert( io.open( tmpfile, 'w' ) )
      if booklet then assert( file:write("\\PassOptionsToPackage{booklet}{pfarrei}\n") ) end
      assert( file:write("\\def\\OriginalFile{",arg[i],"}\n") )
      assert( file:write("\\input{a5toa4.tex}\n") )
      assert( file:flush() )
      file:close()
      -- call pdflatex
      assert( os.execute( 'pdflatex -interaction=batchmode -output-directory='..tmpdir..' '..tmpfile ) )
      -- copy the resulting pdf file
      local srcfile = assert( io.open( tmpdir..'/'..basename..'.pdf', 'rb' ) )
      if overwrite
      then
         tmpfile = arg[i]
      else
         tmpfile = string.match( arg[i], '(.*)%.[^.]*$' ) or arg[i]
         if booklet
         then
            tmpfile = tmpfile..'-booklet.pdf'
         else
            tmpfile = tmpfile..'-sidebyside.pdf'
         end
      end
      local destfile = assert( io.open( tmpfile, 'wb' ) )
      local buffer
      while true do
         buffer = srcfile:read(8388608)
         if buffer==nil then break end
         assert( destfile:write(buffer) )
      end
      assert( destfile:close() )
      srcfile:close()
      if debug
      then
         print('DEBUG: Temporary files in: '..tmpdir);
      else
         tmpfile=tmpdir..'/'..basename
         os.remove( tmpfile..'.aux' )
         os.remove( tmpfile..'.tex' )
         os.remove( tmpfile..'.log' )
         os.remove( tmpfile..'.pdf' )
         os.remove( tmpdir )
      end
      overwrite = false
   end
   i=i+1
end
-- 
--
-- End of file `pfarrei.tlu'.