#!/usr/bin/python -Wignore::DeprecationWarning

# -----------------------------------------------------------------------
#
# Copyright 2010 Reuben D. Budiardja (reuben -at- budiardja.org)
#
# 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 3 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.
#
# To receive a copy of the GNU General Public License
# see <http://www.gnu.org/licenses/>.
#
# -----------------------------------------------------------------------

from pyPdf import PdfFileWriter, PdfFileReader
import sys
import warnings

oddFile = PdfFileReader(file(sys.argv[1], "rb"))
evenFile = PdfFileReader(file(sys.argv[2], "rb"))

output = PdfFileWriter()
outputFile = file(sys.argv[3], "wb")

nOdd = oddFile.getNumPages()
nEven = evenFile.getNumPages()

if(nOdd != nEven):
  print "  Warning: Non-equal number of pages in the Odd and Even files ";

#-- add odd then even page
nMixed = min(nOdd, nEven)
for iPage in range(nMixed):
  output.addPage(oddFile.getPage(iPage))
  output.addPage(evenFile.getPage(iPage))

#-- dump the rest for unequal pages
for iPage in range(nMixed,nOdd):
  output.addPage(oddFile.getPage(iPage))
for iPage in range(nMixed,nEven):
  output.addPage(evenFile.getPage(iPage))

output.write(outputFile)
outputFile.close()
