package xyz.kusoft.argusupdater

import java.io.File

internal class ArgusUpdateCache(private val directory: File) {
    @Synchronized
    fun prepareTarget(): File {
        clear()
        check(directory.mkdirs() || directory.isDirectory) {
            "Не удалось подготовить каталог обновления."
        }

        val target = File(directory, PACKAGE_FILE_NAME).canonicalFile
        check(target.parentFile == directory.canonicalFile) {
            "Некорректный путь временного APK."
        }
        return target
    }

    @Synchronized
    fun clear() {
        if (!directory.exists()) return

        directory.listFiles()?.forEach { it.deleteRecursively() }
        directory.delete()
    }

    companion object {
        const val DIRECTORY_NAME = "argus-updater"
        const val PACKAGE_FILE_NAME = "pending-update.apk"
    }
}
