local utils = require("mp.utils")

function getKey(table, key)
    for _, item in ipairs(table) do
        local k, v = item:match("([^=]+)=(.+)")
        if k == key then
            return v
        end
    end
    return nil -- Key not found
end

function auth()
    local url = mp.get_property("stream-open-filename")
    if url:find("files.hydrar.de") then
        local env = utils.get_env_list()
        local auth = getKey(env, "FILES_AUTH")
        if auth == nil then
            auth = mp.get_opt("FILES_AUTH")
        end
        if auth ~= nil then
            mp.set_property("http-header-fields", "Authorization: Basic " .. auth)
        else
            print("Authorization value not found.")
        end
    end
end

mp.register_event("start-file", auth)