Advertisement
ENCIOK

ENCPAC

May 30th, 2024 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | Gaming | 0 0
  1. --TODO: implement -Syu
  2.  
  3. --IMPORTANT, THIS IS THE PACKAGE DATABASE
  4. local ENCDATA ="ENCIOK/59bd0e8e27b9d34a05b997064728082a/raw/database.json"
  5.  
  6. --check if the user is stupid
  7. if #arg == 0 then
  8.     print("error: no operation specified  (use encpac -h for help)")
  9.     return
  10. end
  11.  
  12. --open the package DB
  13. local ENCfile = fs.open("packagedb/ENCdb", "r")
  14. if ENCfile then
  15.     ENC = ENCfile.readAll()
  16.     ENC = textutils.unserialiseJSON(ENC)
  17.     ENCfile.close()
  18. else    
  19.     if arg[1] ~= "-Sy" then
  20.     print("WARNING didn't find package database")
  21.     end
  22. end
  23.  
  24. local function download(address)
  25.     local answer, err = http.get("https://gist.githubusercontent.com/".. address)
  26.     if not err then
  27.         local data = answer.readAll()
  28.         answer.close()
  29.         return data
  30.     end
  31.     printError(err)
  32.     return "poopoo"
  33. end
  34.  
  35. local function refresh()
  36.     print("synchronizing package database..")
  37.     print("ENC")
  38.     fs.delete("packagedb/ENCdb")
  39.     local file = fs.open("packagedb/ENCdb", "w")
  40.     local data = download(ENCDATA)
  41.     if data == "poopoo" then
  42.         return
  43.     end
  44.     file.write(data)
  45.     file.close()
  46.     print("done!")
  47. end
  48.  
  49. --getting all depends for package
  50. local function checkDepends(pacName, installList)
  51.     if ENC[pacName] then
  52.         if not installList[pacName] then
  53.             installList[pacName] = true
  54.         end
  55.         if ENC[pacName].depends then
  56.             for _, v in ipairs(ENC[pacName].depends) do
  57.                 checkDepends(v, installList)
  58.             end
  59.         else
  60.             return
  61.         end
  62.     else
  63.         printError("can't find package:", pacName)
  64.         return "poopoo"
  65.     end
  66. end
  67.  
  68. local function synchronize(pacName, path)
  69.     path = path or ""
  70.     --get depends
  71.     print("checking dependencies\n..")
  72.     local packsToInstall = {}
  73.     if checkDepends(pacName, packsToInstall) == "poopoo" then
  74.         return
  75.     end
  76.     print("no problems found, about to install the following:\n")
  77.     for v, _ in pairs(packsToInstall) do
  78.         io.write(v.." ")
  79.     end
  80.     print()
  81.     io.write("\nProceed with installation?  [Y/n] ")
  82.     local inp = io.read()
  83.     if inp == "" or inp == "Y" or inp == "y" then
  84.         print("installing:..")
  85.     else
  86.         return
  87.     end
  88.     for v, _ in pairs(packsToInstall) do
  89.         print(v)    
  90.         local data = download(ENC[v].url)
  91.         if data == "poopoo" then
  92.             return
  93.         end
  94.         local file = fs.open(path .. "/" .. ENC[v].install, "w")
  95.         file.write(data)
  96.         file.close()
  97.     end
  98.     print("done!")
  99. end
  100.  
  101. --lookup table for arguments
  102. local argtable = {
  103.     ["-S"] = synchronize,
  104.  
  105.     ["-Sl"] = function(a) synchronize(arg[3], a) end,
  106.  
  107.     ["-Sy"] = refresh,
  108.  
  109.     ["-h"] = function()
  110.  
  111.     end
  112. }
  113. if argtable[arg[1]] then
  114.     argtable[arg[1]](arg[2])
  115. else
  116.     printError("error: invalid option", arg[1])
  117. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement