#!/usr/bin/env python

import os
import re
import simplejson
import sys

def printenv(data, ns=[]):
    for key,value in data.items():

        # Ignore the entire AWS::CloudFormation::Init section. It can contain
        # custom scripts to be run by cloud-init, and it's problematic to escape those
        # properly in the template for json2kv.
        # Since parent keys are added to the ns list with underscores, we also
        # need to check if AWS__CloudFormation__Init is in the namespace.
        if key == "AWS::CloudFormation::Init" or "AWS__CloudFormation__Init" in ns:
            continue

        key = re.sub('[\\.\\-:]', '_', key)
        if isinstance(value, str):
            print "%s=%s"%("_".join(ns+[key]), value)
        elif isinstance(value, dict):
            print "%s=1"%("_".join(ns+[key]))
            printenv(value, ns+[key])

if __name__ == '__main__':
    # Find the first file that exists and use it
    for jsonfile in sys.argv[1:]:
        if not os.path.isfile(jsonfile):
            continue
        break
    else:
        exit(1)

    with open(jsonfile) as fp:
        data = simplejson.loads(fp.read())
        printenv(data)
        
