// DRM initialization : read the DRM dvars and parse the config files

drm_init()
{
	game["drm"] = [];
	
	for (i = 1; i <= 100; i ++)
	{
		dvar = "scr_drm_cfg_" + i;
		fname = getcvar (dvar);
		fdesc = OpenFile (fname, "read");
		
		if (fdesc != -1)
			ParseFile (fname, fdesc);
	}

	game["drm_initdone"] = true;
	
	Log ("DRM : " + game["drm"].size + " vars have been set");
}


// Just a logprint + newline

Log (str)
{
	logprint (str + "\n");
}


// Parse a config file

ParseFile (fname, fdesc)
{
	Log ("DRM : Reading config file " + fname);
	
	for ( ; ; )
	{
		elems = freadln (fdesc);
		
		if (elems == -1)
			break;
			
		if (elems == 0)
			continue;
	
		line = fgetarg (fdesc, 0);
//		Log ("Line read : >" + line + "<");
		
		if ((getsubstr (line, 0, 2) == "//") || (getsubstr (line, 0, 1) == "#"))
		{
//			Log ("		comment -> ignored\n");
			continue;
		}
		
		cleanline = "";
		last = " ";
		
		for (i = 0; i < line.size; i ++)
		{
//			Log ("line[i] : >" + line[i] + "<");
			switch (line[i])
			{
				case "	" :		// tab
				case " " :		// space 0xa0
				case " " :		// space 0x20
					if (last != " ")
					{
//						Log ("added space");
						cleanline += " ";
						last = " ";
					}
					break;

				case "/" :
					if (last == "/")
					{
//						Log ("exiting");
						i = line.size;		// exiting from for loop
						break;
					}
					else
					{
//						Log ("adding slash");
						cleanline += "/";
						last = "/";
					}
					break;

				case "#" :
//					Log ("exiting");
					i = line.size;			// exiting from for loop
					break;

				default :
//					Log ("adding : >" + line[i] + "<");
					cleanline += line[i];
					last = line[i];
					break;
			}
		}

		if ((cleanline.size >= 2) && (getsubstr (cleanline, cleanline.size - 2) == " /"))
		{
			// Ends with " /"
			notsocleanline = cleanline;
			cleanline = getsubstr (notsocleanline, 0, notsocleanline.size - 2);
		}
		
		if ((cleanline.size >= 1) && ((cleanline[cleanline.size - 1] == " ") || (cleanline[cleanline.size - 1] == "/")))
		{
			// Ends with " " or "/"
			notsocleanline = cleanline;
			cleanline = getsubstr (notsocleanline, 0, notsocleanline.size - 1);
		}
				
		if (cleanline == "")
		{
//			Log ("		nothing left -> ignored\n");
			continue;
		}
	
//		Log ("cleanline : >" + cleanline + "<");

		array = strtok (cleanline, " ");
		setcmd = array[0];
		
		if ((setcmd != "set") && (setcmd != "seta") && (setcmd != "sets"))
		{
//			Log ("		does not begin with set, seta or sets -> ignored\n");
			continue;
		}
		
		if (array.size == 1)
		{
//			Log ("		missing var name -> ignored\n");
			continue;
		}
		
		var = array[1];
		
		if (array.size == 2)
			// Value is null
			val = "";
		else
			// Value is not null
			val = getsubstr (cleanline, setcmd.size + var.size + 2);

//		Log ("		OK ! var " + var + " will be set to \"" + val + "\"\n");

		game["drm"][var] = val;
	}

	CloseFile (fdesc);
}


// Replacement for cvardef

cvardef (varname, vardefault, min, max, type)
{
	// Initialization must be done on 1st call
	if (! isdefined (game["drm_initdone"]))
		drm_init();
	
	mapname = getcvar("mapname");		// "mp_dawnville", "mp_rocket", etc.
	gametype = getcvar("g_gametype");	// "tdm", "bel", etc.
	multigtmap = gametype + "_" + mapname;

	tempvar = varname + "_" + gametype;	// i.e., scr_teambalance becomes scr_teambalance_tdm
	if(drm_getcvar(tempvar) != "") 		// if the gametype override is being used
		varname = tempvar; 		// use the gametype override instead of the standard variable

	tempvar = varname + "_" + mapname;	// i.e., scr_teambalance becomes scr_teambalance_mp_dawnville
	if(drm_getcvar(tempvar) != "")		// if the map override is being used
		varname = tempvar;		// use the map override instead of the standard variable

	tempvar = varname + "_" + multigtmap;   // check against the gametype and the map name
	if(drm_getcvar(tempvar) != "")
            varname = tempvar;

	// get the variable's definition
	switch(type)
	{
		case "int":
			if(drm_getcvar(varname) == "")		// if the cvar is blank
				definition = vardefault;	// set the default
			else
				definition = drm_getcvarint(varname);
			break;
		case "float":
			if(drm_getcvar(varname) == "")	// if the cvar is blank
				definition = vardefault;	// set the default
			else
				definition = drm_getcvarfloat(varname);
			break;
		case "string":
		default:
			if(drm_getcvar(varname) == "")		// if the cvar is blank
				definition = vardefault;	// set the default
			else
				definition = drm_getcvar(varname);
			break;
	}

	// if it's a number, with a minimum, that violates the parameter
	if((type == "int" || type == "float") && definition < min)
		definition = min;

	// if it's a number, with a maximum, that violates the parameter
	if((type == "int" || type == "float") && definition > max)
		definition = max;

	return definition;
}


// Replacement for getcvar

drm_getcvar (var)
{
	if (isdefined (game["drm"][var]))
		return (game["drm"][var]);
	
	return ("");
}


// Replacement for getcvarint

drm_getcvarint (var)
{
	if (isdefined (game["drm"][var]))
		return (atoi (game["drm"][var]));
	
	return (0);
}


// Replacement for getcvarfloat

drm_getcvarfloat (var)
{
	if (isdefined (game["drm"][var]))
		return (atof (game["drm"][var]));
	
	return (0);
}

atof (str)
{
	if ((! isdefined (str)) || (! str.size))
		return (0);

	switch (str[0])
	{
		case "+" :
			sign = 1;
			offset = 1;
			break;
		case "-" :
			sign = -1;
			offset = 1;
			break;
		default :
			sign = 1;
			offset = 0;
			break;
	}

	str2 = getsubstr (str, offset);
	parts = strtok (str2, ".");	

	intpart = atoi (parts[0]);
	decpart = atoi (parts[1]);

	if (decpart < 0)
		return (0);

	if (decpart)
		for (i = 0; i < parts[1].size; i ++)
			decpart = decpart / 10;

	return ((intpart + decpart) * sign);
}

atoi (str)
{
	if ((! isdefined (str)) || (! str.size))
		return (0);

	ctoi = [];
	ctoi["0"] = 0;
	ctoi["1"] = 1;
	ctoi["2"] = 2;
	ctoi["3"] = 3;
	ctoi["4"] = 4;
	ctoi["5"] = 5;
	ctoi["6"] = 6;
	ctoi["7"] = 7;
	ctoi["8"] = 8;
	ctoi["9"] = 9;
	
	switch (str[0])
	{
		case "+" :
			sign = 1;
			offset = 1;
			break;
		case "-" :
			sign = -1;
			offset = 1;
			break;
		default :
			sign = 1;
			offset = 0;
			break;
	}

	val = 0;
	
	for (i = offset; i < str.size; i ++)
	{
		switch (str[i])
		{
			case "0" :
			case "1" :
			case "2" :
			case "3" :
			case "4" :
			case "5" :
			case "6" :
			case "7" :
			case "8" :
			case "9" :
				val = val * 10 + ctoi[str[i]];
				break;
			default :
				return (0);
		}
	}
	
	return (val * sign);	
}