CCCPaste

installer.lua

local owner = "Zonk1987"
local repo = "CC-Tweaked"
local apiUrl = "https://api.github.com/repos/" .. owner .. "/" .. repo .. "/contents/"

local function clear()
    term.clear()
    term.setCursorPos(1, 1)
end

clear()
if term.isColor() then term.setTextColor(colors.cyan) end
print("=======================================")
print("      Zonk's CC-Tweaked Installer      ")
print("=======================================")
if term.isColor() then term.setTextColor(colors.white) end
print("Fetching projects from GitHub...\n")

-- Fetch root contents
local response = http.get(apiUrl)
if not response then
    if term.isColor() then term.setTextColor(colors.red) end
    print("Error: Could not connect to GitHub API.")
    print("Check your internet connection or HTTP API settings in CC.")
    return
end

local responseData = response.readAll()
response.close()

local data = textutils.unserializeJSON(responseData)
if not data then
    if term.isColor() then term.setTextColor(colors.red) end
    print("Error parsing GitHub response.")
    return
end

if data.message and string.find(string.lower(data.message), "api rate limit") then
    if term.isColor() then term.setTextColor(colors.red) end
    print("GitHub API Rate Limit exceeded!")
    print("Please try again later.")
    return
end

local projects = {}
for i, item in ipairs(data) do
    -- Only include directories, ignore hidden folders (.git) and the installer folder itself
    if item.type == "dir" and item.name ~= "installer" and not string.match(item.name, "^%.") then
        table.insert(projects, item)
    end
end

if #projects == 0 then
    print("No projects found in the repository.")
    return
end

while true do
    clear()
    if term.isColor() then term.setTextColor(colors.cyan) end
    print("=======================================")
    print("      Zonk's CC-Tweaked Installer      ")
    print("=======================================")
    if term.isColor() then term.setTextColor(colors.white) end
    print("Select a project to install:\n")

    for i, proj in ipairs(projects) do
        if term.isColor() then term.setTextColor(colors.yellow) end
        term.write("[" .. i .. "] ")
        if term.isColor() then term.setTextColor(colors.white) end
        print(proj.name)
    end
    
    if term.isColor() then term.setTextColor(colors.red) end
    print("\n[0] Exit")
    if term.isColor() then term.setTextColor(colors.white) end
    
    term.write("\nChoice: ")
    local choice = tonumber(read())
    
    if choice == 0 then
        print("Exiting...")
        return
    elseif choice and choice > 0 and choice <= #projects then
        local selected = projects[choice]
        print("\nFetching files for " .. selected.name .. "...")
        
        -- URL encode spaces in the path for the API call just in case
        local safePath = string.gsub(selected.path, " ", "%%20")
        local projRes = http.get(apiUrl .. safePath)
        
        if not projRes then
            if term.isColor() then term.setTextColor(colors.red) end
            print("Failed to get project contents.")
            os.sleep(2)
        else
            local projDataRaw = projRes.readAll()
            projRes.close()
            local projData = textutils.unserializeJSON(projDataRaw)
            
            if projData then
                local filesDownloaded = 0
                for _, fileItem in ipairs(projData) do
                    if fileItem.type == "file" then
                        print("Downloading " .. fileItem.name .. "...")
                        local fileRes = http.get(fileItem.download_url)
                        if fileRes then
                            local fileContent = fileRes.readAll()
                            fileRes.close()
                            
                            local f = fs.open(fileItem.name, "w")
                            f.write(fileContent)
                            f.close()
                            filesDownloaded = filesDownloaded + 1
                        else
                            if term.isColor() then term.setTextColor(colors.red) end
                            print("Failed to download " .. fileItem.name)
                            if term.isColor() then term.setTextColor(colors.white) end
                        end
                    end
                end
                
                if term.isColor() then term.setTextColor(colors.green) end
                print("\nSuccessfully downloaded " .. filesDownloaded .. " files!")
                if term.isColor() then term.setTextColor(colors.yellow) end
                print("Rebooting computer in 3 seconds to apply startup.lua...")
                os.sleep(3)
                os.reboot()
                return
            else
                if term.isColor() then term.setTextColor(colors.red) end
                print("Failed to parse project data.")
                os.sleep(2)
            end
        end
    else
        if term.isColor() then term.setTextColor(colors.red) end
        print("Invalid choice!")
        if term.isColor() then term.setTextColor(colors.white) end
        os.sleep(1)
    end
end