#! /usr/bin/env python

import json
import pipes
import sys
import pwd
import grp
import os

NAME = "digital-paper-edit-api"
UID = pwd.getpwnam(NAME).pw_uid
GID = grp.getgrnam(NAME).gr_gid


def write_env(content, path):
    f = open(path, "w")
    for key, value in content.items():
        f.write("%s=%s\n" % (key, pipes.quote(value)))
    f.close()


def merge_two_dicts(x, y):
    z = x.copy()
    z.update(y)
    return z


def main(argv):
    out_dir = os.path.join("/usr/lib/", NAME)
    path = os.path.join(out_dir, ".env.override")

    raw = json.load(open(argv[1], "r"))
    configs = merge_two_dicts(
        raw["configuration"],
        raw["secure_configuration"]
    )
    write_env(configs, path)
    os.chown(path, UID, GID)
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv))
