local enabled = minetest.settings:get_bool("update_checker_enabled", false)
if not enabled
then
	return
end


local S = minetest.get_translator("update_checker")

local modpath = minetest.get_modpath("update_checker")

--read game version from same file that's checked in the remote repo
local GAME_VERSION
do
	local file = io.open(modpath.."/game_version.txt")
	GAME_VERSION = file:read().."\n"
	file:close()
end

--(modpath.."/game_version.txt"), but in origin/master
local URL = "https://notabug.org/NetherEran/The_End/raw/master/mods/misc/update_checker/game_version.txt"




--this is called after the remote repo has been queried. If successful
--and the version string read above is different from the result of the
--query it displays a message to the player
local function on_http_request_arrived(result)
	if result.succeeded and result.data ~= GAME_VERSION
	then
		local res = string.sub(result.data, 1, string.len(result.data) - 1)
		minetest.after(2,
			function()
				minetest.chat_send_all(S("[Update available]\n" ..
					"Version @1 of The End is available at " ..
					"https://notabug.org/NetherEran/The_End", res))
			end)
	end
end


local http_api = minetest.request_http_api()

--http_api is nil if this mod is not listed as trusted with http
--requests in minetest.conf
if http_api
then
	--query the remote repository
	local httprequest =
	{
		url = URL,
		timeout = 3,
	}
	http_api.fetch(httprequest, on_http_request_arrived)
end
