#!/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'.
