#!/usr/bin/python

# This file generates the constants from Moira sources.
# The first argument is path to those sources.

import sys, re, os, os.path

##### Constants #####

# This constant is generated by compile_et from 'sms' table name
et_base = 47836416
et_path = "lib/mr_et.et"

##### Read the file #####

basepath = sys.argv[1]
if not os.path.isdir(basepath):
    print "ERROR: the specified path is not a directory"
    exit()

with open(basepath + "/lib/mr_et.et", "r") as et_file_handler:
    et_file = et_file_handler.read()
with open(basepath + "/include/moira.h", "r") as header_file_handler:
    header_file = header_file_handler.read()

et_match_version = re.search( r'\$Id: (.+) \$', et_file )
et_match_entries = re.findall( r'ec\s*(MR_[A-Z0-9_]+),\s*"([^"]+)"', et_file )
header_match_version = re.search( r'\$Id: (.+) \$', header_file )
header_match_entries = re.findall( r'#define (MR_\S+|UNIQUE_\S+)\s+(".+"|[0-9x\-]+)', header_file )

if not et_match_entries or not et_match_version:
    print "ERROR: unable to parse mr_et file correctly"
    exit()
if not header_match_entries or not header_match_version:
    print "ERROR: unable to parse moira.h file correctly"
    exit()


et_version = et_match_version.group(1)
header_version = header_match_version.group(1)

##### Code file header #####
print "# Moira status codes and other constants, generated from Moira sources"
print "#"
print "# NOTE: this file was autogenerated from the following files:"
print "#         %s" % et_version
print "#         %s" % header_version
print ""

##### Moira error codes #####
print "# Error codes"
cur_code = et_base
for match in et_match_entries:
    print '%s = %s' % (match[0], repr(cur_code))
    cur_code += 1
print ""

print "# Error code descriptions"
print "errors = {"
for match in et_match_entries:
    print '    %s : "%s",' % match
print "}"
print ""

##### Constatns from moira.h #####
print "# Definitions from moira.h"
for match in header_match_entries:
    print '%s = %s' % match
print ""
