from sys import exit
from shutil import rmtree
from os.path import join
from os import listdir
from ..configuration import config
from ..util import check_file


root_and_subdirs = [
join(config.tmp_out_dir,'activate'),
join(config.tmp_out_dir,'cleanup'),
join(config.tmp_out_dir,'compare'),
join(config.tmp_out_dir,'copy_rules'),
join(config.tmp_out_dir,'load_recid_db'),
join(config.tmp_out_dir,'load_recid_db'),
join(config.tmp_out_dir,'load_recid_flat'),
join(config.tmp_out_dir,'load_rules'),
join(config.tmp_out_dir,'new_project'),
join(config.tmp_out_dir,'pre_process'),
join(config.tmp_out_dir,'process'),
join(config.tmp_out_dir,'stage'),
join(config.tmp_out_dir,'update_metadata'),
join(config.tmp_out_dir,'transform'),
join(config.tmp_out_dir,'master'), 
]

subdirs = [
join(config.out_dir,'process'),
config.log_dir,
]

def cleanup(cmd):
    args = cmd.args
    print """You are about to delete all temporary files.""" 
 
    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 = None
        elif ans in ('n', 'no'):
            exit()
        else:
            for d in root_and_subdirs:
                if d in [join(config.tmp_out_dir,'master'), join(config.tmp_out_dir,'transform')]:
                    ans = raw_input('%s contains files necessary for load recidivism database and flat files. ARE YOU SURE YOU WANT TO DELETE? (yes to continue)' % join(d, _d)).lower()
                    if ans == 'yes': 
                        rmtree(join(d, _d))
                    else:
                        print 'skipping %s' % d
                else:
                    x=1#rmtree(d)
                
            for d in subdirs:
                for _d in listdir(d):
                    if not args.verbose:
                        print 'deleting %s' % join(d, _d)
                    ans = raw_input('Do you wish to delete %s?' % join(d, _d)).lower()
                    if ans in ['y', 'yes']: 
                        rmtree(join(d, _d))
                    else:
                        print 'skipping %s' % join(d, _d)
    
    print 'Cleanup complete.'
            
            
    