#!/usr/bin/python

# need to do some path manipulations
import os

names = {}
nameValues = {}

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

outfile = open( os.path.normpath( destination_dir + '/Universal.xsd'), 'w' )
outfile.write( '<?xml version="1.0" encoding="UTF-8"?>\n' )
outfile.write( '<schema targetNamespace="Tethys/Universal"\n' )
outfile.write( '    elementFormDefault="qualified"\n' )
outfile.write( '    xmlns="http://www.w3.org/2001/XMLSchema"\n' ) 
outfile.write( '    xmlns:Universal="Tethys/Universal"\n' ) 
outfile.write( '    xmlns:Tethys="Tethys">\n' )
outfile.write( '\n' )
outfile.write( '    <import namespace="Tethys" schemaLocation="http://okeanids.mbari.org/tethys/Xml/Tethys.xsd"/>\n' )
outfile.write( '\n' )

# we are using regular expression matching
import re
pattern = re.compile('[\s]*const[\s]+UniversalURI[\s]+UniversalURI::([\w_]+)[\s]*[\(][\s]*\"([^\"]+)\"')

infile = open( os.path.normpath("../Source/data/UniversalURI.cpp"))
for line in infile.readlines():
    matches = pattern.match(line)
    if matches:
        outfile.write( '    <element id="' + matches.group(1) )
        outfile.write( '" name="' + matches.group(2) )
        outfile.write( '" substitutionGroup="Tethys:UniversalUri" type="Tethys:UniversalUriType"/>\n' )

infile.close()

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