from .util import *

import subprocess

def find_apk():
    apks = list(SOURCE_APK_DIR.glob("*.apk"))
    if len(apks) == 0:
        print("Source APK is missing from 'source-apk' directory. Please make sure to copy it to the right directory.")
        exit(-1)
    elif len(apks) > 1:
        print("Multiple APKs were found in the 'source-apk' directory. Please only place the correct APK there.")
        exit(-1)

    return apks[0]

def extract():
    target_apk = find_apk()

    # Check if this task is necessary
    if EXTRACTED_DIR.exists() and EXTRACTED_DIR.stat().st_mtime > target_apk.stat().st_mtime:
        return "pass"
    
    # Extract the apk
    print("Extracting APK...")
    _ = subprocess.run([JAVA_BIN, '-jar', APKTOOL, 'd', str(target_apk.absolute()), '-o', EXTRACTED_DIR])