#! /usr/bin/python3

import os

def strip_arg(s):
  s = s.strip()
  if s and s[0] == '[':
    s = s[1:len(s)-1]
  return s.strip()

def parse_func(s, start=0):
  pos = s.find('(', start)
  func = s[start:pos].strip()

  args = []
  n_brackets=0
  n_parenthesis=1
  start = pos = pos + 1
  while n_parenthesis > 0:
    if s[pos] == '[':
      n_brackets += 1
    elif s[pos] == ']':
      n_brackets -= 1
    elif n_brackets == 0 and s[pos] == '(':
      n_parenthesis += 1
    elif n_brackets == 0 and s[pos] == ')':
      n_parenthesis -= 1
    elif n_brackets == 0 and n_parenthesis == 1 and s[pos] == ',':
      args.append(strip_arg(s[start:pos]))
      start = pos + 1
    pos += 1

  args.append(strip_arg(s[start:pos-1]))

  return func, args, pos

def parse_file(filename):
  content = ''
  with open(filename, 'r') as f:
    for line in f:
      if not line.startswith('dnl'):
        content += line

  output = ''
  next_pos = 0
  while True:
    start = content.find('AC_ARG_', next_pos)
    if start < 0:
      break

    func, args, next_pos = parse_func(content, start)

    if func.startswith('AC_ARG_VAR'):
      continue

    if args[1].startswith('AS_HELP_STRING') or args[1].startswith('AC_HELP_STRING'):
      help_args = parse_func(args[1])[1]
    else:
      help_args = ['', args[1]]

    type_str = ''
    value_str = ''
    choices = []
    equal_pos = help_args[0].find('=')
    if equal_pos > 0:
      val = help_args[0][equal_pos+1:]
      if val.startswith('@<:@'):
        choices = val[4:len(val)-4].split('/')
      elif val.find('|') > 0:
        choices = val.split('|')

      if choices:
        type_str = "'combo'"
        value_str = "''"
        if len(args) >= 4:
          for c in choices:
            if args[3].find(c) > 0:
              value_str = "'{}'".format(c)
              break
      else:
        type_str = "'string'"
        value_str = "''"
    else:
      type_str = "'boolean'"
      value_str = "true" if help_args[0].startswith('--disable') else "false"

    name = args[0].replace('_','-').lower()
    if content.startswith('AC_ARG_ENABLE', start):
      name = 'enable-' + name
    elif content.startswith('AC_ARG_WITH', start):
      name = 'with-' + name

    output += "option('{}',\n".format(name)
    output += "       type : {},\n".format(type_str)
    if choices:
      output += "       choices : {},\n".format(str(choices))
    output += "       value : {},\n".format(value_str)
    output += "       description : '{}')\n\n".format(help_args[1])     

  if output:
    output = '# {}\n\n'.format(filename) + output

  m4dir = None
  pos = content.find('AC_CONFIG_MACRO_DIR')
  if pos > 0:
    args = parse_func(content, pos)[1]
    m4dir = args[0]

  return output, m4dir

def parse_dir(path):
  output = ''
  for f in os.listdir(path):
    filename = os.path.join(path, f)
    output += parse_file(filename)[0]

  return output

if __name__ == "__main__":
  output, m4dir = parse_file('configure.ac')
  if m4dir:
    output += parse_dir(m4dir)
  else:
    output += parse_dir('m4')
    output += parse_dir('common/m4')

  print(output)

