import os
import sys

from dotenv import load_dotenv


# This file should contain _zappa_utils at the end of its name in order to be excluded from deploying to lambda
# Refer to "exclude": ["*_zappa_utils.py"] in zappa_settings

def get_env_version():
    load_dotenv(sys.argv[1])
    return os.getenv('VERSION')


def main():
    if len(sys.argv) < 3:
        print(
            "you should specify your .env file name as the first argument and your aws profile_name as the second argument."
            "The ci for the second argument is reserved for continious integration purposes."
            " (python " + sys.argv[0] + " .env.production default)")
        exit(1)

    with open('zappa_settings_template.json', 'r') as file:
        file_data = file.read()

    version = get_env_version()
    file_data = file_data.replace('${version}', version)

    profile_name = sys.argv[2]
    if profile_name == "ci":
        file_data = file_data.replace('${profile_name_field}', '')
    else:
        file_data = file_data.replace('${profile_name_field}', '"profile_name": "' + profile_name + '",')

    with open('zappa_settings.json', 'w') as file:
        file.write(file_data)

    print("zappa_settings.json file generated successfully.")
    print(file_data)


if __name__ == '__main__':
    main()
