Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local databaseURL = "http://pastebin.com/raw.php?i=eV8GcT3Q"
- local databaseSoruce = {}
- local appDatabase = {}
- --Functions----------
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- local function getHttp(url, save)
- local request = http.get(url)
- if type(request) == "table" then
- f = fs.open(save, "w")
- f.write(request.readAll())
- f.close()
- return true
- else
- f = fs.open(}"programs/"..save, "w")
- f.write(request.readAll())
- f.close()
- return true
- else
- return false
- end
- end
- local function printError(text)
- if term.isColor() then
- term.setTextColor(colors.red)
- print(text)
- term.setTextColor(colors.white)
- end
- end
- local function printSucsess(text)
- if term.isColor() then
- term.setTextColor(colors.lime)
- print(text)
- term.setTextColor(colors.white)
- end
- end
- local function databaseUpdate(silentMode)
- f = fs.open("/.uosatp/source", "r")
- databaseSoruce = textutils.unserialize(f.readAll())
- f.close()
- local sucsessfulRequests = 0
- local failedRequests = 0
- for i, v in ipairs(databaseSoruce) do
- if not silentMode then
- print("Updating " ..v)
- end
- local request = http.get(v)
- local tempSource = nil
- if type(request) == "table" then
- local tempSource = textutils.unserialize(request.readAll())
- for k,v in pairs(tempSource) do
- appDatabase[k] = v
- end
- sucsessfulRequests = sucsessfulRequests + 1
- else
- failedRequests = failedRequests + 1
- end
- end
- if not silentMode then
- print(sucsessfulRequests .." sources updated, ".. failedRequests .." sources failed")
- end
- f = fs.open("/.uosatp/database", "w")
- f.write(textutils.serialize(appDatabase))
- f.close()
- return true
- end
- local function install(appInstall)
- isLocated = false
- for k,v in pairs(appDatabase) do
- if string.lower(k) == string.lower(appInstall) then
- isLocated = true
- write("Do you want to continue [Y/n]? ")
- local downloadChoice = read()
- if downloadChoice == "" or string.lower(downloadChoice) == "y" then
- print("Downloading . . .")
- if not getHttp(v, "/.uosatp/cache/" ..k) then
- printError("Download failed!")
- break
- else
- f = fs.open("/.uosatp/cache/" ..k, "r")
- packageData = f.readAll()
- f.close()
- local state, error = pcall(loadfile("/.uosatp/cache/" ..k))
- if error ~= "" then
- error(error)
- end
- saveLocation = appInstall
- print("Initializing installer")
- print("Reading file")
- print("Inserting passwords . . .")
- print("Reading dependencies")
- print("Installing "..saveLocation.." . . .")
- fs.move("/.uosatp/cache/" ..k, saveLocation)
- file = fs.open(saveLocation, "r")
- depend = file.readLine()
- depend = textutils.unserialize(depend)
- names = file.readLine()
- names = textutils.unserialize(names)
- if type(depend) == "table" then
- if type(names) == "table" then
- for i=1,#depend do
- print(" Downloading "..names[i])
- getHttp(names[i], depend[i])
- print(" Installing")
- print(" End")
- end
- end
- end
- printSucsess("Installed!")
- end
- end
- else
- printError("Abort.")
- end
- end
- if not isLocated then
- printError("Unable to locate package")
- end
- end
- local function appSearch(sApp)
- local found = false
- for k,v in pairs(appDatabase) do
- if string.lower(sApp) == string.lower(k) then
- found = true
- end
- end
- return found
- end
- local function fileSystemSetup()
- if not fs.exists("/.uosatp/") then
- fs.makeDir("/.uosatp/cache")
- fWrite = {databaseURL}
- f = fs.open("/.uosatp/source", "w")
- f.write(textutils.serialize(fWrite))
- f.close()
- databaseUpdate(true)
- end
- end
- --Main----------
- fileSystemSetup()
- f = fs.open("/.uosatp/database", "r")
- appDatabase = textutils.unserialize(f.readAll())
- f.close()
- if tArgs[1] == "update" then
- databaseUpdate(false)
- elseif tArgs[1] == "install" and tArgs[2] then
- if string.lower(tArgs[2]) == "apt-get" then
- printSucsess("uosatpCeption!")
- else
- install(tArgs[2])
- end
- elseif tArgs[1] == "add" and tArgs[2] then
- f = fs.open("/.uosatp/source", "r")
- databaseSource = textutils.unserialize(f.readAll())
- f.close()
- table.insert(databaseSource, tArgs[2])
- f = fs.open("/.uosatp/source", "w")
- f.write(textutils.serialize(databaseSource))
- f.close()
- printSucsess("Source added")
- elseif tArgs[1] == "search" and tArgs[2] then
- if appSearch(tArgs[2]) then
- printSucsess("Package found")
- else
- printError("Unable to locate package")
- end
- elseif tArgs[1] == "purge" then
- print("Do you want to continue [Y/n]? ")
- local purgeChoice = read()
- if string.lower(purgeChoice) == "y" or purgeChoice == "" then
- fs.delete("/.uosatp")
- fileSystemSetup()
- printSucsess("Purge complete")
- else
- printError("Abort")
- end
- elseif tArgs[1] == "help" or tArgs[1] == "--help" or tArgs[1] == nil then
- print([[
- Commands:
- Update - Retrieve new database list
- Install - Install new packages
- Search - Check if package exists
- Add - Add a software source
- Purge - Purge all user sources
- uosatp has super cow powers
- API functions
- Start - Iniialize UOSATP so the other functions work
- apiInstall - Install a package
- apiAdd - Add a software Source
- apiUpdate - Retrieve new database list
- apiSearch - Check if package exist
- Returns true or false]])
- elseif tArgs[1] == "moo" then
- print([[
- (__)
- (oo)
- /------\/
- / | ||
- * /\---/\
- ~~ ~~
- ...."Have you mooed today?"...]])
- else
- printError("Invalid operation")
- printSucsess("Usage: uosatp help")
- end
- --APIFUNCTIONS
- function Start()
- databaseURL = "http://pastebin.com/raw.php?i=eV8GcT3Q"
- databaseSoruce = {}
- appDatabase = {}
- fileSystemSetup()
- f = fs.open("/.uosatp/database", "r")
- appDatabase = textutils.unserialize(f.readAll())
- f.close()
- end
- function apiInstall(appInstall)
- isLocated = false
- for k,v in pairs(appDatabase) do
- if string.lower(k) == string.lower(appInstall) then
- isLocated = true
- local downloadChoice = "y"
- if downloadChoice == "" or string.lower(downloadChoice) == "y" then
- if not getHttp(v, "/.uosatp/cache/" ..k) then
- return false
- break
- else
- f = fs.open("/.uosatp/cache/" ..k, "r")
- packageData = f.readAll()
- f.close()
- local state, error = pcall(loadfile("/.uosatp/cache/" ..k))
- if error ~= "" then
- return false
- end
- saveLocation = appInstall
- fs.move("/.uosatp/cache/" ..k, saveLocation)
- file = fs.open(saveLocation, "r")
- depend = file.readLine()
- depend = textutils.unserialize(depend)
- names = file.readLine()
- names = textutils.unserialize(names)
- if type(depend) == "table" then
- if type(names) == "table" then
- for i=1,#depend do
- getHttp(names[i], depend[i])
- end
- end
- end
- return true
- end
- end
- else
- return false
- end
- end
- if not isLocated then
- return false
- end
- end
- function apiAdd()
- f = fs.open("/.uosatp/source", "r")
- databaseSource = textutils.unserialize(f.readAll())
- f.close()
- table.insert(databaseSource, tArgs[2])
- f = fs.open("/.uosatp/source", "w")
- f.write(textutils.serialize(databaseSource))
- f.close()
- return true
- end
- function apiUpdate()
- databaseUpdate(true)
- end
- function apiSearch()
- if appSearch(tArgs[2]) then
- return true
- else
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment