// plugin_desnotes_strcfg_rotation.sma version .90, By desTubes@tampabay.rr.com
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
/*****************************************************
  Set to 1 to have the files update when someone    
  connects, or 0 to have them change on map change   */
// #define UPDATE_ON_CONNECT 0
/*****************************************************/
 
/*****************************************************
  Set to 1 to have iDEBUG statements execute 
  (usually print ), or 0 to ignore them   */
new iDEBUG = 0;
/*****************************************************/
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.26";
#define DEBUG 0
 
public plugin_init() {
	say("plugin init");
	plugin_registerinfo("desNotes Stripper2 Config file rotation", "Changes the previous map strcfg file during map change", STRING_VERSION);
 
	new VaultCurNum = 0; 
	new VaultMaxNum = 0;
	new VaultNewNum = 0;
	new VaultRandomNum = 0;
	new MapName[MAX_NAME_LENGTH];
	new CurMapName[MAX_NAME_LENGTH];
	new MapFile[MAX_NAME_LENGTH];
	new MapFilePath[MAX_NAME_LENGTH];
	new NewMapFile[MAX_NAME_LENGTH];
	new NewMapFilePath[MAX_NAME_LENGTH];
	new VaultCurMap[MAX_NAME_LENGTH];
	new VaultMaxMap[MAX_NAME_LENGTH];
	new Command[MAX_DATA_LENGTH];
	new Text[MAX_TEXT_LENGTH] = "";
	new TextNum[MAX_TEXT_LENGTH] = "";
	new count;
	new FileSize;
	new Temp[MAX_DATA_LENGTH];
 
	if (iDEBUG) say("rotation plugin start");
 
	// Get Current Map
	currentmap(MapName,MAX_NAME_LENGTH);	
	snprintf(CurMapName,MAX_NAME_LENGTH, MapName);
	if (iDEBUG) say("CurrentMapName=");
	if (iDEBUG) say(CurMapName);
 
	// Get Vault.ini var <mapname>_max
	snprintf(VaultMaxMap,MAX_NAME_LENGTH,"%s_max",CurMapName);	// Crossfire_max
	if (iDEBUG) say("VaultMaxMap");
	if (iDEBUG) say(VaultMaxMap);
	get_vaultdata(VaultMaxMap, Command, MAX_NUMBER_LENGTH);
	if (iDEBUG) say(Command);
	VaultMaxNum = strtonum(Command);
 
	if (VaultMaxNum > 0){
		if (iDEBUG) say("VaultMaxNum is not Null");
 
		// Get VaultRandomNum
		get_vaultdata("STRIPPER2_RANDOM", Command, MAX_NUMBER_LENGTH);
		if (iDEBUG) say("VaultRandomNum=");
		if (iDEBUG) say(Command);
		VaultRandomNum = strtonum(Command);
 
		// Get Vault.ini var <mapname>_cur
		snprintf(VaultCurMap,MAX_NAME_LENGTH,"%s_cur",CurMapName);	
		if (iDEBUG) say("VaultCurMap=");
		if (iDEBUG) say(VaultCurMap);
		get_vaultdata(VaultCurMap, Command, MAX_NUMBER_LENGTH);
		VaultCurNum = strtonum(Command);
		if (iDEBUG) say(Command);
 
		// Construct Stripper config file name for current map (mapname_str.cfg)
		snprintf(MapFile,MAX_NAME_LENGTH,"%s_str.cfg",CurMapName);	
		snprintf(MapFilePath,MAX_NAME_LENGTH,"maps/%s",MapFile);	
		if (iDEBUG) {
			say("MapFile=");
			say(MapFile);
			say("MapFilePath=");
			say(MapFilePath);
		}
 
		// StrConfig will be determined randomly or sequencially
		// Based on Vault.ini CVar...if there are only 2 map files then 
		// skip random
		if ((VaultRandomNum) && (VaultMaxNum > 2)){
 
			// Randomly pick next VaultCurNum
			new tmpText[MAX_TEXT_LENGTH] = "";
			new tmpVal = random(VaultMaxNum) + 1;
			numtostr(tmpVal,tmpText);
			VaultNewNum = tmpVal;
			if (iDEBUG) say("RandomNum=");
			if (iDEBUG) say(tmpText);
		} else {
 
			// Update VaultCurNum by 1
			if (VaultCurNum < VaultMaxNum){
				if (iDEBUG) say("add one");
				VaultNewNum= VaultCurNum + 1;
			} else {
				if (iDEBUG) say("back to one");
				VaultNewNum = 1;
			}
		}
 
		// Construct Stripper file name to replace current stripper file
		snprintf(NewMapFile,MAX_NAME_LENGTH,"%s%d_str.cfg",CurMapName,VaultNewNum);
		snprintf(NewMapFilePath,MAX_NAME_LENGTH,"maps/%s",NewMapFile);	
		if (iDEBUG) {
			say("NewMapFile=");
			say(NewMapFile);
			say("NewMapFilePath=");
			say(NewMapFilePath);
		}
 
		// Update Vault.ini var mapname_cur
		snprintf(Command,MAX_NUMBER_LENGTH,"%d",VaultNewNum);
		set_vaultdata(VaultCurMap,Command);
		// set_vaultdata("crossfire_cur",Command);
		numtostr(VaultNewNum,TextNum);
		strcat(Text,"VaultNewNum= ",MAX_DATA_LENGTH);
		strcat(Text,TextNum,MAX_DATA_LENGTH);
		if (iDEBUG) say(Text);
		log(Text);
 
		// Delete mag strcfg file and replace it with selected one
		if (fileexists(NewMapFilePath)){
			if (iDEBUG) say("Found NewMapFile");
			FileSize = filesize(NewMapFilePath,lines);
			deletefile(MapFilePath);
			for(count = 1; count<=FileSize;count++){
				readfile(NewMapFilePath,Temp,count,MAX_DATA_LENGTH);
				writefile(MapFilePath,Temp,-1);
			}
		} else {
			if (iDEBUG) say("Could not find NewMapFile");
		}
	} else {
		if (iDEBUG) say("VaultMaxNum is Null");
	}
	if (iDEBUG) say("rotation plugin end");
	return PLUGIN_CONTINUE;
}