#!/usr/bin/env python

# Creates a temporary folder

import argparse
import logging
import os

from common.configuration import ConfigurationHandler
from common.workspace import WorkspaceHandler

logger = logging.getLogger('pyccflex')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
logger.addHandler(ch)

if __name__ == '__main__':

    logger.info("\n#### Running: {}".format(__file__))

    parser = argparse.ArgumentParser()
    parser.add_argument("--locations_config", help="Path to locations configuration file",
                        type=str, required=False, default="./locations.json")
    args = vars(parser.parse_args())
    logger.info("Run parameters: {}".format(str(args)))

    locations_file_path = args['locations_config']

    try:
        config = ConfigurationHandler(locations_file_path)
    except Exception as e:
        logger.error("Couldn't load configuration file {}".format(locations_file_path))
        exit(1)

    # create storage folder
    workspace_dir_conf = config.get('workspace_dir', None)
    if workspace_dir_conf is None:
        logger.info("Storage folder is required, please update your configuration file")
        exit(1)

    workspace_dir_conf = config.get('workspace_dir', None)
    workspace_dir_path = workspace_dir_conf.get("path", "")
    workspace_dir = WorkspaceHandler(workspace_dir_path)

    if os.path.exists(workspace_dir_path) and workspace_dir_conf.get("erase", False):
        workspace_dir.remove_workspace_dir()
    workspace_dir.create_workspace_dir()







