/*=========================================================================
*
* SaveMdf.c	- Saving functions for ModifLst.txt file
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include <windows.h>
#include <stdio.h>

#include "SaveModifLst.h"

#include "main.h"
#include "print.h"

#include "SctSave.h"


//--- Defines ---------------------------------------------------------------

// maximum number of directory texture in one session
#define MLT_C_MaxDirectoryTextures 10

// file for unknown texture
#define M_FichierTexture "textures.log"

// modification file for the VssDataManager
#define M_FichierSCR_Modif "ModifLst.txt"


//--- Global defines --------------------------------------------------------

BOOL	g_bPrintOutOK		= TRUE;

//--- Global statics --------------------------------------------------------

BOOL	gs_bTextureOK		= FALSE;
BOOL	gs_bFirstAccess		= TRUE;

FILE	*gs_stTextureFile;
long	gs_lOldPosInModifFile;

xString gs_a_sDirectoryTextures[MLT_C_MaxDirectoryTextures];
long	gs_lNbDirectoryTextures	= 0;


//--- Global externs --------------------------------------------------------



//--------------------------------------------------------------------



/****************************************************************************
*		Description:	Open the Texture.log file
*
*		Parameters:		sDir	: texture directory	
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vBeginSaveTextureFile (char *sDir)
{
  xString		sFileName;

  CreateDirectory(sDir, NULL);
  sprintf(sFileName,"%s\\%s", sDir, M_FichierTexture);

  gs_stTextureFile = fopen(sFileName, "at");

  if(gs_stTextureFile == NULL)
  {
	MLT_vOutput( C_ComRes_cErrorLine, "\nError : Can't open file %s", sFileName);
	gs_bTextureOK = FALSE;
  }
  else
    gs_bTextureOK = TRUE;
}
  

/****************************************************************************
*		Description:	if necessary, create file and directory
*
*		Parameters:		sDir		: texture directory
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
BOOL MLT_bFirstAccesInFile (char *sDir)
{
  long i=0;

  while( (i<gs_lNbDirectoryTextures) && stricmp(gs_a_sDirectoryTextures[i], sDir) ) i++;

  if(i == gs_lNbDirectoryTextures)
  {
    xString sFileName;

    // add a new file
    sprintf(gs_a_sDirectoryTextures[i], "%s", sDir);
    gs_lNbDirectoryTextures++;

    // delete file
    CreateDirectory(sDir, NULL);
    sprintf(sFileName,"%s\\%s", sDir, M_FichierTexture);
    gs_stTextureFile = fopen(sFileName, "w");
    if(gs_stTextureFile == NULL)
    {
	  MLT_vOutput( C_ComRes_cErrorLine, "\nError : Can't open file %s", sFileName);
    }
    else
    {
      fclose(gs_stTextureFile);
    }
    return TRUE;
  }
  return FALSE;
}
 
/****************************************************************************
*		Description:	Write a line in the Texture.log file 
*
*		Parameters:		sDir		: texture directory
*						sMessage	: line to write	
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vWriteInTextureFile(char *sDir, char *sMessage)
{
  if (g_bPrintOutOK)
  {
	if (MLT_bFirstAccesInFile(sDir))
	  gs_bFirstAccess = FALSE;

    MLT_vBeginSaveTextureFile(sDir);

    if (gs_bTextureOK)
      fprintf(gs_stTextureFile, "%s\n", sMessage);

    MLT_vEndSaveTextureFile();
  }
}


/****************************************************************************
*		Description:	Close the Texture.log file
*
*		Parameters:			
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vEndSaveTextureFile()
{
  if (gs_bTextureOK)
    fclose(gs_stTextureFile);
}





/****************************************************************************
*		Description:	save current position in modif file
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSavePosInModifFile()
{
  FILE *file;

  SetCurrentDirectory(g_sGameData);
  file = fopen(M_FichierSCR_Modif, "rt");
  if(file != NULL)
  {
    fseek(file, 0, SEEK_END);
    gs_lOldPosInModifFile = ftell(file);
    fclose(file);
  }
  else
    gs_lOldPosInModifFile = 0;
}


/****************************************************************************
*		Description:	add a line in the modif file
*
*		Parameters:		sLine	: line to add
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vAddEntryToModifFile (char *sLine)
{
  FILE *file;

  SetCurrentDirectory(g_sGameData);
  file = fopen(M_TempSCR_Modif, "at");
  if(file != NULL)
  {
    fputs(sLine, file);
    fclose(file);
  }
}


/****************************************************************************
*		Description:	update all paths in modif file
*
*		Parameters:		_p_cPath	: required path
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vUpdatePathInModifFile (char *_p_cPath)
{
  xString	sRead, sRead1, sWrite, sGame, sIdent;
  FILE		*fileR, *fileW, *fileA;
  char		*p_cPath, *p_cChar, *p_Char1;
  long		i;

  SetCurrentDirectory(g_sGameData);
  p_cPath = _p_cPath + strlen(g_sInitialDir) + 1;

  p_cChar = strrchr( g_sGameData, '\\' );
  strcpy(sGame, p_cChar+1);

  CopyFile(M_FichierSCR_Modif, "temp.scr", FALSE);
  if (g_bModifLst)
  {
	MLT_vOutput( C_ComRes_cNormalLine, "\n\tUpdate ModifLst...");

	fileR = fopen(M_TempSCR_Modif, "rt");
	fileW = fopen(M_FichierSCR_Modif, "at");
  
	if (fileR==NULL) return;
	if (fileW==NULL) return;

	while(fgets(sRead, 256, fileR) != NULL)
	{
		p_cChar=strstr(_p_cPath, sGame);
		sprintf(sWrite, "(%c)%s%s", *(sRead+1), p_cChar, sRead+3);

		fileA = fopen("temp.scr", "rt");
		if (fileA)
		{	fgets(sRead1, 256, fileA);
			if (!strcmp(sRead1, sWrite) && (*(sWrite+1) == 'A'))
				*(sWrite+1) = 'R';
			else
				while(ftell(fileA) < gs_lOldPosInModifFile)
				{	fgets(sRead1, 256, fileA);
					if (!strcmp(sRead1, sWrite) && (*(sWrite+1) == 'A'))
					{	*(sWrite+1) = 'R';
						break;
					}
				}
			fclose(fileA);
		}
	  fputs(sWrite, fileW);
	}
    for (i=0; i<g_lNbOldSector; i++)
	{   strcpy(sIdent, p_cChar);
		sIdent[strlen(sIdent)-1]='\0';
		p_Char1=strrchr(sIdent, '\\');
		sprintf(sWrite, "(D)%s%s.sct^Sector:%s\n", p_cChar, p_Char1+1, g_a_sOldSector[i]);
		fputs(sWrite, fileW);
	}

	for (i=0; i<g_lNbOldSector; i++)
		strcpy(g_a_sOldSector[g_lNbOldSector], "") ;
	g_lNbOldSector = 0;
  
	if (g_bExistMdt)
	{  strcpy(sIdent, p_cChar);
       sIdent[strlen(sIdent)-1]='\0';
	   p_Char1=strrchr(sIdent, '\\');
       sprintf(sWrite, "(D)%s%s.mdt\n", p_cChar, p_Char1+1);
	   fputs(sWrite, fileW);
	}
	if (g_bExistMdf)
	{  strcpy(sIdent, p_cChar);
	   sIdent[strlen(sIdent)-1]='\0';
	   p_Char1=strrchr(sIdent, '\\');
       sprintf(sWrite, "(D)%s%s.mdf\n", p_cChar, p_Char1+1);
	   fputs(sWrite, fileW);
	}

	gs_lOldPosInModifFile = ftell(fileW);

	fclose(fileR);
	fclose(fileW);
  }
  remove("temp.scr");

  remove(M_TempSCR_Modif);
}











