package com.topjohnwu.magisk.core.utils

import android.content.Context
import android.os.Build
import com.topjohnwu.magisk.DynAPK
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.*
import com.topjohnwu.magisk.ktx.cachedFile
import com.topjohnwu.magisk.ktx.deviceProtectedContext
import com.topjohnwu.magisk.ktx.rawResource
import com.topjohnwu.magisk.ktx.writeTo
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils
import java.io.File
import java.util.jar.JarFile

class RootInit : Shell.Initializer() {

    override fun onInit(context: Context, shell: Shell): Boolean {
        return init(context.wrap(), shell)
    }

    private fun init(context: Context, shell: Shell): Boolean {

        fun fastCmd(cmd: String) = ShellUtils.fastCmd(shell, cmd)
        fun getVar(name: String) = fastCmd("echo \$$name")
        fun getBool(name: String) = getVar(name).toBoolean()

        shell.newJob().apply {
            add("export SDK_INT=${Build.VERSION.SDK_INT}")
            add("export ASH_STANDALONE=1")

            val localBB: File
            if (isRunningAsStub) {
                val jar = JarFile(DynAPK.current(context))
                val bb = jar.getJarEntry("lib/${Const.CPU_ABI_32}/libbusybox.so")
                localBB = context.deviceProtectedContext.cachedFile("busybox")
                jar.getInputStream(bb).writeTo(localBB)
                localBB.setExecutable(true)
            } else {
                localBB = File(Const.NATIVE_LIB_DIR, "libbusybox.so")
            }

            if (!shell.isRoot) {
                // Directly execute the file
                add("exec $localBB sh")
            } else {
                // Copy it out of /data to workaround Samsung BS
                add(
                    "export MAGISKTMP=\$(magisk --path)/.magisk",
                    "if [ -x \$MAGISKTMP/busybox/busybox ]; then",
                    "  cp -af $localBB \$MAGISKTMP/busybox/busybox",
                    "  exec \$MAGISKTMP/busybox/busybox sh",
                    "else",
                    "  exec $localBB sh",
                    "fi"
                )
            }

            add(context.rawResource(R.raw.manager))
            if (shell.isRoot) {
                add(context.assets.open("util_functions.sh"))
            }
            add("mm_init")
        }.exec()

        Const.MAGISKTMP = getVar("MAGISKTMP")
        Info.isSAR = getBool("SYSTEM_ROOT")
        Info.ramdisk = getBool("RAMDISKEXIST")
        Info.isAB = getBool("ISAB")
        Info.crypto = getVar("CRYPTOTYPE")
        Info.isPixel = fastCmd("getprop ro.product.brand") == "google"

        // Default presets
        Config.recovery = getBool("RECOVERYMODE")
        Config.keepVerity = getBool("KEEPVERITY")
        Config.keepEnc = getBool("KEEPFORCEENCRYPT")

        return true
    }
}
