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

-- ñ봰ڴС
os.execute("title WIFI-MTDȡ")
os.execute("mode con: cols=66 lines=35")

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

-- ȡűĿ¼ľ·
local function get_script_dir()
    local path = io.popen("cd"):read("*l")
    return path .. "\\"
end
local script_dir = get_script_dir()

-- Ŀļ·
local qt_folder = script_dir .. "TQ\\"

-- ȡ
print(colors.blue .. colors.bright .. "Ѱ豸..." .. colors.reset)
delay.sleep (4) -- ȴ豸
os.execute("bin\\adb pull /dev/mtd4 draw.tmp")   -- ̼MTDȡ
print()
print()
print(colors.green .. "ѳȡ" .. colors.reset)

-- ȡûļ
print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)
io.write("µļչ")               --ִͣ,ûı
local new_file_name = io.read()                            --ûļ趨Ϊ"new_file_name"
new_file_name = new_file_name:gsub('^%"', ''):gsub('%"$', '')      --ȥļ·˫ţУ
new_file_name = new_file_name .. ".bin" -- ļ .bin ׺

-- ·
local old_file_path = script_dir .. "draw.tmp"
local new_file_path = script_dir .. new_file_name

-- ļ
local success, err = os.rename(old_file_path, new_file_path)
if success then
    print(colors.green .."ļѳɹΪ: " .. new_file_name .. colors.reset)
else
    print(colors.red .."ʧ: " .. err .. colors.reset)
end

-- ĿļǷڣ򴴽
local function ensure_directory_exists(path)
    local ok, err, code = os.rename(path, path)
    if not ok then
        if code == 2 then
            os.execute('mkdir "' .. path .. '"')
        else
            print(colors.red .."޷򴴽Ŀ¼: " .. err .. colors.reset)
        end
    end
end

ensure_directory_exists(qt_folder)

-- µĿ·
local target_file_path = qt_folder .. new_file_name

-- ƶļ
local success, err = os.rename(new_file_path, target_file_path)
if success then
    print(colors.green .."ļѳɹƶQTļ".. colors.reset)
	os.execute("explorer TQ")
	print(colors.green .. colors.bright .."űȷִ,رմ".. colors.reset)
	delay.sleep (5)
else
    print(colors.red .."ƶļʧ: " .. err .. colors.reset)
	os.execute("pause")
end