from .util import *
from . import fileutil

import shutil

from polly.patcher import process_all_patches

# Task dependencies
from .extract import extract

def patch_smali():
    _ = extract()
    
    first_time = False
    
    # Copy original resources only the first time
    if not PATCHED_SMALI_DIR.exists():
        print("Copying original smali...")
        for smali_dir in EXTRACTED_DIR.glob("smali*"):
            if smali_dir.parts[-1] == 'smali_assets':
                continue # Not you, Seamus.
                
            shutil.copytree(smali_dir, PATCHED_SMALI_DIR / smali_dir.relative_to(EXTRACTED_DIR))
        first_time = True
    
    s = fileutil.merge_into(SRC_SMALI_DIR, PATCHED_SMALI_DIR, check_date=not first_time)
    if s:
        print("Copied custom code from src/smali")
    else:
        print("Skipped copying code from src/smali")
        
    # Patch other resources
    process_all_patches(SRC_SMALI_PATCHES_DIR, dest=PATCHED_SMALI_DIR, check_date=not first_time)