/*=========================================================================
*
* IPOSave.c	- IPO saving functions
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include "IpoSave.h"

#include "SCR.h"			

#include "conventi.h"
#include "print.h"
#include "system.h"

#include "sprobj.h"

#include "RLISave.h"


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


/****************************************************************************
*		Description:	save IPO header
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveIpoFile (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  xString mes;

  // delete the existing section
  SCR_fn_v_SvL1_DeleteSection(p_stFile);

  // save header section
  sprintf(mes, "%s%s", M_IPO_HEADER, SCR_CC_sz_Cfg_SectionIdMark);
  SCR_M_SvL0_SaveBeginSection(p_stFile, mes, SCR_CC_C_Cfg_EOL);

  // save version number
  SCR_M_SvL0_SaveEntry(p_stFile, M_VersionNumber, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, "1");

  // save end section
  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
}


/****************************************************************************
*		Description:	save IPO section
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveIpoSection (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  MLT_tdxHandleToSuperObject	hSprObj;
  xString						mes, sFile, sAction, sIdent, sIdent2;
  char							*sPhysicalName, *p_char;

  // get super-object pointer
  hSprObj = (MLT_tdxHandleToSuperObject)(p_vPtr);
  // get corresponding section name
  sPhysicalName = (char *)(hSprObj->sLinkedObject);
  
  // add or rebuild section
  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
  }
  else
  {
    SCR_fn_v_SvL1_DeleteSection(p_stFile);
  }

  // get physical name 
  SCR_fn_v_RdL0_SplitSectionName(sPhysicalName, sFile, sAction, sIdent);
  // updae it with IPO prefix
  if( (p_char=strstr(hSprObj->sName, "=")) != NULL )
  {
    sprintf(sIdent2, "IPO_%s%s", sIdent, p_char);
  }
  else
  {
    sprintf(sIdent2, "IPO_%s", sIdent);
  }
  // get source file
  sprintf(sFile, "%s%s.ipo", g_sDirectorySave, g_sFileIn);

  // save IPO section
  if (hSprObj->iFlags & IS_MIRROR)
	SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_IpoMirror, sIdent2);
  else  
    SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_Ipo, sIdent2);
  SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(mes), SCR_CC_C_Cfg_EOL);

  // compute coresponding PO section
  SCR_fn_v_RdL0_SplitSectionName(sPhysicalName, sFile, sAction, sIdent);
  sprintf(sFile, "%s%s.phy", g_sDirectorySave, g_sFileIn);
  if( (p_char=strstr(sIdent, "=")) != NULL )
	  strcpy(p_char, "\0");
  sprintf(sIdent2, "PO_%s", sIdent);
  SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_Phy, sIdent2);
  // save PO entry
  SCR_M_SvL0_SaveEntry(p_stFile, M_IPOActionPO, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, mes);

  // if necessary, save RLI entry
  if(g_bGenerateRLI && g_lLightIndex)
  {
	  SCR_fn_v_RdL0_SplitSectionName(sPhysicalName, sFile, sAction, sIdent);
	  sprintf(sFile, "%s%s.rli", g_sDirectorySave, g_sFileIn);
	  sprintf(sIdent2, "ISI_%s", sIdent);
	  SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_ISI, sIdent2);

	  SCR_M_SvL0_SaveEntry(p_stFile, M_IPOActionRLI, SCR_CC_C_Cfg_NoChar);
	  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, mes);
  }

  // save end section
  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
}
