--MJTS-140914

local kitroot = ".\\Data\\MjTs-140914\\ms_keeper2nd\\"
local inifile = ".\\modules\\ms_settings.ini"

function read_ini(ctx)
    if inifile:sub(1,1) == "." then
        inifile = ctx.sider_dir .. inifile
    end
    local t = {}
    for line in io.lines(inifile) do
        local name, value = string.match(line, "^(keeper2nd)%s*=%s*([-%d.]+)")
        if name and value then
            t[name] = tonumber(value) or value
			keeper2nd = t[name]
        end
    end
    return t
end	

local function which_gk_model(filename)
    return string.match(
        filename, "(%d+)_[A-Z]+_GK(%d)[snrt][tdh]_realUni%.bin")
end

local function which_gk_tex(filename)
    return string.match(
        filename, "uniform.texture.[cu](%d+)g(%d)")
end

local function make_key(ctx, filename)
    ini = read_ini(ctx)
    local team_id, gkm = which_gk_model(filename)
    if gkm then
        team_id = tonumber(team_id)
        if team_id == ctx.away_team and keeper2nd == 1 then
            -- switch away GK to 2nd kit
            return string.gsub(filename, "_GK1st_", "_GK2nd_")
        end
    else
        local team_id, gkt = which_gk_tex(filename)
        if gkt then
            team_id = tonumber(team_id)
            if team_id == ctx.away_team and keeper2nd == 1 then
                -- return filename as is.
                -- This is needed so that we serve the texture
                -- file from our own root.
                return filename
            end
        end
    end
end

local function get_filepath(ctx, filename, key)
    if key and keeper2nd == 1 then
	    log("keeper2nd.enable: " .. keeper2nd)
        log(key)
        return kitroot .. key
    end
end

local function init(ctx)
    if kitroot:sub(1,1)=='.' then
        kitroot = ctx.sider_dir .. kitroot
    end
    ctx.register("livecpk_make_key", make_key)
    ctx.register("livecpk_get_filepath", get_filepath)
end

return { init = init }
