function get_key_response ()
	local event = nil

	repeat
		event = { os.peek_event ("key") }
		os.sleep (0.3)
	until #event > 0

	os.get_event ("key")

	if not event[3] and not event[4] then
		if os.peek_event ("char") then
			os.get_event ("char")
		end

		return event[2] == keys.KEY_Y
	end

	return false
end

local function load_file (src)
	local file = io.open (src, "rb")

	if file then
		local str = file:read ("*a")
		file:close ()

		return str
	end

	return nil
end

local function store_file (dest, str)
	fs.mkdir (fs.path_folder (dest))
	local file = io.open (dest, "wb")

	if file then
		local str = file:write (str)
		file:close ()

		return true
	end

	return false
end

local function unpack_file (src, dest)
	print ("unpack %s\n", dest)

	local str = load_file (src)

	if str then
		str = utils.decompress (str, "deflate")

		if str then
			if store_file (dest, str) then
				os.sleep (0.1)

				return true
			end
		end
	end

	term.set_colors (term.colors.red, term.colors.black)
	print ("*** failed %s\n", dest)
	term.set_colors (term.colors.silver, term.colors.black)
	os.sleep (0.1)

	return false
end

term.set_colors (term.colors.white, term.colors.black)
print ("Installing WIN\n\n")
term.set_colors (term.colors.orange, term.colors.black)
print ("Existing files will be overwritten\n")
term.set_colors (term.colors.silver, term.colors.black)
print ("Continue? (y to continue)")

if not get_key_response () then
	print ("\r                          \rcancelled\n")

	os.sleep (0.5)

	os.shutdown ()
end
print ("\r                          \r")

unpack_file ("/WIN/files/boot", "/boot")
unpack_file ("/WIN/files/startup.cmd", "/startup.cmd")
unpack_file ("/WIN/files/win.ini", "/win.ini")
unpack_file ("/WIN/files/license.txt", "/license.txt")
unpack_file ("/WIN/files/win/applicationFrame.lua", "/win/applicationFrame.lua")
unpack_file ("/WIN/files/win/appListFrame.lua", "/win/appListFrame.lua")
unpack_file ("/WIN/files/win/buttonWindow.lua", "/win/buttonWindow.lua")
unpack_file ("/WIN/files/win/checkWindow.lua", "/win/checkWindow.lua")
unpack_file ("/WIN/files/win/__classBase.lua", "/win/__classBase.lua")
unpack_file ("/WIN/files/win/closeButtonWindow.lua", "/win/closeButtonWindow.lua")
unpack_file ("/WIN/files/win/cmndlg.lua", "/win/cmndlg.lua")
unpack_file ("/WIN/files/win/comm.lua", "/win/comm.lua")
unpack_file ("/WIN/files/win/defs.lua", "/win/defs.lua")
unpack_file ("/WIN/files/win/desktopTheme.lua", "/win/desktopTheme.lua")
unpack_file ("/WIN/files/win/desktopWindow.lua", "/win/desktopWindow.lua")
unpack_file ("/WIN/files/win/editWindow.lua", "/win/editWindow.lua")
unpack_file ("/WIN/files/win/GDI.lua", "/win/GDI.lua")
unpack_file ("/WIN/files/win/globals.lua", "/win/globals.lua")
unpack_file ("/WIN/files/win/homePageFrame.lua", "/win/homePageFrame.lua")
unpack_file ("/WIN/files/win/inputWindow.lua", "/win/inputWindow.lua")
unpack_file ("/WIN/files/win/keyboardFrame.lua", "/win/keyboardFrame.lua")
unpack_file ("/WIN/files/win/labelWindow.lua", "/win/labelWindow.lua")
unpack_file ("/WIN/files/win/listWindow.lua", "/win/listWindow.lua")
unpack_file ("/WIN/files/win/lockScrnFrame.lua", "/win/lockScrnFrame.lua")
unpack_file ("/WIN/files/win/menuWindow.lua", "/win/menuWindow.lua")
unpack_file ("/WIN/files/win/msgBoxFrame.lua", "/win/msgBoxFrame.lua")
unpack_file ("/WIN/files/win/parentFrame.lua", "/win/parentFrame.lua")
unpack_file ("/WIN/files/win/popupFrame.lua", "/win/popupFrame.lua")
unpack_file ("/WIN/files/win/private.lua", "/win/private.lua")
unpack_file ("/WIN/files/win/rect.lua", "/win/rect.lua")
unpack_file ("/WIN/files/win/sysMsgBoxFrame.lua", "/win/sysMsgBoxFrame.lua")
unpack_file ("/WIN/files/win/taskBarFrame.lua", "/win/taskBarFrame.lua")
unpack_file ("/WIN/files/win/textWindow.lua", "/win/textWindow.lua")
unpack_file ("/WIN/files/win/win", "/win/win")
unpack_file ("/WIN/files/win/window.lua", "/win/window.lua")
unpack_file ("/WIN/files/win/workSpace.lua", "/win/workSpace.lua")
unpack_file ("/WIN/files/win/devices/desktops.lua", "/win/devices/desktops.lua")
unpack_file ("/WIN/files/win/devices/printers", "/win/devices/printers")
unpack_file ("/WIN/files/win/devices/term/desktop.ini", "/win/devices/term/desktop.ini")
unpack_file ("/WIN/files/win/devices/term/startup.ini", "/win/devices/term/startup.ini")
unpack_file ("/WIN/files/apis/html", "/apis/html")
unpack_file ("/WIN/files/apps/browse", "/apps/browse")
unpack_file ("/WIN/files/apps/browse.ini", "/apps/browse.ini")
unpack_file ("/WIN/files/apps/chat", "/apps/chat")
unpack_file ("/WIN/files/apps/cmd", "/apps/cmd")
unpack_file ("/WIN/files/apps/cmd_los", "/apps/cmd_los")
unpack_file ("/WIN/files/apps/email", "/apps/email")
unpack_file ("/WIN/files/apps/email.ini", "/apps/email.ini")
unpack_file ("/WIN/files/apps/emread", "/apps/emread")
unpack_file ("/WIN/files/apps/emwrite", "/apps/emwrite")
unpack_file ("/WIN/files/apps/fexplore", "/apps/fexplore")
unpack_file ("/WIN/files/apps/fexplore.asc", "/apps/fexplore.asc")
unpack_file ("/WIN/files/apps/manager", "/apps/manager")
unpack_file ("/WIN/files/apps/notepad", "/apps/notepad")
unpack_file ("/WIN/files/apps/notepad.ini", "/apps/notepad.ini")
unpack_file ("/WIN/files/apps/sadmin", "/apps/sadmin")
unpack_file ("/WIN/files/apps/sadmin.ini", "/apps/sadmin.ini")
unpack_file ("/WIN/files/apps/shutdown", "/apps/shutdown")
unpack_file ("/WIN/files/progs/lua", "/progs/lua")

term.set_colors (term.colors.silver, term.colors.black)
print ("WIN installed\n")

return 0
