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

-- ñն붨崰ڴС
os.execute("title WIFI ǰ汾: 5.2 ,ڼ汾")
os.execute("mode con: cols=60 lines=15")

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

-- ȡǰűĿ¼
local script_path = debug.getinfo(1, "S").source:sub(2)
local script_dir = script_path:match("(.*/)") or script_path:match("(.+\\)") or "./"

-- ־ļ·
local flag_file = script_dir .. "dyc"

-- жǷΪһ
local file = io.open(flag_file, "r")
if file then
    file:close()
    
    -- ʹü±ļ
    print("⵽ֵһ")
    print("ڴļֶ༭...")
	delay.sleep(4)
    os.execute('start notepad.exe helper.ini')
    -- ɾ־ļظʾ
    os.remove(flag_file)
	delay.sleep(2)
end

local function print_tips()
   print(colors.green .."ڼƶ˰汾,ĵȴ\n\n" .. colors.reset)
end

local function print_start()
   print()
   print()
   print(colors.green .."ڴ,Եȼ......\n" .. colors.reset)
end

-- ȴû
local function wait_for_user_input()
    print()
    print(colors.yellow .. "⵽°汾..." .. colors.reset)
    os.execute("pause >nul")  -- Windowsµȴû
	print()
	delay.sleep(1)
	print()
	print(colors.red .. "ÿθ¶µĺù,ǽ" .. colors.reset)
	delay.sleep(5)
end
-- Build汾ͨ
local function get_version_path()
    local default_path = "helper"  -- Ĭ·
    local file = io.open("helper.ini", "r")
    if not file then
        return default_path  -- ļڣʹĬ
    end
    for line in file:lines() do
        -- δע͵[Build version channel]
        if line:match("^%s*%[Build version channel%]%s*$") then
            file:close()
            return "helper-build"  -- ʹBuildͨ·
        -- ע͵#[Build version channel]
        elseif line:match("^%s*#%s*%[Build version channel%]%s*$") then
            file:close()
            return default_path  -- ʹĬ·
        end
    end
    file:close()
    return default_path  -- δҵãʹĬ
end

-- ȡƶ˰汾Ϣ
local function check_version()
-- ȡ helper.ini еˢڣ죩
local function read_refresh_cycle(file_path)
    local file = io.open(file_path, "r")
    if not file then
        print("δҵ helper.iniа汾⡣")
        return nil
    end
    for line in file:lines() do
        local cycle = line:match("refresh cycle%s*=%s*(%d+)")
        if cycle then
            file:close()
            return tonumber(cycle)
        end
    end
    file:close()
    print("δ helper.ini ҵ refresh cycle а汾⡣")
    return nil
end
--  version.ini лȡϴμʱ䣨ʽreview time: YYYY-MM-DD
local function read_last_check_time(file_path)
    local file = io.open(file_path, "r")
    if not file then
        print("δҵ version.iniа汾⡣\n")
        return nil
    end
    for line in file:lines() do
        local y, m, d = line:match("review time:%s*(%d+)%-(%d+)%-(%d+)")
        if y then
            file:close()
            return os.time{year=tonumber(y), month=tonumber(m), day=tonumber(d), hour=0, min=0, sec=0}
        end
    end
    file:close()
    print("δ version.ini ҵ review time ¼а汾⡣")
    return nil
end
-- жǷҪ
local function should_check_update()
    local cycle = read_refresh_cycle("helper.ini") -- 
    local last_time = read_last_check_time("version.ini")
    if not cycle or not last_time then
        return true -- ȱļ//ʱϢǿƼ
    end
    local now = os.time()
    local diff_days = os.difftime(now, last_time) / (60 * 60 * 24) -- ת
    if diff_days >= cycle then
    return true
else
    if diff_days < 1 then
        print("ϴμǽ죬汾⡣")
    else
        print(string.format("ϴμ %.1f ǰδﵽ %d ĸ", diff_days, cycle))
		print(string.format("汾⡣", diff_days, cycle))
    end
    -- ´μʱ
    local next_time = last_time + cycle * 24 * 60 * 60
    local next_date = os.date("%Y-%m-%d", next_time)
    print("Ԥƽ " .. next_date .. " ٴμ⡣")
    return false
end
end
-- Ҫ⣬
if not should_check_update() then
    delay.sleep(2)
    return
end

    -- ͨget_version_path()ȡʵȡ·
    local version_path = get_version_path()
    local version_urls = {
        "https://punguin.pages.dev/" .. version_path,      -- ƴ·
        --"http://47.239.84.169/" .. version_path,            -- ƴ·
		"http://127.0.0.1:0721/" .. version_path                   -- ƴ·
    } -- б
    local local_version = "5.2" -- 滻Ϊصİ汾
    local temp_version_file = "version.ini" -- ʱ汾ļ
    local cloud_version = nil  -- ע,ڱʱҪipе
    -- Ӱ汾ļȡƶ˰汾
    local function get_version_from_html(file_path)
        local file = io.open(file_path, "r")
        if not file then
            return nil
        end
        for line in file:lines() do
            local version = line:match("%d+%.%d+[%w%-]*") -- "%d.%d"Ϊ汾Ÿʽ,Ӧİ汾ΪX.X(ָʽ)
            if version then
                file:close()
                return version
            end
        end
        file:close()
        return nil
    end
    -- ļǷЧ
    local function is_file_valid(file_path)
        local file = io.open(file_path, "r")
        if not file then
            return false
        end
        local content = file:read("*a")
        file:close()
        return content and #content > 0 and not content:find("404") -- Ƿ404
    end
    -- Դÿذ汾ļ
    for _, url in ipairs(version_urls) do
        -- ʹcurlذ汾ļ
		os.execute(string.format('"%s" -l -o %s %s >nul 2>nul', path.curl, temp_version_file, url))
        
        if is_file_valid(temp_version_file) then
            cloud_version = get_version_from_html(temp_version_file)
            if cloud_version then
                break -- ɹȡƶ˰汾ţ˳ѭ
            end
        end
    end
    --os.remove(temp_version_file) -- ɾʱ汾ļ
    -- 汾Ϣ
    if cloud_version then
	-- ȡǰʱ䲢д汾ļ
local function write_check_time(file_path)
    local time_str = os.date("\n\nreview time: %Y-%m-%d") -- ʽʱ䣺--
    local file = io.open(file_path, "a") -- ʹ׷ģʽ
    if file then
        file:write(time_str)
        file:close()
    end
end
write_check_time(temp_version_file)
        print(colors.bright .. "°汾: " .. cloud_version .. "  ǰ汾: " .. local_version .. colors.reset .. "\n")
        print(colors.blue .. colors.bright .. "ƶ°汾,ֺ'new'\n" .. colors.reset)
        print()
        print("Ц:1.2")
		print()
        -- жǷҪȴû
        if cloud_version > local_version then
            wait_for_user_input()  -- ƶ˰汾ȱذ汾ߣȴû
        else
        end
    else
        print(colors.bright .. "޷ȡƶ˰汾\n" .. colors.reset)
    end
end
print_tips()
check_version()
print_start()
delay.sleep(2)
os.execute("bin\\lua54 lua\\helper.lua")