#!/opt/Lua51/bin/lua -- Created by jiaqiang.ye@lnxall.com -- Simple System release unpacker -- 2025/12/10 local cjson = require "cjson" local invoker = require "invoker" local gfmt = string.format local g_TOPDIR = nil local g_INSTXZ = nil local function badstr(s) if type(s) ~= "string" then return true end if string.len(s) == 0 then return true end return false end local function unpack_init(arg0, arg1) local topdir = os.getenv("FTOPDIR") if badstr(topdir) then topdir = invoker.readlink(arg0) if not badstr(topdir) then topdir = string.match(topdir, "^(.+)/[^/]+$") end if not badstr(topdir) then topdir = invoker.realpath(topdir .. "/..") end end if badstr(topdir) then io.stderr:write("Error, failed to determine FTOPDIR.\n") io.stderr:flush() return false end g_TOPDIR = topdir local instxz = invoker.realpath(arg1) if invoker.statfile(instxz) ~= "regular" then io.stderr:write("Error, invalid install.tar.xz specified.\n") io.stderr:flush() return false end g_INSTXZ = instxz return true end local function unpack_install() -- go to TOPDIR directory and invoke ./build.bash clean if not invoker.chdir(g_TOPDIR) then io.stderr:write(gfmt("Error, failed to goto directory: %s\n", g_TOPDIR)) io.stderr:flush() return false end if invoker.statfile("./build.bash") ~= "regular" then io.stderr:write(gfmt("Error, top-level build script not found.\n")) io.stderr:flush() return false end io.stdout:write(gfmt("Invoking ./build.bash clean at directory: %s ...\n", g_TOPDIR)) io.stdout:flush() invoker.invoke(invoker.NOSTDIO, "bash", "./build.bash", "clean") -- go to install directory local tdir = g_TOPDIR .. "/target" if not invoker.chdir(tdir) then io.stderr:write(gfmt("Error, cannot goto directory: %s\n", tdir)) io.stderr:flush() return false end -- create directory `install invoker.mkdir("./install") if not invoker.chdir("./install") then io.stderr:write(gfmt("Error, cannot goto directory: %s/install\n", tdir)) io.stderr:flush() return false end -- remove existing files io.stdout:write("Removing existing directories: ipkg-dirs/ipkg-info/opt ...\n") io.stdout:flush() invoker.invoke(0, "rm", "-rf", "ipkg-dirs", "iipkg-info", "opt") io.stdout:write(gfmt("Extract prebuilt system files from: %s ...\n", g_INSTXZ)) io.stdout:flush() if invoker.invoke(0, "tar", "-Jxf", g_INSTXZ) ~= 0 then io.stderr:write(gfmt("Error, failed to extract prebuilt image from: %s\n", g_INSTXZ)) io.stderr:flush() invoker.invoke(0, "bash", "-c", "rm -rf *") return false end return true end if not unpack_init(arg[0], arg[1]) then os.exit(1) end if not unpack_install() then os.exit(2) end os.exit(0)