/* AMX Mod script.
*
* (c) Copyright 2002, Noplay
* This file is provided as is (no warranties).
*
*/

/*

This AMX plugin requires MySQL MetaSmall module.

For this to work, you might create your MySQL table with this SQL query :

CREATE TABLE plugins (
  number smallint(3)
  plugin varchar(32) NOT NULL default '',
  enable tinyint(1) NOT NULL default '1',
  UNIQUE number (number)
) TYPE=MyISAM;



Notes :
. check $moddir/addons/amx/mysql.cfg for MySQL access configuration

*/

#include <amxmod>
#include <mysql>

public load_plugins()
{

	new mysql,cnt
	new plugin[33]
	new error[257]
	new host[65]
	new user[33]
	new pass[33]
	new db[33]
	
	get_cvar_string("amx_mysql_host",host,64)
	get_cvar_string("amx_mysql_user",user,32)
	get_cvar_string("amx_mysql_pass",pass,32)
	get_cvar_string("amx_mysql_db",db,32)

	mysql = mysql_connect(host,user,pass,db,error,256)
	if(mysql>0)
	{
		if(mysql_query(mysql,"SELECT plugin FROM `plugins` WHERE `enable`='1' ORDER BY `number`")>0)
		{
			cnt = 0
			delete_file("addons/amx/plugins/plugins.ini")
			while(mysql_nextrow(mysql)>0)
			{


				mysql_getfield(mysql,1,plugin,32)

				format(plugin,32,"%s.amx",plugin)

				
				write_file("addons/amx/plugins/plugins.ini",plugin,-1)
				
				cnt++				
			}
			server_print("[AMX] Loaded %d plugin(s) from database",cnt)
		}
		else
		{
			mysql_error(mysql,error,32)
			server_print("MySQL error : could not load plugins : '%s'",error)
		}
		mysql_close(mysql)
	}
	else
	{
		server_print("MySQL error : could not connect : '%s'",error)
	}
	return PLUGIN_HANDLED
}

public plugin_init()
{
	register_plugin("Plugins from MySQL","0.2","Noplay")
	register_cvar("amx_mysql_host","127.0.0.1")
	register_cvar("amx_mysql_user","root")
	register_cvar("amx_mysql_pass","")
	register_cvar("amx_mysql_db","amx")
	set_task(5.0,"load_plugins")
	return PLUGIN_CONTINUE
}