#!/usr/bin/python
# --------------------------------------------------------------------------- #
# $Id: xml2pll,v 1.3 2004/01/15 20:07:35 weismann Exp $
# --------------------------------------------------------------------------- #

## contribution from Matthew Johnson, thanks
## http://www.matthew.ath.cx/programs/pointless

import sys
import os
if os.path.exists("POINTLESS_HACK"):
	sys.path.append("POINTLESS_HACK")
import ploptions   # options and their default settings
import plsetup     # resource setup stuff
import pllog       # log module
import plfiles     # filehandler
import pllmodules  # find

__id__ = "$Id: xml2pll,v 1.3 2004/01/15 20:07:35 weismann Exp $"

rxp = pllmodules.find_program("rxp")
xsltproc = pllmodules.find_program("xsltproc")
if not (rxp and xsltproc):
	sys.stderr.write("Using this tool requires rxp and xsltproc")
	sys.exit(0)

def convert():
	failed = os.system("%s -Vs %s " % (rxp, ploptions.inputfile))
	if failed:
		pllog.info("Input file does not appear to be in xml format")
	else:
		pllog.info("Input file appears to be in xml format")
		outfile=ploptions.tmpwork+"".join(os.path.basename(ploptions.inputfile).split('.')[:-1])+'.pll'
	if not os.path.isdir(os.path.dirname(outfile)):
		os.mkdir(os.path.dirname(outfile))
		print ("%s -o %s %s %s" %
			(xsltproc, outfile, plfiles.find_file(ploptions.template_xsl), ploptions.inputfile))
		pllog.info("Output in %s" % outfile )
	failed = os.system("%s -o %s %s %s" % 
			(xsltproc, outfile, plfiles.find_file(ploptions.template_xsl), ploptions.inputfile))
	if failed:
		pllog.abort("xsltproc called failed, error code: %s" % failed)
	else:
		pllog.info("Completed conversion, the pll file is located here %s" % outfile)

# --------------------------------------------------------------------------- #
# Main loop

def main():
	plsetup.process_args()
	pllog.debug("CVS id for this particular version %s" % __id__)
	convert()

# --------------------------------------------------------------------------- #

if __name__ == '__main__':
	main()

# --------------------------------------------------------------------------- #

