Update src/main/kotlin/Main.kt

This commit is contained in:
Maxwell Jeffress 2024-10-01 11:11:54 +10:00
parent f73a32e72c
commit 7ff00d7cf0

View File

@ -15,7 +15,6 @@ import java.nio.file.Files
import java.nio.file.Paths import java.nio.file.Paths
import kotlin.collections.listOf as listOf import kotlin.collections.listOf as listOf
fun uncompressXZFile(inputFile: String, outputFile: String) { fun uncompressXZFile(inputFile: String, outputFile: String) {
XZInputStream(FileInputStream(inputFile)).use { input -> XZInputStream(FileInputStream(inputFile)).use { input ->
FileOutputStream(outputFile).use { output -> FileOutputStream(outputFile).use { output ->
@ -53,6 +52,33 @@ fun md5(input:String): String {
return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0') return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0')
} }
suspend fun grabDependencies(input:String): String {
val dependencyList = Fuel.get("https://git.maxwellj.xyz/mapm/stable/raw/branch/main/$input/dependencies").body.string()
return(dependencyList)
}
suspend fun grabPackage(input:String): Int {
println("Downloading package $input...")
val linkToUse = "https://git.maxwellj.xyz/mapm/stable/raw/branch/main/$input/source"
val packageLocation = Fuel.get(linkToUse).body.string()
val file = File("/usr/mapm/tmp/$input.tar.xz")
val fileBytes = Fuel.get(packageLocation).body.bytes()
file.writeBytes(fileBytes)
val fileChecksum = Fuel.get("https://git.maxwellj.xyz/mapm/stable/raw/branch/main/$input/hash").body.string()
if (fileChecksum == md5(file.readText())) {
println("Checksum matches!")
uncompressXZFile(inputFile = "/usr/mapm/tmp/${input}.tar.xz", outputFile = "/usr/mapm/tmp/${input}.tar")
unarchiveTarFile(tarFilePath = "/usr/mapm/tmp/${input}.tar", outputDirectoryPath = "/usr/mapm/")
Runtime.getRuntime().exec("chmod +x /usr/mapm/${input}")
println("Finished!")
return(0)
} else {
println("Checksum doesn't match! Aborting...")
println("Please tell Max or your app's developer to use mapm to generate a new checksum. If the checksum is right, check your internet connection security.")
return(1)
}
}
suspend fun main(args: Array<String>) { suspend fun main(args: Array<String>) {
if (args.size < 2) { if (args.size < 2) {
if (args.size == 1) { if (args.size == 1) {
@ -84,32 +110,25 @@ suspend fun main(args: Array<String>) {
if (userInput == "g") { if (userInput == "g") {
val userInput: String = args[1] val userInput: String = args[1]
val linkToUse = "https://git.maxwellj.xyz/mapm/stable/raw/branch/main/$userInput/source" val linkToUse = "https://git.maxwellj.xyz/mapm/stable/raw/branch/main/$userInput/source"
val dingus = Fuel.get(linkToUse).body.string() val packageLocation = Fuel.get(linkToUse).body.string()
println("Package $userInput found at $dingus") val dependencies = grabDependencies(userInput)
// val packageList = listOf($userInput) println("Downloading packages $userInput $dependencies")
println("Downloading package $userInput")
println("Is this okay?") println("Is this okay?")
val userInputInteractive: String = readln() val userInputInteractive: String = readln()
if (userInputInteractive != "y") { if (userInputInteractive != "y") {
println("Confirmation not given. Exiting...") println("Confirmation not given. Exiting...")
exitProcess(0) exitProcess(0)
} }
val file = File("/usr/mapm/tmp/$userInput.tar.xz") var currentPackage = ""
val fileBytes = Fuel.get(dingus).body.bytes() for (char in dependencies) {
file.writeBytes(fileBytes) if (char.toString() != " ") {
val fileChecksum = Fuel.get("https://git.maxwellj.xyz/mapm/stable/raw/branch/main/$userInput/hash").body.string() currentPackage += char.toString()
if (fileChecksum == md5(file.readText())) {
println("Checksum matches!")
uncompressXZFile(inputFile = "/usr/mapm/tmp/${userInput}.tar.xz", outputFile = "/usr/mapm/tmp/${userInput}.tar")
unarchiveTarFile(tarFilePath = "/usr/mapm/tmp/${userInput}.tar", outputDirectoryPath = "/usr/mapm/")
Runtime.getRuntime().exec("chmod +x /usr/mapm/${userInput}")
println("Finished!")
exitProcess(0)
} else { } else {
println("Checksum doesn't match! Aborting...") grabPackage(currentPackage)
println("Please tell Max or your app's developer to use mapm to generate a new checksum. If the checksum is right, check your internet connection security.") var currentPackage = ""
exitProcess(1)
} }
}
grabPackage(userInput)
} else if (userInput == "r") { } else if (userInput == "r") {
val executable = File("/usr/mapm/${args[1]}") val executable = File("/usr/mapm/${args[1]}")
println("Deleting package ${args[1]}") println("Deleting package ${args[1]}")