import os,shutil
import fileinput

dir_path = os.path.dirname(os.path.realpath(__file__))
print dir_path


##set files that need to be "cleaned" or ereased
file_to_clean = "//PLAYERS//conf.cfg"

                   ## Disable beta boot screen
lines_to_replace = {'"boot_to_selection" : true' : '"boot_to_selection" : false,',
                   ## Reset boot to game optio vaikka tan ei kylla pitais olla edes enaa paalla koskaan!
                   '"boot_to_game" : true,' : '"boot_to_game" : false,',
                   ## Disable crash TESTING
                   '"enable_crash_TESTING" : true,' : '"enable_crash_TESTING" : false,',
                   ## Disable print to log 
                   '"print_to_log" : true,' : '"print_to_log" : false,',
                   ## Disable all logging
                   '"disable_cclog" : false,' : '"disable_cclog" : true,',
                   ## Disable dev cheats
                   '"dev_cheats" : true,' : '"dev_cheats" : false,',
                   ## Game complete counter
                   '"game_complete_counter"' : '"game_complete_counter" : 0,',  
                   ## Set to full screen
                   '"full_screen" : false,' : '"full_screen" : true,' ,
                   ## Reset first resolution check state
                   '"use_default_resolution" : false,' : '"use_default_resolution" : true,',
                   ## Hide FPSa and draw label
                   '"show_fps_and_draw_count" : true,' : '"show_fps_and_draw_count" : false,',
                   ## Reset resolution
                   '"res_h"' : '"res_h" : 1080,',
                   '"res_w"' : '"res_w" : 1920,'}

erase_files_that_end_with = ["tlog","dmp"]

def cleanConfFile():
    print "CLEANING CONF FILE"
    path_to_conf = dir_path + "\\" + file_to_clean
    for line in fileinput.input(path_to_conf, inplace=True):
        replace_this_line_with_string = ""
        for key, value in lines_to_replace.iteritems():
            if line.find(key) != -1:
                replace_this_line_with_string = value
                break

        if len(replace_this_line_with_string) > 1:
            print replace_this_line_with_string
        else:
            print line.rstrip()

def eraseNotNeededFiles():
    print "ERASE TEMP FILES"
    for file_line in os.listdir(dir_path):
        for file_end in erase_files_that_end_with:
            if file_line.endswith(file_end):
                path_to_file = dir_path + "\\" + file_line
                print path_to_file
                os.remove(path_to_file)

def cleanPlayersFolder():
    print "CLEAN PLAYERS FOLDER"
    folder = dir_path + "\\PLAYERS"
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)

        if os.path.isfile(file_path):
            os.unlink(file_path)
        elif os.path.isdir(file_path): shutil.rmtree(file_path)

cleanConfFile()
eraseNotNeededFiles()
##cleanPlayersFolder()
