import os, sys
from ..util import check_file, capture_input
from ..run import run_new

def new_project(cmd):
    """Creates a new project.
       
    @param args: The command line parameters.
    
    As listed under cchrrrd.py new_project -h the following command line 
    parameters are accepted by new_project.
    
    name: The name of the project.
    
    project_dir: The path to the top level directory for the BJSConvert output.
    
    in_dir: The path to the directory containing the NLETS raw bar delimited 
            segment files and the spss sample file *(required)*.
                     
    meta_dir: The path to the directory containing the project metadata files. 
              Default: **root_dir**/bjs_rules/metadata.
    
    rule_dir: The path to the directory containing the project rules.
              Default: **root_dir**/bjs_rules/rules.
                     
    follow_period: The number of years after the date of release arrest 
                   to include in the recidivism database and flat files. 
                   
    threads: The number of threads to use when running the recidivism 
             transform phase. Default: 250
    
    cores: The number of cores the system has available to use. Default: 4
    
    verbose: A debug option to set debug logging to verbose. Default: False
    
    host: Name of database host to connect to. Default: localhost.
    
    port: TCP port of MySQL server. Default: standard port (3306).
    
    db_stand_name: Name of standardized database. Default: **project name**
    
    db_stand_user: The user to authenticate as. Default: root. 
    
    db_stand_pass: The password to authenticate with. Default: pass.
    
    db_recid_name: Name of recidivism database. Default: **project name**_recid
    
    db_recid_user: The user to authenticate as. Default: root. 
    
    db_recid_pass: The password to authenticate with. Default: pass.
    
    """
    args = cmd.args
    #if no project name added ask user for the name
    if not args.name:
        args.name = capture_input('Please enter the name of the project: ', **{'required':True})
    
    #if no project directory argument supplied get from user
    if not args.project_dir:
        args.project_dir = capture_input('Please specify the path: ', **{'required':True})
        
    if not os.path.isdir(args.project_dir):
        os.makedirs(args.project_dir)
    if os.path.isdir(os.path.join(args.project_dir, 'project_data', args.name)):
        print 'Project already exists, try a different name.'
        sys.exit()
    
    _db_s = args.db_stand_name if args.db_stand_name else args.name
    _db_r = '%s_recid' % (args.db_stand_name if args.db_stand_name else args.name)
    
    for dir in (args.project_dir, args.in_dir, 
                args.metadata_dir, args.rule_dir):
        if dir:
            check_file(dir, as_dir = True)
        
    opt = {'name': args.name,
           'root_dir': args.project_dir,
           'in_dir': args.in_dir,
           'meta_dir': args.metadata_dir,
           'rule_dir': args.rule_dir,
           'threads': args.threads,
           'cores': args.cores,
           'verbose': args.verbose,
           'host': args.host,
           'port': args.port,
           'follow_period': args.follow_period,
           'db_stand_name': _db_s,
           'db_stand_user': args.user,
           'db_stand_pass': args.pwd,
           'db_recid_name': _db_r,
           'db_recid_user': args.user,
           'db_recid_pass': args.pwd,
          }
    
    run_new(**opt)