-- so.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 PINGUIN_runд     ǰ汾: 1.0԰")
os.execute("mode con: cols=66 lines=35")

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

-- ʾϢ
print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)
print(colors.red .. '!κļֱӴ豸/tmp/' .. colors.reset)
print(colors.red .. "ȷļ3.19MB" .. colors.reset)
print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)

-- ȡűĿ¼
local script_dir = debug.getinfo(1).source:match("@?(.*/)")   --趨һΪ"script_dir"
if not script_dir then                                        --ѭȡ"./"·
    script_dir = "."
end

-- ʾûļ·ļ
io.write("ļ·ļ봰: ")               --ִͣ,ûı
local file_path = io.read()                                --ûļ趨Ϊ"file_path"
file_path = file_path:gsub('^%"', ''):gsub('%"$', '')      --ȥļ·˫ţУ

-- ļǷ
local function file_exists(path)                           --ȡõļ
    local file = io.open(path, "rb")                       --ȡûոļ
    if file then                                           --ʣµĴǼļ
        file:close()
        return true
    else
        return false
    end
end

if not file_exists(file_path) then                         --ļڣʾû
    print(colors.red .. "ļ: " .. file_path .. "ű˳" .. colors.reset)
    os.execute("pause")
	return
end

-- Ʋļ
local destination_path = script_dir .. "/mtds.new"         --Ҫļ"mtds.new"
local input_file = io.open(file_path, "rb")                --"rb"
local output_file = io.open(destination_path, "wb")        --"wb"

if input_file and output_file then                         --и
    output_file:write(input_file:read("*all"))
    input_file:close()
    output_file:close()
    print("ļѸƲΪ: " .. destination_path)    --
else
    print("ļʧܡ")                                --
    os.execute("pause")
	return                                                 --˳ű
end

-- busyboxļǷ
local busybox_path = script_dir .. "\\file\\busybox"              --Ҫļ
if not file_exists(busybox_path) then                      --
    print("busyboxļ: " .. busybox_path)           --
    os.execute("pause")
	return                                                 --˳ű
end

-- ʹadb pushļ䵽豸/tmp
os.execute('adb push "' .. destination_path .. '" /tmp/')  --ͨadbϴļ
os.execute('adb push "' .. busybox_path .. '" /tmp/')      --ͨadbϴļ
print(colors.green .. "ļbusyboxѳԴ䵽豸/tmpĿ¼".. colors.reset)
print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)

-- ɾʱļ
os.execute("del mtds.new")                                 --ɾ"mtds.new"ļ,ûոʱļ              

-- ѯûǷйؼԲ
print(colors.red .. '!Ĳ漰ؼMTD' .. colors.reset)
print(colors.red .. colors.bright .. 'ʧܻܿᵼ豸޷!' .. colors.reset)
print(colors.green .. 'ȷǷҪMTD²' .. colors.reset)
print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)
io.write("'yes'Լ")                            --ִͣ,ûı
local input = io.read()                                    --ûոıΪ"input"
if input ~= "yes" then                                     --ȶûǷΪ"yes"
  print("δͬ⣬˳")
  print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)
  os.execute("pause")
  os.exit() -- ˳
  else
  print(colors.green .. "ͬ⣬ִг" .. colors.reset)
  print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)
end

print(colors.red .. '!󼤻߳,Ŀʱ!' .. colors.reset)
delay.sleep(5) -- ӳ5
print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)

-- MTD/ˢд
print(colors.cyan .. colors.bright .. "ڳԸbusyboxؼȨ" .. colors.reset)
os.execute("adb shell chmod +x /tmp/busybox")
print(colors.green .."busyboxѱдȨ".. colors.reset)
print(" ")
print(colors.cyan .. colors.bright .. "ڳʹddˢдMTD4" .. colors.reset)
os.execute("adb shell /tmp/busybox nohup dd if=/tmp/mtds.new of=/dev/mtd4 &")
print(colors.red .. "޸ϺԶ豸" .. colors.reset)
print(colors.bright .. "(==0%)" .. colors.reset)
delay.sleep(10) -- ӳ10
print(colors.bright .. "(=======20%)" .. colors.reset)
print(colors.bright .. "(=============30%)" .. colors.reset)
print(colors.bright .. "(===================40%)" .. colors.reset)
print(colors.bright .. "(=========================50%)" .. colors.reset)
print(colors.bright .. "(===============================66%)" .. colors.reset)
delay.sleep(10) -- ӳ10
print(colors.bright .. "(======================================79%)" .. colors.reset)
delay.sleep(9) -- ӳ9
print(colors.bright .. "(===========================================83%)" .. colors.reset)
delay.sleep(12) -- ӳ12
print(colors.bright .. "(=================================================96%)" .. colors.reset)
delay.sleep(6) -- ӳ6
print(colors.bright .. "(=======================================================99%)" .. colors.reset)
delay.sleep(60) -- ӳһ
print(colors.cyan .. colors.bright .. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" .. colors.reset)
print(colors.green .. "дѾ,˵豸" .. colors.reset)
print(colors.red .. "ȴӺٰγ" .. colors.reset)
print(colors.red .. "пˢʱԤƵʱ䲻ͬ" .. colors.reset)
os.execute("pause")