#!/usr/bin/env python
# Copyright (c) 2014-2016 - Adjacent Link LLC, Bridgewater, New Jersey
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#  * Neither the name of Adjacent Link LLC nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# See toplevel COPYING for more information.
#

from optparse import OptionParser
from lxml import etree
import textwrap
import re
import os
import sys

usage = "%prog [OPTION].. NAMEFILES..."

optionParser =  OptionParser(usage=usage)

optionParser.add_option("-o",
                        "--output",
                        type="string",
                        dest="output",
                        default="output",
                        help="Base output file name [Default: %default]")

optionParser.add_option("-p",
                        "--prefix",
                        type="string",
                        dest="prefix",
                        default=".",
                        help="File output prefix [Default: %default]")

optionParser.add_option("--category",
                        type="string",
                        dest="category",
                        default="EMANE",
                        help="Probe category [Default: %default]")

(options, args) = optionParser.parse_args()

def normalize(name,skip=None):
    global thisindex
    global thiscount
    global options

    original = name;


    # remove new line
    name = name.rstrip()

    if skip:
        match = re.match("^%s(.*)" % skip,name)

        if match:
            name = skip+match.group(1).lower()
        else:
            name = name.lower()
    else:
        # lowe case the name
        name = name.lower()

    # remove trailing spaces
    name = re.sub('\s+$','',name)

    # convert continguos spaces to single '_'
    name = re.sub('\s+','_',name)

    # convert chratchers outside set to '_'
    name = re.sub('[^A-Za-z0-9\_]','_',name)

    # convert contiguous '_' to single '_'
    name = re.sub('_+','_',name)

    # remove trailing '_'
    name = re.sub('_$','',name)

    return (name,original)

schemeXSD=etree.XML("""\
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' >
  <xs:simpleType name='StatisticType'>
    <xs:restriction base='xs:token'>
      <xs:enumeration value='int64'/>
      <xs:enumeration value='uint64'/>
      <xs:enumeration value='int32'/>
      <xs:enumeration value='uint32'/>
      <xs:enumeration value='double'/>
      <xs:enumeration value='float'/>
      <xs:enumeration value='string'/>
    </xs:restriction>
  </xs:simpleType>
  <xs:element name='probes'>
    <xs:complexType>
      <xs:sequence>
        <xs:element name='statistics' maxOccurs='unbounded'>
          <xs:complexType>
            <xs:sequence>
              <xs:element name='probe' maxOccurs='unbounded'>
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name='entry' maxOccurs='unbounded'>
                      <xs:complexType>
                        <xs:attribute name='name' type='xs:string' use='required'/>
                        <xs:attribute name='type' type='StatisticType' use='required'/>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute name='name' type='xs:string' use='required'/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name='tables' maxOccurs='unbounded' minOccurs='0'>
          <xs:complexType>
            <xs:sequence>
              <xs:element name='probe' maxOccurs='unbounded'>
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name='entry' maxOccurs='unbounded'>
                      <xs:complexType>
                        <xs:attribute name='name' type='xs:string' use='required'/>
                        <xs:attribute name='type' type='xs:string' use='optional' default="MeasurementTable"/>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute name='name' type='xs:string' use='required'/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
""")

for ifile in args:
    thisindex = 1
    thiscount = {}
    subsystem = options.output
    category = options.category
    prefix = options.prefix

    tree = etree.parse(ifile)
    root = tree.getroot()

    schema = etree.XMLSchema(etree=schemeXSD,attribute_defaults=True)

    if not schema(root):
        message = ""
        for entry in schema.error_log:
            print >>sys.stderr, "%d: %s" % (entry.line,entry.message)
        exit(1)


    probes = {}
    tables = {}
    tableimports = set()
    specializedtables = set()

    for probe in root.xpath('/probes/statistics/probe'):
        probeName = category + "." + subsystem + "." + probe.get('name')
        probes[probeName] = [];

        for entry in probe.xpath('entry'):
            name,original = normalize(entry.get('name'))
            probes[probeName].append((name,entry.get('type'),original))

    for probe in root.xpath('/probes/tables/probe'):
        tableName =  category + "." + subsystem + "." + probe.get('name')
        tables[tableName] = [];

        for entry in probe.xpath('entry'):
            name,original = normalize(entry.get('name'))
            stype = entry.get('type')
            tables[tableName].append((name,stype,original))
            tableimports.add(stype.lower() + ".proto")
            if stype != 'MeasurementTable':
                specializedtables.add((stype,entry.get('name')))

    header="""\
/*
 * Copyright (c) 2014-2016 - Adjacent Link LLC, Bridgewater, New Jersey
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of Adjacent Link LLC nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * See toplevel COPYING for more information.
 */

 /*
  * !!!
  * !!! THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY
  * !!!
  */
"""
    ofd = open("%s/%s.proto" % (prefix,subsystem.lower()),"w")

    print >>ofd,header
    print >>ofd, "package OpenTestPoint;"
    print >>ofd
    print >>ofd, "option optimize_for = SPEED;"
    print >>ofd
    for proto in tableimports:
        print >>ofd, "import \"otestpoint/emane/%s\";" % proto
    print >>ofd

    for probe in sorted(probes.keys()):
        entries = probes[probe]
        i = 1
        print >>ofd
        print >>ofd, "message Measurement_%s" % (normalize(probe)[0])
        print >>ofd, "{"
        print >>ofd, "  message Description"
        print >>ofd, "  {"
        print >>ofd, "    required string name = 1 [default = \"Measurement_%s\"];" % (normalize(probe)[0])
        print >>ofd, "    required string module = 2 [default = \"otestpoint.emane.%s\"];" % subsystem.lower()
        print >>ofd, "    required uint32 version = 3 [default = 1];"
        print >>ofd, "  }"
        print >>ofd, "  // do not set description"
        print >>ofd, "  optional Description description = 1;"
        print >>ofd
        i+=1
        for name,stype,original in entries:
            print >>ofd, "  optional",stype,name,"=",i,";"
            i+=1
        print >>ofd, "}"


    for probe in sorted(tables.keys()):
        entries = tables[probe]
        i = 1
        print >>ofd
        print >>ofd, "message Measurement_%s" % (normalize(probe)[0])
        print >>ofd, "{"
        print >>ofd, "  message Description"
        print >>ofd, "  {"
        print >>ofd, "    required string name = 1 [default = \"Measurement_%s\"];" % (normalize(probe)[0])
        print >>ofd, "    required string module = 2 [default = \"otestpoint.emane.%s\"];" % subsystem.lower()
        print >>ofd, "    required uint32 version = 3 [default = 1];"
        print >>ofd, "  }"
        print >>ofd, "  // do not set description"
        print >>ofd, "  optional Description description = 1;"
        print >>ofd
        i+=1
        for name,stype,original in entries:
            print >>ofd, "   optional",stype,name,"=",i,";"
            i+=1
        print >>ofd, "}"

    ofd.close()




    ofd = open("%s/probe-%s-%s.xsd" % (prefix,category.lower(),subsystem.lower()),"w")


    print >>ofd,"""\
<!--
 Copyright (c) 2014-2016 - Adjacent Link LLC, Bridgewater, New Jersey
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.
  * Neither the name of Adjacent Link LLC nor the names of its
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
-->
<!-- THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY -->
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  <xs:simpleType name='YesNoType'>
    <xs:restriction base='xs:token'>
      <xs:enumeration value='yes'/>
      <xs:enumeration value='no'/>
    </xs:restriction>
  </xs:simpleType>
  <xs:element name='probe-%s-%s'>
    <xs:complexType>
      <xs:sequence>
        <xs:element name='probes'>
          <xs:complexType>
            <xs:sequence>""" % (category.lower(), subsystem)

    for probe in sorted(probes.keys()):
        print >>ofd,"""\
              <xs:element name='%s' minOccurs='0'>
                <xs:complexType>
                  <xs:attribute name='enable' type='YesNoType' use='optional' default="no"/>
                </xs:complexType>
              </xs:element>""" % probe

    for probe in sorted(tables.keys()):
        print >>ofd,"""\
              <xs:element name='%s' minOccurs='0'>
                <xs:complexType>
                  <xs:attribute name='enable' type='YesNoType' use='optional' default="no"/>
                </xs:complexType>
              </xs:element>""" % probe

    print >>ofd,"""\
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name='address' type='xs:string' use='required'/>
      <xs:attribute name='port' type='xs:unsignedShort' use='optional' default='47000'/>
      <xs:attribute name='nem' type='xs:unsignedShort' use='optional'/>
    </xs:complexType>
  </xs:element>
</xs:schema>
"""
