#!/usr/bin/env python

import os
import argparse
from distutils.dir_util import copy_tree

import eve_utils

def add_docker(project_name):
    print(f'Creating docker files for {project_name}')

    skel = os.path.join(os.path.dirname(eve_utils.__file__), 'skel/docker')

    if not os.path.exists(f'{project_name}'):
        print(f'Please run this in the folder above {project_name}')
    else:
        copy_tree(skel, '.')

        # TODO: potentially dangerous to traverse folder from here?
        for dname, dirs, files in os.walk('.'):
            for fname in files:
                fpath = os.path.join(dname, fname)
                with open(fpath) as f:
                    s = f.read()
                s = s.replace("{$project_name}", project_name)
                with open(fpath, "w") as f:
                    f.write(s)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('api_name', help='The name of the API to add docker to.')

    args = parser.parse_args()
    project_name = args.api_name  # TODO: validate, safe name, etc.
    add_docker(project_name)
    

if __name__ == '__main__':
    main()
