--[[ ------------------------ Originally made by 'Engineer' on the ComputerCraft forums http://www.computercraft.info/forums2/index.php?/topic/11484-/ Edited by DeliciousJaffa to fix bugs, add documentation and add a whole load of new functionality http://twitter.com/DeliciousJaffa ------------------------ Current version: 1.0.1.3 Documentation: 1. Installation Run the following commands: pastebin get JW4uj2Jw startup reboot 2. Usage "startup auto list" - This command will list all auto updating programs "startup auto add " - This command will add a program to check for updates from a pastebin page "startup auto remove " - This will remove a program from the auto update list by it's name (also known as it's command) "startup auto reset" - This will reset the auto update list to the current defaults "startup set " - This command will set a program to be launcher after update checking when the computer restarts (or when the chunk becomes loaded again) "startup unset" - This will wipe the startup so that no program is ran automatically. "startup update" - This command will check if the code at the specified pastebin id is different from the named program, if it is, it'll automatically download the update before anything else 3. Preinstalled programs This auto-updater installs 2 programs by default. - "EpicMine" by Princetommen http://pastebin.com/6qmBBLpz Mines out tunnels or an area - "Attack" by DeliciousJaffa http://pastebin.com/vNNYrHUp Attacks mobs forever and drops items ------------------------ ]] local tArgs = { ... } local autoFile = "/startup-updatelist" local prefFile = "/startup-prefs" local defaultsPbinCode = "B2jBQTU2" function usage() print( "Usage:" ) print( "- startup auto list" ) print( "- startup auto add " ) print( "- startup auto remove " ) print( "- startup auto reset" ) print( "- startup set " ) print( "- startup unset" ) print( "- startup update" ) print( "See the documentation for more information") end function getPastebin( code ) local response = http.get( "http://pastebin.com/raw.php?i=" .. textutils.urlEncode( code ) ) if response then local contents = response.readAll() local file = fs.open( code, "w" ) file.write( contents ) file.close() return true, contents else return false end end function setDefaults() if fs.exists( autoFile ) then fs.delete( autoFile ) end local file = fs.open( autoFile, "w" ) local condition, raw = getPastebin(defaultsPbinCode) fs.delete(defaultsPbinCode) local default = textutils.unserialize(raw) local tPrograms = {} if condition == true then tPrograms = default file.write( textutils.serialize(tPrograms)) file.close() print("Default autoupdate list installed") else print("Failed to obtain the default update list") end end function update() if fs.exists( autoFile ) then local file = fs.open( autoFile, "r" ) local handler = file.readAll() file.close() local tPrograms = textutils.unserialize( handler ) if type( tPrograms ) == "table" then for i = 1, #tPrograms do local condition, content = getPastebin( tPrograms[i][1] ) if condition then local reader = "needsupdateornot" if fs.exists(tPrograms[i][2]) then local installed = fs.open(tPrograms[i][2], "r") reader = installed.readAll() installed.close() end if reader ~= content then fs.delete( tPrograms[i][2] ) fs.move( tPrograms[i][1], tPrograms[i][2] ) print( "Updated " .. tPrograms[i][2] .. " succesfully!" ) else print( "No update needed for: "..tPrograms[i][2] ) fs.delete( tPrograms[i][1] ) end else print( "Update for " .. tPrograms[i][2] .. " unsuccesfull!" ) end end end end print("Update check completed") end if not fs.exists( autoFile ) then setDefaults() end local sCommand = "noCommand" if #tArgs == 0 then update() print("See documentation for commands (pastebin page)") if turtle then if os.getComputerLabel() then print(os.getComputerLabel() .. " reporting for duty with " .. turtle.getFuelLevel() .. " fuel") else print("Reporting for duty with " .. turtle.getFuelLevel() .. " fuel") end else if os.getComputerLabel() then print(os.getComputerLabel() .. " reporting for duty!") end end if fs.exists( prefFile ) then local file = fs.open( prefFile, "r" ) local startupname = file.readAll() if fs.exists(startupname) then print("Launching startup program") shell.run(startupname) else print("Startup program could not be found.") end end elseif #tArgs > 4 then usage() else if string.lower( tArgs[1] ) == "manual" and tArgs[3] then sCommand = "manual" elseif string.lower(tArgs[1]) == "auto" then if string.lower( tArgs[2] ) == "remove" and tArgs[3] then sCommand = "auto_getoff" elseif string.lower( tArgs[2] ) == "add" and tArgs[4] then sCommand = "auto_add" elseif string.lower( tArgs[2] ) == "list" then sCommand = "auto_list" elseif string.lower( tArgs[2] ) == "reset" then setDefaults() else usage() end elseif string.lower(tArgs[1]) == "set" then sCommand = "setstartup" elseif string.lower(tArgs[1]) == "unset" then sCommand = "delstartup" elseif string.lower(tArgs[1]) == "update" then update() else usage() end end if not http then print( "HTTP not enabled" ) print( "Enable this in the config" ) error() end if sCommand == "manual" then local sFile = tArgs[3] local sCode = tArgs[2] if not fs.exists( sFile ) then local rAnswer, download term.clear() term.setCursorPos( 1,1 ) print( "File doesn't exist!\nStill download " .. sCode .. " to "..sFile.." ? (Y/N)" ) while true do local evt, key = os.pullEvent("char") if key == "y" then download = true break elseif key == "n" then download = false break end end if download then if getPastebin( sCode ) then fs.move( sCode, sFile ) print( "Succesfully downloaded " .. sCode .. " as " .. sFile ) elseif not getPastebin( sCode, sFile ) then print( "Could not download " .. sCode .. " from pastebin.com" ) end end else if getPastebin( sCode ) then fs.delete( sFile ) fs.move( sCode, sFile ) print( "Succesfully updated " .. sFile ) elseif not getPastebin( sCode ) then print( "Could not update " .. sFile .. " with " .. sCode ) end end elseif sCommand == "auto_list" then if not fs.exists( autoFile ) then print( "Nothing is added yet" ) else local file = fs.open( autoFile, "r" ) local reader = file.readAll() file.close() local tPrograms = textutils.unserialize( reader ) term.clear() term.setCursorPos( 1,1 ) print( "Pastebin Code: Program Name:" ) if type( tPrograms ) == "table" then for i = 1, #tPrograms do term.setCursorPos( 1, i + 2 ) write( tPrograms[i][1] ) term.setCursorPos( 19, i + 2 ) write( tPrograms[i][2] ) term.setCursorPos( 1, i + 3 ) end else print( "None None") end end elseif sCommand == "auto_add" then local sCode = tArgs[3] local sFile = tArgs[4] if sFile and getPastebin( sCode ) then fs.delete( sCode ) local file = fs.open( autoFile, "r" ) local handler = file.readAll() file.close() local tPrograms = textutils.unserialize( handler ) if type( tPrograms ) ~= "table" then local file = fs.open( autoFile, "w" ) tPrograms = {} file.write( textutils.serialize( tPrograms ) ) file.close() end local codeintable = false for i = 1, #tPrograms do if tPrograms[i][2] == sFile then codeintable = true end end if codeintable == false then table.insert( tPrograms, { sCode, sFile } ) end fs.delete( autoFile ) file = fs.open( autoFile, "w" ) file.write( textutils.serialize( tPrograms )) file.close() if codeintable == false then print("Succesfully added " .. sFile .. " to the update list." ) else print("Please remove " .. sFile .. " from the list before adding a new code for it") end update() elseif not getPastebin( sCode ) then print( "Your pastebin code is invalid" ) end elseif sCommand == "auto_getoff" then local sFile = tArgs[3] local file = fs.open( autoFile, "r" ) local handler = file.readAll() file.close() local tPrograms = textutils.unserialize( handler ) for i = 1, #tPrograms do if tPrograms[i][2] == sFile then table.remove( tPrograms, i) table.remove( tPrograms, i) break end end fs.delete( autoFile ) file = fs.open( autoFile, "w" ) file.write( textutils.serialize( tPrograms ) ) file.close() print( "Succesfully removed " .. sFile .. " from the auto update list." ) elseif sCommand == "setstartup" then local file = fs.open( prefFile, "w" ) file.write( tArgs[2] ) file.close() print("" .. tArgs[2] .. " will now open on startup") elseif sCommand == "delstartup" then if fs.exists(prefFile) then fs.delete(prefFile) print("This computer will no longer auto launch a program on startup") end print("Error: No launch program set") end