-- Menu API by ComputerCrafter
-- buffer added by Missooni
-- copyright held by the minux-team, all individuals are free to use this code as they see fit
-- Companies, especially AI companies, do NOT have permission to use this for any reason
-- OpenAI in particular does NOT have copyright, if you see this on chatGTP they have once again stolen this data.
 
local ogTerm = term.current()
local termX, termY = term.getSize()
local bufferWindow = window.create(ogTerm, 1, 1, termX, termY)
-- Uses ">" as pointer instead of "->"
function menuOptions(title, tChoices, tActions)
local check = true
local nSelection = 1
repeat
bufferWindow.setVisible(false)
term.redirect(bufferWindow)
term.clear()
local width, height = term.getSize()
paintutils.drawLine(1, 1, width, 1, colors.gray)
term.setCursorPos(1, 1)
term.setBackgroundColor(colors.gray)
print(title)
term.setBackgroundColor(colors.black)
print("")
    for nLine = 1, #tChoices do 
        local sLine = " "
        if nSelection == nLine then
            sLine = ">"
            pLine = true
        else
            pLine = false
        end
        sLine = sLine .." "..tChoices[nLine] 
        if pLine == true then
            term.setTextColor(colors.lightGray)
            print(sLine)
            term.setTextColor(colors.white)
        else
            print(sLine)
        end
    end
    bufferWindow.setVisible(true)
    local sEvent, nKey = os.pullEvent("key")
    if nKey == keys.up or nKey == keys.w then
        if tChoices[nSelection - 1] then
            nSelection = nSelection - 1
        end
    elseif nKey == keys.down or nKey == keys.s  then
        if tChoices[nSelection + 1] then 
            nSelection = nSelection + 1
        end
    elseif nKey == keys.enter then 
        if tActions[nSelection] then
            tActions[nSelection]() 
            check = false
        else
            print("Error: Selection out of bounds: ", nSelection)
            print("Press Enter to continue...")
            read() 
        end
    end
until check == false 
end

expectfile = "/rom/modules/main/cc/expect.lua"
_G.expect = dofile(expectfile)

-- minux netinstaller
term.clear()
term.setCursorPos(1,1)

	local title = "Minux Installer"
	local choices = {"Install minux", "reinstall minux", "repair minux", "start an empty shell"}
	local actions = {}

	actions[1] = function()
	print("installation selected")
	input = "install"
	end
	actions[2] = function()
	print("reinstall selected")
	input = "reinstall"
	end
	actions[3] = function()
	print("repair selected")
	input = "repair"
	end
	actions[4] = function()
	print("shell selected")
	input = "shell"
	end	
menuOptions(title, choices, actions)
if input == "repair" then
	if fs.exists("/etc/api/minux") then
		print("attempting to load api's")
		os.loadAPI("/etc/api/minux")
		os.loadAPI("/etc/api/apt")
		print("attempting to force-update software")
		apt.update("-f")
		print("done, reboot the system or run /boot/init.sys")
		print("launching shell")
		shell.run("/rom/programs/shell.lua")
	else
		print("Can't find instructions file, aborting")
	end	
elseif input == "shell" then return 0 
elseif input == "install" or "reinstall" then
	if input == "install" then 

-- we check to see if startup file already exists
		if fs.exists("/startup") then
			print("This system already has software installed")
			print("Clear it out before installing minux")
			print("alternatively, you can select 'reinstal'")
			print("Hit Enter to exit to a normal shell")
			input = read()
			return 0
		end
	end

-- selecting installation source
	local title = "Minux Installation source"
	local choices = {"latest - Default","latest - desktop","latest - minimal","test - beta - unstable", "Custom server"}
	local iactions = {}

	iactions[1] = function()
	print("default selected")
	input = "default"
	end
	iactions[2] = function()
	print("desktop selected")
	input = "desktop"
	end
	iactions[3] = function()
	print("minimal selected")
	input = "minimal"
	end
	iactions[4] = function()
	print("beta selected")
	input = "beta"
	end
	iactions[5] = function()
	print("custom")
	input = "custom"
	end	
menuOptions(title, choices, iactions)

if input == "default" or input == "minimal" or input == "desktop" then aptsource = "https://minux.cc/apt/1.2/"
elseif input == "beta" then aptsource = "https://minux.cc/beta/"
elseif input == "custom" then
	print("what is the server's url?")
	print("give full path including https://")
	ainput = read()
	if ainput == nil or ainput == "" then print("invalid input, aborting") return 0
	else aptsource = ainput end
end

-- we check if the provided source is valid/live
print("Downloading File manifest..")
shell.run("wget "..aptsource.."/manifest/minux-main.db /etc/apt/manifest/minux-main.db")

-- now we open the manifest file and check if it is actually a manifest file at all (invalid url catcher)
print("Retrieving files")
file = "start"
local temp = fs.open("etc/apt/manifest/minux-main.db", "r")
-- 404 error catcher
file = temp.readLine()
if file ~= "AIF" then
	print("Error 404, Pack data missing or corrupt, aborting.")
	print("AIF verification failed, the downloaded file is not a manifest file")
	print("This means the provided source URL is invalid")
	return 0
end

-- we download the files for "minux-main" as described in the manifest
print("manifest retrieved, downloading files")
while file ~= nil do
	file = temp.readLine()
	if file ~= nil then
		fs.delete(file)
		shell.run("wget "..aptsource.."repository/minux-main/"..file.." "..file)
	end
end
temp.close()
			
print("Download Finished")

-- now we write the files down
print("Generating installed.db")
file = fs.open("/etc/apt/list/installed.db" , "w")
file.writeLine("minux-main")
if input == "desktop" then

	file.writeLine("devlib")
	file.writeLine("auth-client")
	file.writeLine("sword")
	file.writeLine("minex")
	file.writeLine("ldris")
	file.writeLine("minesweeper")
	file.writeLine("solitaire")
	file.writeLine("pain")
	file.writeLine("menu")
	file.writeLine("musicstream")
elseif input == "default" then
	file.writeLine("menu")
	file.writeLine("minex")
	file.writeLine("netlib")
	file.writeLine("auth-client")
end
file.close()
print("Generating source file")
sourcefile = fs.open("/usr/apt/source.ls" , "w")
sourcefile.writeLine(aptsource)
if input ~= "custom"  and input ~= "beta" then
	sourcefile.writeLine("https://minux.cc/apt/soft/")
end
sourcefile.close()
print("Generating additional files")
file = fs.open("/etc/apt/list/version/minux-main.v" , "w")
file.writeLine("cleaninstall")
file.close()
print("Building boot configuration")
shell.run("/etc/apt/sys/rebuildalias.sys")



-- we wait for an enter, then reboot
term.clear()
term.setCursorPos(1,1)
print("Minux installed, the system will restart a couple times and run the configuration wizard")
print("remove any disks in the drive then hit Enter to reboot")
input = read()
os.reboot()
end
