#!/usr/bin/env python

import PySablot
import Sablot
import getopt
import sys
import string

VERSION = "0.0.1"


class GetOpt:
  def __init__(self):
    # set defaults
    self.cmdopt = { 'in'             : None,
                    'xsl'            : None,
                    'help'           : 0,
                    'version'        : 0,
                    'out'            : None,
                    'manifest'       : None}
    self.cmdarg = []

  def parseOptions(self,args):  
    # Parse command line for options
    options, args = getopt.getopt(args[1:],
                                  'i:x:hv:o:m:',
                                  ['in=', 'xsl=','help','version', 'out=', 'manifest='])

    for opt in options:
      if opt[0] in ('--in','-i'):
        self.cmdopt['in'] = opt[1]
      elif opt[0] in ('--xsl','-x'):
        self.cmdopt['xsl'] = opt[1]
      elif opt[0] in ('--help','-h'):
        self.cmdopt['help'] = 1
      elif opt[0] in ('--version','-v'):
        self.cmdopt['version'] = 1
      elif opt[0] in ('--out', '-o'):
        self.cmdopt['out'] = opt[1]
      elif opt[0] in ('--manifest', '-m'):
        self.cmdopt['manifest'] = opt[1]

    for arg in args:
      self.cmdarg.append(arg)

  def getOption(self,option):
    return self.cmdopt[option]

  def getArgument(self,arg):
    return self.cmdarg[arg]


#
#  Start of Program
#

Options = GetOpt()
Options.parseOptions(sys.argv);

if Options.getOption('help') == 1:
  print """usage: pyxslt [option] [arguments]
Options and arguments:
-i : Input file.  This is necessary unless using manifest. (--in)
-o : Output file. This is necessary unless using manifest. (--out)
-x : Stylesheet file. This is necessary unless using manifest. (--xsl)
-m : Manifest listing.  You can use this to process multiple files. (--manifest)
-h : Get listing of help. (--help)
-v : Get version number. (--version)

Example:
pyxslt -i mysource.xml -x mystyle.xsl -o myoutput.html

or using manifest

pyxslt -m mymanifest.build

[Manifest would look like]
stylefile.xsl:infile.xml:outfile.html
stylefile.xsl:otherinfile.xml:otheroutfile.html
stylefile.xsl:otherinfile2.xml:otheroutfile2.html
"""

infile = Options.getOption('in')
style = Options.getOption('xsl')
outfile = Options.getOption('out')
manifest = Options.getOption('manifest')
if manifest == None:
  if infile == None:
    print "You must define an input file."
    sys.exit()
  if style == None:
    print "You must define a stylesheet."
    sys.exit()
  if outfile == None:
    print "You must define an output file."
    sys.exit()
  PySablot.ProcessFiles(style,infile,outfile)

else:
  mf = open(manifest)
  line = mf.readline()
  while line:
    files = string.split(line, ':', 3)
    PySablot.ProcessFiles(string.strip(files[0]),string.strip(files[1]),string.strip(files[2]))
    line = mf.readline()  
  mf.close()


P = Sablot.Sablot()
