#!/usr/bin/env python3
# This script generates the provd daemon configuration file based on the current
# device resources as specified by the user.
import sys
import central_dop.config


def main():
    deploy_dir = "/srv/deploy"
    config = central_dop.config.load_config_from_dir(deploy_dir)
    try:
        central_dop.config.validate_config(config)
    except central_dop.config.Error as err:
        print("error loading configuration: {}".format(err.message))
        sys.exit(1)

    # Write a provd configuration file for each of the groups in the deployment
    # list.
    try:
        for inv_name in list(config.get('deployment').keys()):
            cfg = central_dop.config.generate_provd_config(config, inv_name)
            with open("{}/{}/provd.conf".format(deploy_dir, inv_name), 'w') as f:
                f.write(cfg)
    except central_dop.config.Error as err:
        print("error generating provd configuration: {}".format(err.message))


if __name__ == "__main__":
    main()
