#!/usr/bin/python
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Poor man's JSON parser.

This module reads the input JSON file, retrieves from it some name/value pairs
and generates a .h file to allow a C code use the definitions.

The JSON file name is required to be passed in in the command line, the nodes
this script pays attention to are included in required_keys tuple below.
"""

import json
import sys

required_keys = ('epoch', 'major', 'minor')


def main(json_file_name):
  # get rid of the comments
  json_text = []
  h_file_text = ['''
/*
 * Copyright %d The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* This file was autogenerated, do not edit. */
''',]

  json_file = open(json_file_name, 'r')
  for line in json_file.read().splitlines():
    json_text.append(line.split('//')[0])

  j = json.loads('\n'.join(json_text))

  for key in required_keys:
    if key in j.keys():
      value = j[key]
    else:
      value = '0'

    h_file_text.append('#define MANIFEST_%s %s' % (key.upper(), value))

  h_file_text.append('')
  return '\n'.join(h_file_text)


if __name__ == '__main__':
  print main(sys.argv[1])
