#!/usr/bin/env python

""" 
	Copyright (C) 2001 PimenTech SARL (http://www.pimentech.net)

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

	This library 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
	Library General Public License for more details.

	You should have received a copy of the GNU Library General Public
	License along with this library; see the file COPYING.LIB.  If not,
	write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
	Boston, MA 02111-1307, USA.  
"""

from libcommon.common import *
from string import *
import getopt
import sys
import os
from xtex2latex import *
from time import time


def main():
	"""
	xtex2pdf.py output pdf from xtex...

	usage: xtex2pdf.py -f <xtex file> [-h] 
	-h : this help
	-f : xtex file
	-o : pdf output
	-d : debug

	example : xtex2pdf -f test.xtex -o test.tex
	"""
	
	try:
		opts, args = getopt.getopt(sys.argv[1:], 'o:f:hd')
	except getopt.error, msg:
		usage(msg)

	debug = PYTHONDEBUG
	pdfFilename = xtexFilename = None
	for o, a in opts:
		if o == '-f': xtexFilename = a
		if o == '-h': usage(main.__doc__)
		if o == '-o': pdfFilename = a
		if o == '-d': debug = 1

	x = XTeX('Xtex',debug)
	
	xtexFilename = xtexFilename or stdin

	latexFileName = "/tmp/%s.tex" % time()
	output = open(latexFilename, 'w')
	x.read(xtexFilename, output)
	output.close()

	shell = "pdflatex /tmp/%s" % latexFileName
	if os.system(shell):
		raise shell
	if pdfFilename:
		output = open(pdfFilename, 'w')
	else:
		output = stdout

if __name__ == '__main__':
	main()








