/* AMX Mod script.
*
* Message of the Day
*  by JustinHoMi
*
*/

#include <amxmod>

#define MAX_MOTD 10

new msg[MAX_MOTD][128]
new r_n[MAX_MOTD],g_n[MAX_MOTD],b_n[MAX_MOTD]
new hold[MAX_MOTD]
new mnum = 0
new cur = 0

public admin_motd()
{
    if (read_argc() < 4) {
        server_print("[AMX] Usage:  amx_motd ^"<text>^" <RRRGGGBBB> <hold time>")
        return PLUGIN_HANDLED
    }

    new color[11],hold_s[3]
    read_argv(1,msg[mnum],127)
    read_argv(2,color,10)
    read_argv(3,hold_s,3)

    new hostname[64]
    get_cvar_string("hostname",hostname,63)
    replace(msg[mnum],128,"%hostname%",hostname)

    server_print("[AMX] Loading motd %i ^"%s^"",mnum+1,msg[mnum])

    new r_s[4],g_s[4],b_s[4]
    copy(r_s,3,color)
    copy(g_s,3,color[3])
    copy(b_s,3,color[6])

    r_n[mnum] = strtonum(r_s)
    g_n[mnum] = strtonum(g_s)
    b_n[mnum] = strtonum(b_s)
    hold[mnum] = strtonum(hold_s)

    mnum++

    return PLUGIN_CONTINUE
}

public display_motd()
{
	set_hudmessage(r_n[cur], g_n[cur], b_n[cur], -1.0, 0.3, 0, 6.0, float(hold[cur]), 0.5, 0.15, 4)
	show_hudmessage(0,msg[cur])
	cur<mnum-1 ? cur++ : cur=0
	return PLUGIN_CONTINUE
}


public init_motd()
{
	if(file_exists("addons/amx/motd.tmp"))
	{
		new text[2],len
		read_file("addons/amx/motd.tmp",0,text,2,len)
		cur = strtonum(text)
		delete_file("addons/amx/motd.tmp")
	}

	new Float:motdtime = get_cvar_float("amx_motdtime") * 60
	set_task(5.0,"display_motd")
	set_task(motdtime,"display_motd",1,"",0,"b")
	return PLUGIN_CONTINUE
}

public plugin_end()
{
	new cur_s[3]
	numtostr(cur,cur_s,3)
	write_file("addons/amx/motd.tmp",cur_s)
}

public plugin_init()
{
    register_plugin("MOTD","0.7","JustinHoMi")
    register_srvcmd("amx_motd","admin_motd")
    register_cvar("amx_motdtime","10.0")
    set_task(5.0,"init_motd")
    return PLUGIN_CONTINUE
}
