
local Msg 				= Msg
local file 				= file
local util 				= util
local pairs				= pairs
local CreateConVar		= CreateConVar
local ConVarExists		= ConVarExists
local GetConVarNumber 	= GetConVarNumber 
local FCVAR_NOTIFY		= FCVAR_NOTIFY
local CONFIG_FILE 		= "../cfg/server_cfg.txt"

/*---------------------------------------------------------
	This module controls server settings.
   
	It reads the file defined by CONFIG_FILE, which it
	 stores in a table, then registers convars for each entry.
	
	The gamemode can then poll for server settings. One example
	 would be in the sandbox gamemode, it would poll for the 
	 max amount of ragdolls player's can spawn. This would be
	 done like so.
	 
	local maxragdolls = server_settings.Int( "sbox_maxragdolls", 4 )
	
	The 4 here represents the default value. So if the convar
	 doesn't exist it will just return 4.
	 
	The CONFIG_FILE is created automatically by the multiplayer
	 game panel (CREATE SERVER). The variables are read from
	 settings\server_settings\*.txt across all mods.
	 
	So don't forget to give your variables unique names or
	 your gamemode might conflict with other gamemodes.
   
---------------------------------------------------------*/

module("server_settings")

/******************* START SETUP *******************/

// Load the config file
local f = file.Read( CONFIG_FILE )

// Put it in a table
Settings = util.KeyValuesToTable( f )

// Create the convars if they don't already exist
for k, v in pairs( Settings ) do

	if ( !ConVarExists( k ) ) then
		CreateConVar( k, v, FCVAR_NOTIFY )
	end

end

/******************** END SETUP ********************/


/*---------------------------------------------------------
   Returns int server setting
---------------------------------------------------------*/
function Int( name, default )

	if ( !ConVarExists( name ) ) then return default end

	return GetConVarNumber( name )

end

/*---------------------------------------------------------
   Returns boolean server setting
---------------------------------------------------------*/
function Bool( name, default )

	if (default) then default = 1 else default = 0 end

	return Int( name, default ) != 0

end
