#!/usr/bin/python

#We need to escape xml arguments
from xml.sax.saxutils import quoteattr

#We need command line arguments - stored in sys.argv (including script name)
import sys
sourceFilename = sys.argv[1]

import re

# need to do some path manipulations
import os

addCommandPattern = re.compile('addCommand\s*\(\s*"([^"]+)"\s*,\s*"([^"]+)"\s*\,?\s*(true)?\s*\)')
addSyntaxPattern = re.compile('addSyntax\s*\(\s*"([^"]+)"[^\)]*\)')
addArgPattern = re.compile('addArg\s*\(\s*((?:ARG_\w+)|(?:"[^"]+"))\s*,?\s*(\w+)?\s*,?\s*("[^"]+")?\s*\)')

names = {}
nameValues = {}

destination_dir = os.getenv( 'XSD_DEST_DIR', '/mbari/LRAUV/xsd' )

outfile = open( os.path.normpath( destination_dir + '/Command.xml'), 'w' )
outfile.write( '<?xml version="1.0" encoding="UTF-8"?>\n' )
outfile.write( '<Commands\n' )
outfile.write( '    xmlns="Command"\n' )
outfile.write( '    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n')
outfile.write( '    xsi:schemaLocation="Command http://okeanids.mbari.org/tethys/Xml/Command.xsd">\n' )

inCommand = False
inSyntax = False
infile = open( os.path.normpath(sourceFilename) )
for line in infile.readlines():
    line = line.strip()
    addCommandMatches = addCommandPattern.search(line)
    if addCommandMatches:
        if inSyntax:
            outfile.write( '        </Syntax>\n' )
            inSyntax = False
        if inCommand:
            outfile.write( '    </Command>\n' )
            inCommand = False
        inCommand = True
        keyword = addCommandMatches.group(1)
        description = addCommandMatches.group(2)
        advanced = addCommandMatches.group(3)
        if advanced:
            outfile.write( '    <Command Keyword="%s" Description="%s" Advanced="true">\n' % (keyword,description) )
        else:
            outfile.write( '    <Command Keyword="%s" Description="%s">\n' % (keyword,description) )
        line = line[len(addCommandMatches.group(0)):].strip()
    addSyntaxMatches = addSyntaxPattern.search(line)
    if addSyntaxMatches:
        if inSyntax:
            outfile.write( '        </Syntax>\n' )
            inSyntax = False
        inSyntax = True
        help = addSyntaxMatches.group(1)
        outfile.write( '        <Syntax Help="%s">\n' % help )
        line = line[len(addSyntaxMatches.group(0)):].strip()
    while True:
        addArgMatches = addArgPattern.search(line)
        if addArgMatches:
            if addArgMatches.group(1).startswith('"'):
                keywordStr = ' Keyword=' + addArgMatches.group(1)
                argType = 'ARG_KEYWORD'
            else:
                keywordStr = ''
                argType = addArgMatches.group(1)
            outfile.write( '            <%s%s' % (argType,keywordStr) )
            required = addArgMatches.group(2)
            if required:
                outfile.write( ' Required="%s"' % (required) )
            altName = addArgMatches.group(3)
            if altName:
                outfile.write( ' AltName=%s' % (altName) )
            outfile.write( '/>\n' )
            line = line[len(addArgMatches.group(0)):].strip()
        else:
            break
if inSyntax:
    outfile.write( '        </Syntax>\n' )
    inSyntax = False
if inCommand:
    outfile.write( '    </Command>\n' )
    inCommand = False

infile.close()

outfile.write( '</Commands>\n' )
outfile.close()