#include <sourcemod>
#include <cstrike>

#define CLANID "9110211"
#define GROUP "Game-Member"

public Plugin myinfo =
{
	name			= "UNLOZE Clantag",
	author			= "BotoX",
	description		= "Assign group to people wearing the UNLOZE clantag",
	version			= "1.0",
	url				= ""
};

public void OnPluginStart()
{
	/* Handle late load */
	for(int client = 1; client <= MaxClients; client++)
	{
		if(IsClientInGame(client) && IsClientAuthorized(client))
		{
			OnClientPostAdminFilter(client);
		}
	}
}

public void OnClientPostAdminFilter(int client)
{
	CheckClantag(client);
}

public void OnClientSettingsChanged(int client)
{
	CheckClantag(client);
}

void CheckClantag(int client)
{
	if(!IsClientAuthorized(client))
		return;

	char sClanID[32];
	GetClientInfo(client, "cl_clanid", sClanID, sizeof(sClanID));

	if(!StrEqual(sClanID, CLANID))
		return;

	AdminId AdmID;
	GroupId GrpID;

	if ((AdmID = GetUserAdmin(client)) == INVALID_ADMIN_ID)
	{
		LogMessage("Creating new admin for %L", client);
		
		AdmID = CreateAdmin("");
		SetUserAdmin(client, AdmID, true);
	}

	if ((GrpID = FindAdmGroup(GROUP)) != INVALID_GROUP_ID)
	{
		if (AdminInheritGroup(AdmID, GrpID))
			LogMessage("%L added to group %s", client, GROUP);
	}
	else
	{
		LogMessage("%L group not found %s", client, GROUP);
	}
}
