local X=require("\064\108\117\110\101\047\102\115")local n=(require("\046\047\108\105\098\047\108\111\103\103\101\114")).new(4)local l=require("\046\047\108\105\098\047\101\120\101\099\117\116\101\067\111\109\109\097\110\100")if X.isDir("\112\097\099\107\097\103\101\115")then X.removeDir("\112\097\099\107\097\103\101\115")end local U=l("\119\097\108\108\121",{"\105\110\115\116\097\108\108"},{stdio="\105\110\104\101\114\105\116"},true)if not U.ok then n:error("\070\097\105\108\101\100 \116\111 \105\110\115\116\097\108\108 \112\097\099\107\097\103\101\115\033")return end if not X.isDir("\112\097\099\107\097\103\101\115")then X.writeDir("\112\097\099\107\097\103\101\115")end if X.isFile("\115\114\099\047\115\101\114\118\101\114\047\110\101\116\119\111\114\107\046\122\097\112")then l("\122\097\112",{"\115\114\099\047\115\101\114\118\101\114\047\110\101\116\119\111\114\107\046\122\097\112"})end l("\114\111\106\111",{"\115\111\117\114\099\101\109\097\112";X.isFile("\115\111\117\114\099\101\109\097\112\046\112\114\111\106\101\099\116\046\106\115\111\110")and"\115\111\117\114\099\101\109\097\112\046\112\114\111\106\101\099\116\046\106\115\111\110"or"\100\101\102\097\117\108\116\046\112\114\111\106\101\099\116\046\106\115\111\110","-\111","\115\111\117\114\099\101\109\097\112\046\106\115\111\110"})if X.isDir("\080\097\099\107\097\103\101\115")then local U=l("\119\097\108\108\121-\112\097\099\107\097\103\101-\116\121\112\101\115",{"--\115\111\117\114\099\101\109\097\112","\115\111\117\114\099\101\109\097\112\046\106\115\111\110","\046\047\080\097\099\107\097\103\101\115"},nil,true)if U.ok then n:info("\083\117\099\099\101\115\115\102\117\108\108\121 \103\101\110\101\114\097\116\101\100 \112\097\099\107\097\103\101 \116\121\112\101\115\033")else n:error("\070\097\105\108\101\100 \116\111 \103\101\110\101\114\097\116\101 \112\097\099\107\097\103\101 \116\121\112\101\115\033")end X.move("\046\047\080\097\099\107\097\103\101\115","\046\047\112\097\099\107\097\103\101\115",true)end n:info("\073\110\115\116\097\108\108\101\100 \112\097\099\107\097\103\101\115\033")
local fs = require("@lune/fs")

local logger = require("./lib/logger").new(4)
local executeCommand = require("./lib/executeCommand")

if fs.isDir("packages") then
	fs.removeDir("packages")
end

local status = executeCommand("wally", {
	"install",
}, {
    stdio = "inherit"   
}, true)

if not status.ok then 
    logger:error("Failed to install packages!")
    return
end

if not fs.isDir("packages") then 
    fs.writeDir("packages")
end

-- do this so the project file doesn't refer to files that don't exist and cause rojo to fail
if fs.isFile("src/server/network.zap") then
	executeCommand("zap", { "src/server/network.zap" })
end

executeCommand("rojo", {
	"sourcemap",
	fs.isFile("sourcemap.project.json") and "sourcemap.project.json" or "default.project.json",
	"-o",
	"sourcemap.json",
})

if fs.isDir("Packages") then
	local packageTypeStatus = executeCommand("wally-package-types", {
		"--sourcemap",
		"sourcemap.json",
		"./Packages",
	}, nil, true)

	if packageTypeStatus.ok then
		logger:info("Successfully generated package types!")
	else
		logger:error("Failed to generate package types!")
	end

    fs.move("./Packages", "./packages", true)
end

logger:info("Installed packages!")