


local meta = FindMetaTable( "Player" )
if (!meta) then return end


/*---------------------------------------------------------
    Is an Admin
---------------------------------------------------------*/
function meta:IsAdmin()

	if ( self:IsSuperAdmin() ) then return true end
	if ( self:IsUserGroup("admin") ) then return true end

	return false
	
end

/*---------------------------------------------------------
    Is a Super Admin
---------------------------------------------------------*/
function meta:IsSuperAdmin()

	// We're always a super admin in SP
	if ( SinglePlayer() ) then return true end
	
	// Always a super admin if this is our server
	if ( self:IsListenServerHost() ) then return true end

	return ( self:IsUserGroup("superadmin") )
	
end
	
/*---------------------------------------------------------
    Is Usergroup X
	If you're really customizing your server you can add custom
	usergroups and modify your scripts to accomodate them easily
---------------------------------------------------------*/
function meta:IsUserGroup( name )

	return ( self:GetTable().UserGroup == name )
	
end


/*---------------------------------------------------------
    SetUserGroup
	Sets the user's group (Server only)
---------------------------------------------------------*/
function meta:SetUserGroup( name )

	self:GetTable().UserGroup = name
	
	if (SERVER) then
	
		self:SendLua( "LocalPlayer():SetUserGroup( \""..name.."\" )" )
	
	end
	
end


/*---------------------------------------------------------
    This is the meat and spunk of the player auth system
---------------------------------------------------------*/

if ( SERVER ) then

// SteamIds table..
// STEAM_0:1:7099:
//	 	 name	=	garry
//	 	 group	=	superadmin
local SteamIDs = {}

// Load the users file
local UsersKV = util.KeyValuesToTable( file.Read( "../settings/users.txt" ) )

// Extract the data into the SteamIDs table
for key, tab in pairs( UsersKV ) do

	for name, steamid in pairs( tab ) do
	
		SteamIDs[ steamid ] = {}
		SteamIDs[ steamid ].name = name
		SteamIDs[ steamid ].group = key
	
	end

end

PrintTable( SteamIDs )

local function PlayerSpawn( pl )

	local steamid = pl:SteamID()
	
	if ( SteamIDs[ steamid ] == nil ) then
		pl:SetUserGroup( "user" )
		return
	end

	pl:SetUserGroup( SteamIDs[ steamid ].group )
	pl:PrintMessage( HUD_PRINTTALK, "Hey '"..SteamIDs[ steamid ].name.."' - You're in the '"..SteamIDs[ steamid ].group.."' group on this server." )
	
end

hook.Add( "PlayerInitialSpawn", "PlayerAuthSpawn", PlayerSpawn )

end

