#!/usr/bin/env python3

import json
import sys

pod_name = sys.argv[1]
json_file_path = sys.argv[2]
dest_folder = sys.argv[3]

def create_file(file_name, data):
    '''
    create a file: file name is based on the key on the json with appended path.
    '''
    with open(file_name, 'w') as out_file:
        for key, value in data.items():
            if isinstance(value, int) or isinstance(value, float):
                value = str(value)
            if isinstance(value, bool):
                value = str(value).lower()
            if len(value) == 0:
                out_file.write(key + "\n")
            else:
                out_file.write(key+"="+value+"\n")

def read_file(file_name):
    '''
    Read a file and print each line
    '''
    with open(file_name, 'r') as out_file:
        lines = out_file.readlines()
        for line in lines:
            sys.stdout.write(line)
            

def read_json_file(file_name):
    '''
    read a json file
    '''
    with open(file_name, 'r') as file:
        data = json.load(file)
    return data
##
## create required files
d = read_json_file(json_file_path)
for key in d:
    dest_file_path = "/".join([dest_folder, key])
    create_file(dest_file_path, d[key])
    print(dest_file_path)
    read_file(dest_file_path)
