Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fp = ".mad-dl" -- Filepath
- local function getPath(path)
- if flib.exists(path) then
- local lastSlashPos = 1
- for i = 1, path:len() do
- if path:sub(i, i) == "/" then
- lastSlashPos = i
- end
- end
- return path:sub(1, lastSlashPos)
- end
- return ""
- end
- function string:split(sep)
- local sep, fields = sep or ":", {}
- local pattern = string.format("([^%s]+)", sep)
- self:gsub(pattern, function(c) fields[#fields+1] = c end)
- return fields
- end
- local function initFilePath(_fp)
- local f = io.open(_fp .. "/repos.dat", "w");
- f:write("http://madsc.webege.com/CC/mad_dl/repo\nEND_REPO_LIST")
- f:close()
- end
- local function getRepoList()
- local f = io.open(fp .. "/repos.dat", "r")
- local l = f:read("*l")
- local t = {}
- while l ~= "END_REPO_LIST" do
- print("Found repository:\n" .. l .. "\n")
- t[#t + 1] = l
- l = f:read("*l")
- end
- f:close()
- return t
- end
- local function reloadCache()
- local f = io.open(fp .. "/cache", "w")
- for _, repo in ipairs(getRepoList()) do
- local dat = http.get(repo .. "/.mad_dl").readAll()
- f:write(dat:gsub("START INDEX", "START INDEX " .. repo) .. "\n\n# END OF REPO " .. repo .. "\n\n")
- end
- f:write("END_CACHE")
- f:close()
- end
- local function checkPackageExistance(pkg)
- if(not fs.exists(fp .. "/cache")) then
- io.write("Cache does not exist. Automatically update cache[Y/n]? ")
- local ans = io.read()
- if ans:lower() == ("y" or "yes") then
- reloadCache()
- else
- print("Exiting...")
- return false
- end
- end
- print("Reading through cache...")
- local f = io.open(fp .. "/cache", "r")
- local l = f:read("*l")
- local cLines = {}
- while l ~= "END_CACHE" do
- -- print("Found repository " .. l)
- cLines[#cLines + 1] = l
- l = f:read("*l")
- end
- f:close()
- local inIndex = false
- local hostRepo = ""
- for _, line in ipairs(cLines) do
- if line ~= nil then
- if inIndex then
- local words = string.split(line, " ")
- if words ~= nil then
- if words[1] ~= nil and words[2] ~= nil then
- if words[1] == "END" and words[2] == "INDEX" then
- inIndex = false
- -- print("Outside INDEX")
- else
- -- print(words[1] .. " " .. words[2])
- if words[1]:lower() == pkg:lower() then
- return true, words[2], hostRepo
- end
- end
- end
- end
- else
- local words = string.split(line, " ")
- if words ~= nil then
- if words[1] ~= nil and words[2] ~= nil then
- if words[1] == "START" and words[2] == "INDEX" then
- inIndex = true
- hostRepo = words[3]
- -- print("Inside INDEX")
- end
- end
- end
- end
- end
- end
- return false
- end
- local function attemptInstall(pkg)
- if fs.exists(pkg) then
- io.write("A directory with the same name already exists locally.\nOverride[y/N]? ")
- local ans = io.read()
- if ans:lower() == "y" or ans:lower() == "yes" then else
- print("Ok. Cancelling...")
- return false
- end
- end
- local exists, path, hostRepo = checkPackageExistance(pkg)
- -- print(exists and "Package " .. pkg .. " exists in path:\n" .. path or "Package " .. pkg .. " does not exist.")
- if not exists then
- print("Package " .. pkg .. " does not exist. Cancelling.")
- return false
- end
- print("Installing package " .. pkg .. " from path:\n" .. hostRepo .. "/" .. path .. "\n")
- fs.makeDir(pkg)
- local data = http.get(hostRepo .. "/" .. path)
- local f = io.open(fp .. "/temp_pkg", "w")
- f:write(data.readAll())
- f:close()
- f = io.open(fp .. "/temp_pkg", "r")
- local l = f:read("*l")
- local lines = {}
- while l ~= "END PKG" do
- lines[#lines + 1] = l:gsub("__PKG__", pkg)
- l = f:read("*l")
- end
- local inContent = false
- for _, line in ipairs(lines) do
- if line ~= nil then
- if inContent then
- local words = string.split(line, " ")
- if words ~= nil then
- if words[1] ~= nil and words[2] ~= nil then
- if words[1] == "END" and words[2] == "CONTENT" then
- inContent = false
- -- print("Outside INDEX")
- else
- -- print(words[1] .. " " .. words[2])
- if words[1]:lower() == "cmd_make_dir" then
- fs.makeDir(words[2])
- else
- if words[1]:find("#") == nil then
- print("Reading file:\n" .. hostRepo .. "/pkg_" .. pkg .. "/" .. words[1] .. "\n")
- local curData = http.get(hostRepo .. "/pkg_" .. pkg .. "/" .. words[1])
- local f = io.open(pkg .. "/" .. words[1], "w")
- f:write(curData.readAll())
- f:close()
- end
- end
- end
- end
- end
- else
- local words = string.split(line, " ")
- if words ~= nil then
- if words[1] ~= nil and words[2] ~= nil then
- if words[1] == "START" and words[2] == "CONTENT" then
- inContent = true
- end
- end
- end
- end
- end
- end
- print("Successfully installed package " .. pkg)
- end
- local function addRepo(repo)
- local f = io.open(fp .. "/repos.dat", "r")
- local data = f:read("*a")
- f:close()
- f = io.open(fp .. "/repos.dat", "w")
- f:write(repo .. "\n" .. data)
- f:close()
- end
- local function help(arg)
- local helpTexts = {
- ["recache"] = "Reloads the cache. This should be called before downloading a package.";
- ["check"] = "Checks if a package exists. Usage:\n\nmad-dl check <package name>\n";
- ["install"] = "Installs a package, if it exists. Usage:\n\nmad-dl install <package name>\n";
- ["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";
- ["none"] = "List of commands:\n\nrecache\ncheck\ninstall\naddrepo\nhelp\n\nRun 'mad-dl help <command>' to see help for a specific command.";
- ["help"] = "Shows the help page. Usage:\n\nmad-dl help <command>\n";
- }
- if arg == nil then
- print(helpTexts["none"])
- else
- if helpTexts[arg] == nil then
- print("No help available.")
- else
- print(helpTexts[arg])
- end
- end
- end
- local function onStart(args)
- if not fs.exists(fp) then
- fs.makeDir(fp)
- initFilePath(fp)
- end
- local cmd = args[1]
- if cmd == nil then
- help()
- end
- if cmd:lower() == "recache" then
- reloadCache()
- end
- if cmd:lower() == "check" then
- local exists, path = checkPackageExistance(args[2])
- print(exists and "Package " .. args[2] .. " exists in path:\n" .. path or "Package " .. args[2] .. " does not exist.")
- end
- if cmd:lower() == "install" then
- attemptInstall(args[2])
- end
- if cmd:lower() == "addrepo" then
- addRepo(args[2])
- end
- if cmd:lower() == "help" then
- help(args[2])
- end
- end
- onStart({...})
Advertisement
Add Comment
Please, Sign In to add comment