#!/usr/bin/env python
# import models as mb
from shutil import copyfile
import os
import sys
import sofos
BDIR = os.path.dirname(sofos.__file__)
TEMPLATE_PATH = os.path.join(BDIR, "templates")


def main():
    print('Generating a new project')
    if len(sys.argv) <= 1:
        print('path is missing')
        return False
    counter = 0
    dir1 = sys.argv[1]
    indir = os.path.abspath(dir1)  # Destination directory
    # Create destination directory if not exists
    if not os.path.exists(indir):
        os.mkdir(indir)
    # Create uforms directory if not exists
    uforms_path = os.path.join(indir, 'zforms')
    if not os.path.exists(uforms_path):
        os.mkdir(uforms_path)
        init_py = os.path.join(uforms_path, '__init__.py')
        with open(init_py, 'w'):
            pass
    # Copy files
    for fnam in os.listdir(TEMPLATE_PATH):
        # If fnam is a directory do nothing
        if os.path.isdir(os.path.join(TEMPLATE_PATH, fnam)):
            continue
        copyfile(os.path.join(TEMPLATE_PATH, fnam), os.path.join(indir, fnam))
        print('copied %s' % fnam)
        counter += 1
    # Copy images
    img = 'images'
    simg = os.path.join(TEMPLATE_PATH, img)
    dimg = os.path.join(indir, img)
    # Create image destination directory if not exists
    if not os.path.exists(dimg):
        os.mkdir(dimg)
    # Copy image files
    for file in os.listdir(simg):
        # If files is a directory do nothing
        if os.path.isdir(os.path.join(simg, file)):
            continue
        copyfile(os.path.join(simg, file), os.path.join(dimg, file))
        print('copied %s' % file)
        counter += 1
    print('%s files copied !!!' % counter)


if __name__ == '__main__':
    main()
