

/* AMX Mod script.
*
*Release date: 09/11/2002
*For amx v0.61 
*
* (c) Copyright 2002, Manus Freedom
* This file is provided as is (no warranties).
*
*/

#define MAX_DATA_LENGTH 256

#include <amxmod> /* read this file to learn the functions */

findForward( message[], start, num, toFind ){
	new count = 0
	for(new x=start;x<MAX_DATA_LENGTH;x++){
		if(message[x]==toFind){
			count++
			if( count >= num ){
				return x
			}
		}
	}
	return -1
}
extractStringValue( message[], begQuote, tempStr[] ){
	new c
	new end = findForward( message, begQuote + 1, 1, '"' )
	for( new x = 0;; x++ ){
		//if weve reached the end or its the end of our storing string
		if( begQuote + 1 + x == end || x == MAX_DATA_LENGTH - 1 ){
			tempStr[x]=0 //null terminate the string
			break
		}
		c = message[ begQuote + 1 + x ]
		if( c == ' ' ) //replace spaces with '$'
		    tempStr[ x ] = '$'
		else
		    tempStr[ x ] = c
	}
}
gamehostcfg(){
	new line[MAX_DATA_LENGTH]
	new len=0
	new i=0
	new configfile[MAX_DATA_LENGTH]
	new port[MAX_DATA_LENGTH]
	get_cvar_string("port",port,MAX_DATA_LENGTH)
	format(configfile,MAX_DATA_LENGTH,"%s.cfg", port)
	if(file_exists(configfile)==0){
		return
	}
	i=0
	while(read_file(configfile,i,line,MAX_DATA_LENGTH,len)!=0){
		read_file(configfile,i,line,MAX_DATA_LENGTH,len)
		if(equal(line,"mghcfg",5)){
			log_message("[MFGameHost] Config File Already Modified")
			return
		}
		i++
	}
	new mapfile[MAX_DATA_LENGTH]
	format(mapfile,MAX_DATA_LENGTH,"%s.txt", port)
	if(file_exists(mapfile)==1){
		delete_file(mapfile)
	}
	write_file(mapfile,"")
	i=0
	while(read_file(configfile,i,line,MAX_DATA_LENGTH,len)!=0){
		read_file(configfile,i,line,MAX_DATA_LENGTH,len)
		if(equali(line,"maplist",7)){
			new mapnum = strtonum(line[7])
			for ( new j = 0; j < mapnum; j++){
				new linetmp[MAX_DATA_LENGTH]
				if ( read_file(mapfile,j,linetmp,MAX_DATA_LENGTH,len) == 0 ){
					write_file(mapfile,"")
				}
			}
			new mapname[MAX_DATA_LENGTH]
			extractStringValue( line, findForward( line, 0, 1, '"'), mapname )
			write_file(mapfile,mapname,mapnum)
		}
		if(equali(line, "map", 3)){
			write_file(configfile,"",i)
		}
		if(equali(line, "game", 4)){
			write_file(configfile,"",i)
		}
		if(equali( line, "port",4 )){
			write_file(configfile,"",i)
		}
		i++
	}
	set_cvar_string("mapcyclefile", mapfile)
	log_message("[MFGameHost] Config File Modified")
	server_cmd("exec %s.cfg", port );
	log_message("[MFGameHost] Config File Executed")
	write_file(configfile,"mghcfg",0)
}
public plugin_init()
{
	register_plugin("Manus Freedom GameHost","0.61","Manus Freedom")
	gamehostcfg()
	return PLUGIN_CONTINUE
}

