from sys import exit
from os.path import join, normpath
from shutil import copytree, rmtree

from ..configuration import config
from ..util import check_file

def copy_rules(cmd):
    args = cmd.args
    
    if args.src_project:
        _path = join(config.root_dir, 'project_data', args.src_project, 'rules')
        src = check_file(_path, as_dir=True)
    elif args.src:
        src = check_file(args.src, as_dir=True)
    else:
        src = config.rule_dir
    
    if args.dst_project:
        _path = join(config.root_dir, 'project_data', args.dst_project, 'rules')
        dst = check_file(_path, as_dir=True)
    elif args.dst:
        dst = check_file(args.dst, as_dir=True)
    else:
        dst = normpath(join(__file__, '..', '..', '..', 'build', 'rules'))
    
    print """You are about to copy
             %s 
             to
             %s""" % (src,dst)
 
    ans = None
    while not ans:
        ans = raw_input('Do you wish to proceed (yes or no)?').lower()
        if ans not in ('y', 'yes', 'n', 'no'):
            print '%s is not a valid option please enter yes or no.' % ans
            ans = None
        elif ans in ('n', 'no'):
            exit()
        else:
            rmtree(dst)
            copytree(src, dst) 
            rmtree(join(dst, 'crosswalk_raw'))
    