#! /usr/bin/env python

from pyfeyn import __version__
from pyfeyn.config import *
from pyfeyn.feynml import *
from optparse import OptionParser
import sys, re


## Parse command line options
_usage = "Usage: %prog [options] hepmlfile"
_version = "%prog " + __version__
_parser = OptionParser(usage=_usage, version=_version)
config.addPyfeynOptions(_parser)
_parser.add_option("--pdf", action="store_true", dest="PDF_OUTPUT",
                   default=True, help="write output in PDF format")
_parser.add_option("--eps", action="store_false", dest="PDF_OUTPUT",
                   default=True, help="write output in EPS format")
_parser.add_option("-o", "--out", dest="OUTPUT_FILE",
                   default=None, help="file to write the output to. An appropriate file extension will be appended if needed")
(_opts, _args) = config.processOptions(_parser)


## Check right number of arguments
if len(_args) != 1:
    sys.stderr.write("Wrong number of arguments... exiting.\n")
    _parser.print_help()
    sys.exit(1)


## Select input and output files
_infile = _args[0]
_outfile = _infile
if _opts.OUTPUT_FILE:
    _outfile = _opts.OUTPUT_FILE


## Get the output file suffix right
_suffix = ".pdf"
if not _opts.PDF_OUTPUT:
    _suffix = ".eps"
if not re.search(r'%s$' % _suffix, _outfile):
    _outfile += _suffix


## Read the file and write out to the chosen filename
reader = FeynMLReader(_infile)
_f = reader.get_diagram(0)
_f.draw(_outfile)
