/*****************************************************************************/

/*
 *      saveparam.c -- Save analog overlay settings into config file
 *
 *      Copyright (C) 2000 Sigma Designs
 *                    written by Nicolas Vignal <nicolas_vignal@sdesigns.com>
 *                    modified by Emmanuel Michon <emmanuel_michon@sdesigns.com>
 *                                Pascal Cannenterre <pascal_cannenterre@sdesigns.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*****************************************************************************/

#include "analogstruct.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

DWORD SaveParamForThisResolution(DWORD xresolution, DWORD yresolution, DWORD depth, DWORD freq){

  DWORD dwReturn;
  analogstructlist *thelist=NULL;

	dwReturn=LoadAllResolutions(&thelist);
 	UpdateResolutions(&thelist, xresolution, yresolution, depth, freq);
 	dwReturn=SaveParam(thelist);
 	CloseAllResolutions(thelist);

	return dwReturn;
}



DWORD UpdateResolutions(analogstructlist **thelist, DWORD xresolution, DWORD yresolution, DWORD depth, DWORD freq){
  DWORD dwReturn=0;
  DWORD found=0;
  int i=0;
  analogstructlist *current;
  analogstructlist *head;
	current=*thelist;

	while((current != NULL) && !found){
		if (current->object.XRESOLUTION == xresolution && current->object.YRESOLUTION == yresolution &&
    		current->object.DEPTH == depth && current->object.FREQUENCY == freq){
			found=1;
			while (current->object.paramtable[i].text != NULL){
        current->object.paramtable[i].value=FMPAnalogOverlay(FMP_ANALOG_OVERLAY_GET,current->object.paramtable[i].attribute,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG);
  	    i++;
  		}
			
		}else {
		  current=current->next;
		}
	}
  if (found==0){
  	// New resolution
  	head=*thelist;
    current=( analogstructlist *)malloc(sizeof(analogstructlist));
	 	current->next=head;
	 	*thelist=current;
		InitParamTable(&current->object);
		
    current->object.XRESOLUTION=xresolution;
		current->object.YRESOLUTION=yresolution;
		current->object.DEPTH=depth;
		current->object.FREQUENCY=freq;

		while (current->object.paramtable[i].text != NULL){
      current->object.paramtable[i].value=FMPAnalogOverlay(FMP_ANALOG_OVERLAY_GET,current->object.paramtable[i].attribute,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG);
 	    i++;
 		}

  	dwReturn=2;
  }
	
	return dwReturn;
}



// save the parameters in the .realmagicanalogoverlayrc file
// you MUST connect the novasever before using this function
// return
// 1 : cannot open the file
// 2 : structure doesn't match, please use the same version of your library

DWORD SaveParam(analogstructlist *thelist){

  FILE *fd;
	int iRes;
  char sRegFile[MAX_REG_LINE ];
  char sRegFilebackup[MAX_REG_LINE ];
 	char sLine[MAX_REG_LINE];
  int i;

  strcpy( sRegFile,getenv( "HOME" ) );
	strcat( sRegFile,"/");
	strcat( sRegFile,ANALOGREGISTRYFILE);
#ifdef _BARBADOS_
	// adding as last character the decoder number. 
	// REALmagicdevice is present in libnovaclient.so.
	strcat(sRegFile,REALmagicdevice+strlen(REALmagicdevice)-1);
#endif _BARBADOS_
	
	strcpy( sRegFilebackup,sRegFile );
	strcat(  sRegFilebackup, "~" );

	
 	// delete the old backup file
 	iRes = unlink(sRegFilebackup);
 	
  if ( iRes != 0 ) {
  	// create the new backup file
  	iRes = rename(sRegFile , sRegFilebackup );
  }

  fd = fopen( sRegFile,"w+");
  if (  fd == NULL ){
  	return 1;
  }

  while (thelist != NULL){
  	fprintf( fd,"[ %u x %u x %u x %u ]\n",(int)thelist->object.XRESOLUTION,(int)thelist->object.YRESOLUTION,(int)thelist->object.DEPTH,(int)thelist->object.FREQUENCY);

		i=0;
		while (thelist->object.paramtable[i].text != NULL){
		  sprintf( sLine,"%s = %d\n",thelist->object.paramtable[i].text,(int)thelist->object.paramtable[i].value);	
		  fprintf( fd,sLine);
			i++;
		}
		fprintf( fd,"\n");
    thelist=thelist->next;
  }
			
  fflush(fd);
  fclose(fd);

	return 0;
}



/*
  DWORD dwReturn;
  int i;

  analogstruct saveanalogparam;

  InitParamTable(&saveanalogparam);

 	saveanalogparam.XRESOLUTION=xresolution;
 	saveanalogparam.YRESOLUTION=yresolution;
 	saveanalogparam.DEPTH=depth;
	saveanalogparam.FREQUENCY=freq;
 		
	i=0;
  while (saveanalogparam.paramtable[i].text != NULL){
  	saveanalogparam.paramtable[i].value=FMPAnalogOverlay(FMP_ANALOG_OVERLAY_GET,saveanalogparam.paramtable[i].attribute,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG,UNUSED_ARG);
		i++;
	}


  dwReturn=SaveParam(&saveanalogparam);

  CloseParamTable(&saveanalogparam);

	return dwReturn;
} */


