MOD = {};
MOD.sVersion = "1.1";
MOD.Version = tonumber(MOD.sVersion);
MOD.Tracker = "http://cryforce.gamevision.de/CryForce Productions/Projekte/Deep/Update/";
MOD.ConfigFile = "Mods/Deep/Tracker.cfg";
MOD.Checkfiles = 0;


function MOD.Init()
	MOD.Config = ReadTableFromFile(MOD.ConfigFile);

	if (MOD.Config == nil) then
		local handle = openfile(MOD.ConfigFile, "w");
		write (handle, "Tracker="..MOD.Tracker.."\r\n");
		closefile (handle);
		MOD.Config = ReadTableFromFile(MOD.ConfigFile);
	end	

	MOD.GetVersion = System:CreateDownload();
	MOD.GetVersion.URL  = MOD.Config.Tracker.."Moddesc.txt";
	MOD.GetVersion.Path = "Mods/Deep/Moddesc.txt";
	MOD.GetVersion.OnComplete = MOD.OnGetVersionComplete;
	MOD.GetVersion.OnError    = MOD.OnGetVersionError;
	MOD.GetVersion:Download(MOD.GetVersion.URL, MOD.GetVersion.Path);
	
	MOD.GetCFG = System:CreateDownload();
	MOD.GetCFG.URL  = MOD.Config.Tracker.."UpdateFiles.cfg";
	MOD.GetCFG.Path = "Mods/FarCry-Sports/UpdateFiles.cfg";
	MOD.GetCFG.OnComplete = MOD.OnGetCFGComplete;
	MOD.GetCFG.OnError    = MOD.OnGetCFGError;
	MOD.GetCFG:Download(MOD.GetCFG.URL, MOD.GetCFG.Path);
	
end


function MOD.OnGetCFGComplete()
	local szFile = MOD.GetVersion.Path;
	local Cfg = ReadTableFromFile(szFile);
	Cfg.Ver = tonumber(Cfg.Ver);

	Cfg.Size = tonumber(Cfg.Size);
	local mb = Cfg.Size;

	if (Cfg.Ver > MOD.Version) then
		UI.YesNoBox("FarCry Sports MOD Update", "Your FarCry Sports MOD found a newer Version.\n\nUpdatesize "..mb.." MB\n\nDo you want to start updating?", MOD.StartUpdate);
	else
		UI.MessageBox("FarCry Sports MOD Update", "Sorry, no update found!", MOD.OnMessageBoxError);
	end
end

function MOD.StartUpdate()

	local FilesCFG = "Mods/FarCry-Sports/UpdateFiles.cfg";
	local Lines = ReadTableFromFile(FilesCFG, 1);
	
	if (Lines) then
	
         UI.ProgressBox("Downloading in Progress", "FarCry Sports is currently downloading the update.\n\nPlease wait, but it could take a few minutes.\nDon't stop downloading if it isn't necessary!!!", MOD.OnGetVersionError);

		for i, File in Lines do

				MOD.Downloader(file);

		end
	end
end

function MOD.Downloader(file)

	MOD.Download = System:CreateDownload();
	MOD.Download.URL  = MOD.Config.Tracker..file..".pak";
	MOD.Download.Path = "Mods/FarCry-Sports/"..file..".pak";
	MOD.Download.OnComplete = MOD.OnDownloadComplete;
	MOD.Download.OnError    = MOD.OnDownloadError;
	MOD.Download:Download(MOD.Download.URL, MOD.Download.Path);
end

function MOD.OnGetVersionError()
         UI.MessageBox("Server Error", "Can't contact the Update-Server, please try it in a few minutes again!", MOD.OnMessageBoxError);
	return;
end

function MOD.OnMessageBoxError()
	return;
end

function MOD.OnDownloadError()
        MOD.Checkfiles = 0;
        UI:DeactivateScreen("ProgressDialog");
        UI.MessageBox("Download Error", "Downloading failed, please try again!", MOD.OnMessageBoxError);
	return;
end

function MOD.OnDownloadComplete()
        MOD.Checkfiles = MOD.Checkfiles + 1;
        
        local szFile = MOD.GetVersion.Path;
	local Cfg = ReadTableFromFile(szFile);
	Cfg.FilesCount = tonumber(Cfg.FilesCount);
        
        if( MOD.Checkfiles == Cfg.FilesCount ) then
            UI:DeactivateScreen("ProgressDialog");
            Game:SendMessage("Relaunch");
            MOD.Checkfiles = 0;
	end
end

function MOD.OnGetVersionComplete()
        return;
end

function MOD.OnGetCFGError()
         UI.MessageBox("Server Error", "Can't contact the Update-Server, please try it in a few minutes again!", MOD.OnMessageBoxError);
        return;
end