-- web.lua
-- 򵥵jffs2ɶд豸¼web̨
-- 
-- Ȩ (C) 2025-2026 Punguin
--
-- ԸᷢGNU Afferoͨù֤֤ĵ3ѡģκκİ汾·/޸
-- ķϣáûκα֤ûı֤ķַϣõģûκα֤û·ʺĳһضĿĵı֤ μ GNU Afferoͨù֤˽ϸڡ
-- ӦѾյһGNU Afferoͨù֤ĸ ûУμ<https://www.gnu.org/licenses/>
--
-- ϵǣ3618679658@qq.com
-- ChatGPTЭд

local colors = require("lua\\colors") -- ANSIɫ
local delay = require("lua\\sleep") -- ʱ

-- ñն붨崰ڴС
os.execute("title WIFI_WEBд")
os.execute("mode con: cols=66 lines=35")
print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)

--  executeADBCommand ,ͨos.executeadb,жϻǷΪɶд
local function executeADBCommand(command)
    local handle = io.popen(command)
    local result = handle:read("*a")
    handle:close()
    return result
end

--  check_file 
local function check_file()
    print(colors.blue .. colors.bright .. "Ѱ豸..." .. colors.reset)
    delay.sleep(4)
    os.execute("cls")
    --  adb devices 񷵻ؽ
    local adbDevicesCommand = "bin\\adb devices"
    local devicesOutput = executeADBCommand(adbDevicesCommand)

    -- Ƿ豸
    if not string.find(devicesOutput, "\tdevice") then
        print(colors.red .. "ǰ豸" .. colors.reset)
		os.execute("pause")
        os.exit(1)
    end

    --  adb touch 񷵻ؽ
    local adbCommand = "bin\\adb shell touch /etc_ro/web/test_file"
    local output = executeADBCommand(adbCommand)

    -- 鷵ؽǷ "Read-only file system"
    if string.find(output, "Read") then
        print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)
        print(colors.red .. "ûΪֻļϵͳ (squashfs)" .. colors.reset)
        print(colors.blue .. colors.bright .. "ǿˢᵼ豸ʧ̨" .. colors.reset)
        os.execute("pause")
        os.exit(1)
    else
        print(colors.green .. colors.bright .."ûΪдļϵͳ,֧ļϴ(jffs2)".. colors.reset)
        print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)
    end
end

-- ޸ 5.1-Build-250816
folderPath = nil -- ȫnil
-- End

local function file()
-- ȡûļ·
print(colors.yellow .."뽫ļ˴ڣȻ󰴻س:" ..colors.red)
  -- ޸ 5.1-Build-250816
  -- ȥlocal,Ϊȫֱ
  folderPath = io.read("*l") -- ȡûļ·
  -- End

-- ȥܴڵ
folderPath = folderPath:gsub("\"", "")

-- ļ·ǷЧ
local checkCommand = string.format("if exist \"%s\" (echo ok) else (echo not found)", folderPath)
local handle = io.popen(checkCommand)
local result = handle:read("*a")
handle:close()

if not result:find("ok") then
    print(colors.red .. "Чļ·")
	os.execute("pause")
    os.exit(1)
end
end

-- ȡǰ Lua űĿ¼
local function getScriptDirectory()
    local str = arg[0]
    return str:match("(.*/)") or str:match("(.*\\??") or ".\\"
end

-- Ŀ¼
local function createDirectory(path)
    os.execute("mkdir \"" .. path .. "\"")
end

-- ִADBؽ
local function executeADBCommand(command)
    local handle = io.popen(command)
    local result = handle:read("*a")
    handle:close()
    return result
end

--  /etc_ro/web ļеָĿ¼
local function backupWebFolder(tempBackupPath)
    print()
    print(colors.green .. "ڱ豸̨WEBļ..." .. colors.blue)
    local backupCommand = "bin\\adb pull /etc_ro/web \"" .. tempBackupPath .. "\""
    return executeADBCommand(backupCommand)
end

-- ƶļ
local function moveBackupFolder(tempBackupPath, backupDir)
    -- ѯûļ
	print()
    print(colors.yellow .. "뱸ļе:" .. colors.red)
    local folderName = io.read()
	print(colors.blue)

    -- ƶļ
    local moveCommand = "move /Y \"" .. tempBackupPath .. "\" \"" .. backupDir .. folderName .. "\""
    local moveResult = os.execute(moveCommand)
    print(colors.reset)
	
    -- ƶ
    if moveResult then
	    print()
        print(colors.green .. "ɣļѱ浽: " .. backupDir .. folderName .. colors.reset)
		os.execute("explorer TQ")
    else
	    print()
        print(colors.red .. "ƶļʧܣȨޡ" .. colors.reset)
    end
end

-- װ̵ĺ
local function Backup_web()
    local scriptDir = getScriptDirectory()

    -- ñݱ·
    local tempBackupPath = scriptDir .. "web_backup" -- ֱӱݵűĿ¼
    local backupDir = scriptDir .. "TQ\\"

    -- ѯûǷ񱸷 /etc_ro/web ļ
	print()
    print(colors.yellow .. colors.bright .. "Ƿ񱸷豸ԭ̨? (y/n)" .. colors.reset)
    local userInput = io.read()

    if userInput == "y" or userInput == "Y" then
        -- ûѡ񱸷ݣִбݲ
        local backupResult = backupWebFolder(tempBackupPath)
        
        -- 鱸ݽ
        if backupResult and backupResult:find("error") then
		    print()
            print(colors.red .. "ʧܣ豸ӼȨޡ" .. colors.reset)
        else
            moveBackupFolder(tempBackupPath, backupDir)
        end
    else
        -- ûѡ񲻱
		print()
        print(colors.blue .. "ݲ" .. colors.reset)
    end

    print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)
end

local function up_web()
-- ɾ豸ԭе /etc_ro/web ļ
local deleteCommand = "bin\\adb shell rm -rf /etc_ro/web"
os.execute(deleteCommand)

-- ϴļе豸
local uploadCommand = string.format("bin\\adb push \"%s\" /etc_ro/web", folderPath)
os.execute(uploadCommand)
print()
print(colors.green .. colors.bright .."ϴ,豸web滻!"..colors.reset)
end

os.execute("bin\\adb shell mount -o remount,rw /")
check_file() -- 豸Ƿд
file() -- ûļ
Backup_web() -- к̨ı
up_web() -- ɾ豸ԭеWEBϴµļ
os.execute("pause")