mad1231999

Mad-DL v2.0

Jan 12th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.63 KB | None | 0 0
  1. local fp = ".mad-dl" -- Filepath
  2.  
  3. local function getPath(path)
  4.    if flib.exists(path) then
  5.       local lastSlashPos = 1
  6.       for i = 1, path:len() do
  7.          if path:sub(i, i) == "/" then
  8.             lastSlashPos = i
  9.          end
  10.       end
  11.      
  12.       return path:sub(1, lastSlashPos)
  13.    end
  14.  
  15.    return ""
  16. end
  17.  
  18. function string:split(sep)
  19.         local sep, fields = sep or ":", {}
  20.         local pattern = string.format("([^%s]+)", sep)
  21.         self:gsub(pattern, function(c) fields[#fields+1] = c end)
  22.         return fields
  23. end
  24.  
  25. local function initFilePath(_fp)
  26.     local f = io.open(_fp .. "/repos.dat", "w");
  27.     f:write("http://madsc.webege.com/CC/mad_dl/repo\nEND_REPO_LIST")
  28.     f:close()
  29. end
  30.  
  31. local function getRepoList()
  32.     local f = io.open(fp .. "/repos.dat", "r")
  33.     local l = f:read("*l")
  34.  
  35.     local t = {}
  36.  
  37.     while l ~= "END_REPO_LIST" do
  38.         print("Found repository:\n" .. l .. "\n")
  39.         t[#t + 1] = l
  40.  
  41.         l = f:read("*l")
  42.     end
  43.  
  44.     f:close()
  45.  
  46.     return t
  47. end
  48.  
  49. local function reloadCache()
  50.     local f = io.open(fp .. "/cache", "w")
  51.  
  52.     for _, repo in ipairs(getRepoList()) do
  53.         local dat = http.get(repo .. "/.mad_dl").readAll()
  54.  
  55.         f:write(dat:gsub("START INDEX", "START INDEX " .. repo) .. "\n\n# END OF REPO " .. repo .. "\n\n")
  56.     end
  57.  
  58.     f:write("END_CACHE")
  59.     f:close()
  60. end
  61.  
  62. local function checkPackageExistance(pkg)
  63.     if(not fs.exists(fp .. "/cache")) then
  64.         io.write("Cache does not exist. Automatically update cache[Y/n]? ")
  65.  
  66.         local ans = io.read()
  67.        
  68.         if ans:lower() == ("y" or "yes") then
  69.             reloadCache()
  70.         else
  71.             print("Exiting...")
  72.             return false
  73.         end
  74.     end
  75.  
  76.     print("Reading through cache...")
  77.  
  78.     local f = io.open(fp .. "/cache", "r")
  79.     local l = f:read("*l")
  80.  
  81.     local cLines = {}
  82.  
  83.     while l ~= "END_CACHE" do
  84.         -- print("Found repository " .. l)
  85.         cLines[#cLines + 1] = l
  86.  
  87.         l = f:read("*l")
  88.     end
  89.  
  90.     f:close()
  91.  
  92.     local inIndex = false
  93.     local hostRepo = ""
  94.  
  95.     for _, line in ipairs(cLines) do
  96.         if line ~= nil then
  97.             if inIndex then
  98.                 local words = string.split(line, " ")
  99.  
  100.                 if words ~= nil then
  101.                     if words[1] ~= nil and words[2] ~= nil then
  102.                         if words[1] == "END" and words[2] == "INDEX" then
  103.                             inIndex = false
  104.                             -- print("Outside INDEX")
  105.                         else
  106.                             -- print(words[1] .. "    " .. words[2])
  107.  
  108.                             if words[1]:lower() == pkg:lower() then
  109.                                 return true, words[2], hostRepo
  110.                             end
  111.                         end
  112.                     end
  113.                 end
  114.             else
  115.                 local words = string.split(line, " ")
  116.  
  117.                 if words ~= nil then
  118.                     if words[1] ~= nil and words[2] ~= nil then
  119.                         if words[1] == "START" and words[2] == "INDEX" then
  120.                             inIndex = true
  121.                             hostRepo = words[3]
  122.                             -- print("Inside INDEX")
  123.                         end
  124.                     end
  125.                 end
  126.             end
  127.         end
  128.     end
  129.  
  130.     return false
  131. end
  132.  
  133. local function attemptInstall(pkg)
  134.     if fs.exists(pkg) then
  135.         io.write("A directory with the same name already exists locally.\nOverride[y/N]? ")
  136.         local ans = io.read()
  137.  
  138.         if ans:lower() == "y" or ans:lower() == "yes" then else
  139.             print("Ok. Cancelling...")
  140.             return false
  141.         end
  142.     end
  143.  
  144.     local exists, path, hostRepo = checkPackageExistance(pkg)
  145.     -- print(exists and "Package " .. pkg .. " exists in path:\n" .. path or "Package " .. pkg .. " does not exist.")
  146.  
  147.     if not exists then
  148.         print("Package " .. pkg .. " does not exist. Cancelling.")
  149.         return false
  150.     end
  151.  
  152.     print("Installing package " .. pkg .. " from path:\n" .. hostRepo .. "/" .. path .. "\n")
  153.  
  154.     fs.makeDir(pkg)
  155.  
  156.     local data = http.get(hostRepo .. "/" .. path)
  157.     local f = io.open(fp .. "/temp_pkg", "w")
  158.     f:write(data.readAll())
  159.     f:close()
  160.  
  161.     f = io.open(fp .. "/temp_pkg", "r")
  162.     local l = f:read("*l")
  163.     local lines = {}
  164.  
  165.     while l ~= "END PKG" do
  166.         lines[#lines + 1] = l:gsub("__PKG__", pkg)
  167.  
  168.         l = f:read("*l")
  169.     end
  170.  
  171.     local inContent = false
  172.  
  173.     for _, line in ipairs(lines) do
  174.         if line ~= nil then
  175.             if inContent then
  176.                 local words = string.split(line, " ")
  177.  
  178.                 if words ~= nil then
  179.                     if words[1] ~= nil and words[2] ~= nil then
  180.                         if words[1] == "END" and words[2] == "CONTENT" then
  181.                             inContent = false
  182.                             -- print("Outside INDEX")
  183.                         else
  184.                             -- print(words[1] .. "    " .. words[2])
  185.  
  186.                             if words[1]:lower() == "cmd_make_dir" then
  187.                                 fs.makeDir(words[2])
  188.                             else
  189.                                 if words[1]:find("#") == nil then
  190.                                     print("Reading file:\n" .. hostRepo .. "/pkg_" .. pkg .. "/" .. words[1] .. "\n")
  191.                                     local curData = http.get(hostRepo .. "/pkg_" .. pkg .. "/" .. words[1])
  192.                                     local f = io.open(pkg .. "/" .. words[1], "w")
  193.                                     f:write(curData.readAll())
  194.                                     f:close()
  195.                                 end
  196.                             end
  197.                         end
  198.                     end
  199.                 end
  200.             else
  201.                 local words = string.split(line, " ")
  202.  
  203.                 if words ~= nil then
  204.                     if words[1] ~= nil and words[2] ~= nil then
  205.                         if words[1] == "START" and words[2] == "CONTENT" then
  206.                             inContent = true
  207.                         end
  208.                     end
  209.                 end
  210.             end
  211.         end
  212.     end
  213.  
  214.     print("Successfully installed package " .. pkg)
  215. end
  216.  
  217. local function addRepo(repo)
  218.     local f = io.open(fp .. "/repos.dat", "r")
  219.     local data = f:read("*a")
  220.     f:close()
  221.  
  222.     f = io.open(fp .. "/repos.dat", "w")
  223.     f:write(repo .. "\n" .. data)
  224.     f:close()
  225. end
  226.  
  227. local function help(arg)
  228.     local helpTexts = {
  229.         ["recache"] = "Reloads the cache. This should be called before downloading a package.";
  230.         ["check"] = "Checks if a package exists. Usage:\n\nmad-dl check <package name>\n";
  231.         ["install"] = "Installs a package, if it exists. Usage:\n\nmad-dl install <package name>\n";
  232.         ["addrepo"] = "Adds a lookup-repository to this Mad-DL installation. This will allow Mad-DL to look on multiple servers for a package.\nUsage:\n\nmad-dl addrepo <URL>\n";
  233.         ["none"] = "List of commands:\n\nrecache\ncheck\ninstall\naddrepo\nhelp\n\nRun 'mad-dl help <command>' to see help for a specific command.";
  234.         ["help"] = "Shows the help page. Usage:\n\nmad-dl help <command>\n";
  235.     }
  236.  
  237.     if arg == nil then
  238.         print(helpTexts["none"])
  239.     else
  240.         if helpTexts[arg] == nil then
  241.             print("No help available.")
  242.         else
  243.             print(helpTexts[arg])
  244.         end
  245.     end
  246. end
  247.  
  248. local function onStart(args)
  249.     if not fs.exists(fp) then
  250.         fs.makeDir(fp)
  251.         initFilePath(fp)
  252.     end
  253.  
  254.     local cmd = args[1]
  255.  
  256.     if cmd == nil then
  257.         help()
  258.     end
  259.  
  260.     if cmd:lower() == "recache" then
  261.         reloadCache()
  262.     end
  263.  
  264.     if cmd:lower() == "check" then
  265.         local exists, path = checkPackageExistance(args[2])
  266.         print(exists and "Package " .. args[2] .. " exists in path:\n" .. path or "Package " .. args[2] .. " does not exist.")
  267.     end
  268.  
  269.     if cmd:lower() == "install" then
  270.         attemptInstall(args[2])
  271.     end
  272.  
  273.     if cmd:lower() == "addrepo" then
  274.         addRepo(args[2])
  275.     end
  276.  
  277.     if cmd:lower() == "help" then
  278.         help(args[2])
  279.     end
  280. end
  281.  
  282. onStart({...})
Advertisement
Add Comment
Please, Sign In to add comment