#include "../elastro.h"
#include "eltel.h"


char configfile[128] = "eltelrc\0";



void cfg_alert( char *txt, FILE *fp ) {
	fprintf(stdout,"\r\n%s\r\n",txt);
	if( fp != NULL ) {
		fclose( fp );
	}
}





void cfg_init_defaultvalues( void )
{

	/*only serial interface*/
	serflowmotorstart			= 0;
	serflowazhspersecsslow     	= 0;
	serflowalthspersecsslow		= 0;
	serflowazhspersecsfast     	= 0;
	serflowalthspersecsfast		= 0;



	/*we use this from configuration */


	scope.utcadjust 				= -7200;
	scope.latitude  				= 48.766667;
	scope.longitude 				= 9.1833333;

	scope.alt_horizontlimit 		= 0;
	scope.alt_zenitlimit    		= 120;

	scope.az_halfstepsize  			= 1.0693065;
	scope.alt_halfstepsize 			= 1.0983045;
	scope.fr_halfstepsize  			= 1.0;


	scope.timerintct_goto			= 0;
	scope.timerintct_correction   	= 2;

	scope.az_gotospeed				= 8;
	scope.alt_gotospeed				= 8;
	scope.fr_gotospeed				= 8;
	
	scope.az_correctionspeed		= 20;
	scope.alt_correctionspeed		= 20;
	scope.fr_correctionspeed		= 20;

	scope.mstimerintct_goto			= 0;
	scope.mstimerintct_correction 	= 1;

	scope.msaz_gotospeed			= 4;
	scope.msalt_gotospeed			= 4;
	scope.msfr_gotospeed			= 4;
	
	scope.msaz_correctionspeed	= 10;
	scope.msalt_correctionspeed	= 10;
	scope.msfr_correctionspeed	= 10;


	scope.lastazhs				= 0;
	scope.lastalths               = 0;

	scope.hstepmaximumspeed       = 500;
	scope.calcrefraction			= 1;
	scope.temperature				= 10.0;
	scope.airpressure				= 1013.0;


	scope.fruse                   = 0;
	scope.fr_msecperhs            = 1000.0;


	guide.az_correctionhs    	= 1.0;
	guide.alt_correctionhs   	= 1.0;
	
	
	prog.debug                   = 0;
	prog.iftype		 			= 0;
	prog.guiderunning 			= 0;
	prog.motorrun     			= MNORUN;
	prog.connection   			= 0;

	prog.looprun					= 0;
	prog.keypad   				= 0;

	prog.event					= EVT_NOTHING;


	sprintf( prog.azstr, "000:00:00\0");
	sprintf( prog.altstr,"000:00:00\0");
	sprintf( prog.frstr,"000:00:00\0");

	sprintf( prog.rastr, "000:00:00\0");
	sprintf( prog.decstr,"000:00:00\0");

	sprintf( prog.objname,"none\0");
	sprintf( prog.objtype,"none\0");

	prog.objmark = 0;
	prog.skipstartupmotor = 0;
	
	server.serverrun = 0;	
	sprintf( server.servicename,"eltel\0");
	sprintf( server.envname,"ELTELPORT\0");
	server.initport = -1;

	server.sock 		 = INVALID_SOCKET;
	server.serversock  = INVALID_SOCKET;
		
	server.timeoutread  = 3;
	server.timeoutwrite = 3;	
	server.start        = 0;

	server.servertype   	  = 0;
	server.fifoinfile[0]    = 0;
	server.fifooutfile[0]   = 0;
	
	xephem.start            = 0;
	xephem.xefiforun		  = 0;
	xephem.fifoinfile[0]    = 0;
	xephem.fifooutfile[0]   = 0;
	xephem.fifooutloop      = 0;
	xephem.fifooutloopbusy  = 0;
	xephem.fifooutloopsave  = 0;
	xephem.keypadspecial    = 0;
			
	monitor.start		 = 0;
	monitor.monitorrun   = 0;
	monitor.objdbpath[0] = 0;
	
	sprintf( monitor.azstr, "000:00:00\0");
	sprintf( monitor.altstr,"000:00:00\0");
	sprintf( monitor.rastr, "000:00:00\0");
	sprintf( monitor.decstr,"000:00:00\0");


	exfifo.start 		  = 0;
	sprintf(exfifo.fifoinfile,"/tmp/elcam_fifoout\n");
	exfifo.fiforun		  = 0;
	exfifo.invert		  = 0;
	exfifo.follow         = 0;
	exfifo.azsteps        = 1.0;
	exfifo.altsteps       = 1.0;


	encoder.type		  = NOENCODER;
	encoder.active		  = 0;
	encoder.azsighs	  	  = 1.0;
	encoder.altsighs	  = 1.0;
	encoder.azpulse		  = 0.0;
	encoder.altpulse	  = 0.0;
	encoder.azpulse360    = 4000.0;
	encoder.altpulse90    = 1000.0;
	encoder.azleft		  = -1;
	encoder.altdown       = -1;
	encoder.val           = 0;
	encoder.cazhs         = 0.0;
	encoder.calths        = 0.0;
	encoder.correctazhs   = 0.0;
	encoder.correctalths  = 0.0;
	encoder._altflag      = 0;
	encoder._azflag       = 0;
	encoder.aztolerance   = 0.1;
	encoder.alttolerance  = 0.1;
	encoder.debugazcount  = 0;
	encoder.debugaltcount = 0;
	encoder.az_errcount   = 0;
	encoder.alt_errcount  = 0;
}




int cfg_init_scopevalues( void )
{
	double v;
	
	scope.hs_az360 = 1296000;
	scope.hs_az270 = 972000;
	scope.hs_az180 = 648000; 
	scope.hs_az90  = 324000;
	
	if(scope.az_halfstepsize > 0){		

		scope.hs_az360 = 1296000.0 / scope.az_halfstepsize;
		scope.hs_az270 =  972000.0 / scope.az_halfstepsize;
		scope.hs_az180 =  648000.0 / scope.az_halfstepsize;
		scope.hs_az90  =  324000.0 / scope.az_halfstepsize;	
	}


	scope.hs_alt180 = 648000; 
	scope.hs_alt90  = 324000;
	scope.hs_altlb  = 0;
	scope.hs_altub  = 324000;

	if(scope.alt_halfstepsize > 0){

		scope.hs_alt180 =  648000.0 / scope.alt_halfstepsize;
		scope.hs_alt90  =  324000.0 / scope.alt_halfstepsize;

		if(scope.alt_horizontlimit > 0 ){
			v = scope.alt_horizontlimit * 3600;
			scope.hs_altlb = v / scope.alt_halfstepsize;
		}

		if(scope.alt_zenitlimit > 0 ){
			v = scope.alt_zenitlimit * 3600;
			scope.hs_altub = v / scope.alt_halfstepsize;
		}				

	}
		


	scope.hs_fr360 = 1296000;
	scope.hs_fr270 = 972000;
	scope.hs_fr180 = 648000; 
	scope.hs_fr90  = 324000;

	if(scope.fr_halfstepsize > 0){		
		scope.hs_fr360 = 1296000.0 / scope.fr_halfstepsize;
		scope.hs_fr270 =  972000.0 / scope.fr_halfstepsize;
		scope.hs_fr180 =  648000.0 / scope.fr_halfstepsize;
		scope.hs_fr90  =  324000.0 / scope.fr_halfstepsize;
		
		scope.fr_msecperhs = 86400000.0 / scope.hs_fr360;		
	}


	scope.northpole    = 0;
	scope.southpole    = 648000.;

	if(scope.latitude != 0){
		scope.northpole = scope.latitude * 3600.;
		scope.southpole = scope.northpole + 648000.;	
	}
			
		
	scope.hs_northpole = 0;
	scope.hs_southpole = 648000;
	scope.hs_latitude  = 0;
		
		
	if(scope.latitude != 0 ){
		scope.hs_latitude = scope.latitude * 3600.;
		if(scope.alt_halfstepsize > 0 ){
			scope.hs_latitude  = scope.hs_latitude / scope.alt_halfstepsize;
			scope.hs_northpole = scope.hs_latitude;
			scope.hs_southpole = 648000. / scope.alt_halfstepsize + scope.hs_northpole; 
		}
	}


	if(encoder.type != NOENCODER){
		
		if(encoder.azpulse360 > 0 ){
			encoder.azsighs = scope.hs_az360 / encoder.azpulse360;
		}

		if(encoder.altpulse90 > 0 ){
			encoder.altsighs = scope.hs_alt90 / encoder.altpulse90;
		}	
		
		encoder.active = 1;
		if(prog.debug){
			encoder.active = 0;
		}		
	}
	
	return(1);
}









int cfg_check_conditions( void )
{

	if(encoder.type != NOENCODER){
		if( (prog.iftype == IF_SERIAL) && (encoder.type != SERIALENCODER) ){
			fprintf(stderr,"For serial interface, we cannot use any other encoderinterface\n");
			return(0);
		}

		if( (prog.iftype == IF_PARALLEL) && (encoder.type == SERIALENCODER) ){
			fprintf(stderr,"For parallel interface, we can only use joystick, or parallel encoders.\n");
			return(0);
		}
	
	
	}
	
	return(1);
}





int cfg_read_configfile( char *filename )
{
	size_t len;
	double d;
	long   l;
	int    i;
	unsigned short us;
	char *fname;
	FILE *fp;
	char *s,*p;
	
	s = &il_tmptext[0];

	fname = filename;
	if( fname == NULL ){
		fname = &configfile[0];
	}

	sprintf(s,"./%s",fname);
	fp = fopen(s,"rt");
	
	if(fp == NULL){
		p = getenv("HOME");
		if(p != NULL){
			sprintf(s,"%s/.%s",p,fname);
			fp = fopen(s,"rt");
		}
	}
	
	if(fp == NULL){
		sprintf(s,"/etc/%s",fname);
		fp = fopen(s,"rt");
	}

	
	if( fp == NULL ) {
		cfg_alert("Cannot find, - or open configfile", fp);
		return( 0 );
	}

	rewind(fp);
	
	
	while ( ! feof( fp ) ) { 

		fgets( s , 252 , fp );
		strCleanStr( s, 254 );
		strEatBytes(0x20 , s ,254, 1);

		if( ( s[0] == ';' ) || ( s[0] == '#' ) ){
			continue;
		}

		p = strchr(s, '=' );
		if( p == NULL ){
			continue;
		}


		p[0] = 0;p++;



		if(strcmp(s,"INTERFACE" ) == 0){
			
			if(strcmp( p, "SERIAL") == 0 ){
				prog.iftype = IF_SERIAL;
				sersubtype = IF_SERIALSUBSERIAL;
				continue;
			}

			if(strcmp( p, "SERPARPROT") == 0 ){
				prog.iftype = IF_SERIAL;
				sersubtype = IF_SERIALSUBPARALLEL;
				continue;
			}

			if(strcmp( p, "PARALLELSMC") == 0 ){
				prog.iftype 		= IF_PARALLEL;
				parsubtype	 	= IF_PARALLELSUBSMC;	 
				continue;
			}

			if(strcmp( p, "PARALLELRAW") == 0 ){
				prog.iftype 		= IF_PARALLEL;
				parsubtype	 	= IF_PARALLELSUBRAW;	 
				continue;
			}

			
			cfg_alert("No valid interface defined for INTERFACE", fp);			
			return(0);
		}

		if(strcmp(s,"AZHALFSTEPSIZE" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for AZHALFSTEPSIZE", fp);			
				return(0);
			}
			scope.az_halfstepsize = d;
			continue;
		}

		if(strcmp(s,"ALTHALFSTEPSIZE" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for ALTHALFSTEPSIZE", fp);			
				return(0);
			}
			scope.alt_halfstepsize = d;
			continue;
		}

		if(strcmp(s,"FRHALFSTEPSIZE" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for FRHALFSTEPSIZE", fp);			
				return(0);
			}
			scope.fr_halfstepsize = d;
			continue;
		}


		if(strcmp(s,"GUIDEAZCORRECTIONHS" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for GUIDEAZCORRECTIONHS", fp);			
				return(0);
			}
			guide.az_correctionhs = d;
			continue;
		}

		if(strcmp(s,"GUIDEALTCORRECTIONHS" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for GUIDEALTCORRECTIONHS", fp);			
				return(0);
			}
			guide.alt_correctionhs = d;
			continue;
		}


		if(strcmp(s,"LATITUDE" ) == 0){
			
			d = strtod((const char *)p,NULL);
			scope.latitude = d;
			continue;
		}

		if(strcmp(s,"LONGITUDE" ) == 0){
			
			d = strtod((const char *)p,NULL);
			scope.longitude = d;
			continue;
		}

		if(strcmp(s,"ALTHORIZONTLIMIT" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for ALTHORIZONTLIMIT", fp);			
				return(0);
			}
			scope.alt_horizontlimit = d;
			continue;
		}

		if(strcmp(s,"ALTLZENITLIMIT" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for ALTZENITLIMIT", fp);			
				return(0);
			}
			scope.alt_zenitlimit = d;
			continue;
		}



		if(strcmp(s,"UTCADJUST" ) == 0){
			
			l = atol((const char *)p );
			scope.utcadjust = l;
			continue;
		}

		if(strcmp(s,"SERIALPORT" ) == 0){
			
			len = strlen(p);
			if( len < 3 ){
				cfg_alert("No valid portstring for SERIALSERIALPORT", fp);			
				return(0);
			}
			if( len >= 128 ){
				cfg_alert("portstring for SERIALSERIALPORT is too long", fp);			
				return(0);
			}
			
			strcpy( &serialport[0], p );
			continue;
		}

		if(strcmp(s,"JOYSTICKPORT" ) == 0){

			if( strTestPtrHex( p ) == 1 ){
				sscanf(p, "%x", &i );
			}
			else {
				i = atoi( p );
			}
			
			us = ( unsigned short ) i;
			
			if( us <= 0 ){
				cfg_alert("Invalid value for JOYSTICKPORT",fp);
				return(0);
			}
			
			setJoystickPort( us );
			continue;
		}

		if(strcmp(s,"PARALLELPORT" ) == 0){

			if( strTestPtrHex( p ) == 1 ){
				sscanf(p, "%x", &i );
			}
			else {
				i = atoi( p );
			}
			
			us = ( unsigned short ) i;
			
			if( us <= 0 ){
				cfg_alert("Invalid value for PARALLELPORT",fp);
				return(0);
			}
			
			setParallelPort( us );
			continue;
		}



		if(strcmp(s,"JOYSTICKKEYPAD" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				prog.keypad = KEYPADTYPE_JOYSTICK;
			}			
			continue;
		}

		if(strcmp(s,"PARALLELKEYPAD" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				prog.keypad = KEYPADTYPE_PARALLEL;
			}			
			continue;
		}



		if(strcmp(s,"TIMERINTCTGOTO" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for TIMERINTCTGOTO", fp);			
				return(0);
			}
			scope.timerintct_goto = d;
			continue;
		}


		if(strcmp(s,"TIMERINTCTCORRECTION" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for TIMERINTCTCORRECTION", fp);			
				return(0);
			}
			scope.timerintct_correction = d;
			continue;
		}
	

		if(strcmp(s,"MSTIMERINTCTGOTO" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for MSTIMERINTCTGOTO", fp);			
				return(0);
			}
			scope.mstimerintct_goto = d;
			continue;
		}


		if(strcmp(s,"MSTIMERINTCTCORRECTION" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for MSTIMERINTCTCORRECTION", fp);			
				return(0);
			}
			scope.mstimerintct_correction = d;
			continue;
		}
	

		if(strcmp(s,"SERIALAZCORRECTIONHSPERSEC" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for SERIALAZCORRECTIONHSPERSEC", fp);			
				return(0);
			}
			serflowazhspersecsslow = d;
			continue;
		}
	

		if(strcmp(s,"SERIALAZGOTOHSPERSEC" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for SERIALAZGOTOHSPERSEC", fp);			
				return(0);
			}
			serflowazhspersecsfast = d;
			continue;
		}
	

		if(strcmp(s,"SERIALALTCORRECTIONHSPERSEC" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for SERIALALTCORRECTIONHSPERSEC", fp);			
				return(0);
			}
			serflowalthspersecsslow = d;
			continue;
		}
	

		if(strcmp(s,"SERIALALTGOTOHSPERSEC" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for SERIALALTGOTOHSPERSEC", fp);			
				return(0);
			}
			serflowalthspersecsfast = d;
			continue;
		}
	


		if(strcmp(s,"AZGOTOSPEED" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for AZGOTOSPEED", fp);			
				return(0);
			}
			scope.az_gotospeed = d;
			continue;
		}

		if(strcmp(s,"ALTGOTOSPEED" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for ALTGOTOSPEED", fp);			
				return(0);
			}
			scope.alt_gotospeed = d;
			continue;
		}

		if(strcmp(s,"FRGOTOSPEED" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for FRGOTOSPEED", fp);			
				return(0);
			}
			scope.fr_gotospeed = d;
			continue;
		}

		if(strcmp(s,"AZCORRECTIONSPEED" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for AZCORRECTIONSPEED", fp);			
				return(0);
			}
			scope.az_correctionspeed = d;
			continue;
		}

		if(strcmp(s,"ALTCORRECTIONSPEED" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for ALTCORRECTIONSPEED", fp);			
				return(0);
			}
			scope.alt_correctionspeed = d;
			continue;
		}

		if(strcmp(s,"FRCORRECTIONSPEED" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for FRCORRECTIONSPEED", fp);			
				return(0);
			}
			scope.fr_correctionspeed = d;
			continue;
		}



		if(strcmp(s,"HSTEPMAXIMUMSPEED" ) == 0){
			
			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for HSTEPMAXIMUMSPEED", fp);			
				return(0);
			}
			scope.hstepmaximumspeed = d;
			continue;
		}


		if(strcmp(s,"TEMPERATURE" ) == 0){
			
			d = strtod((const char *)p,NULL);
			scope.temperature = d;
			continue;
		}

		if(strcmp(s,"AIRPRESSURE" ) == 0){
			
			d = strtod((const char *)p,NULL);
			scope.airpressure = d;
			continue;
		}

		if(strcmp(s,"CALCREFRACTION" ) == 0){
			
			if( ( strcmp( p, "OFF" ) == 0) || ( strcmp(p, "0") == 0 ) ) {
				scope.calcrefraction = 0;
			}			
			continue;
		}


		if(strcmp(s,"SERVERSERVICENAME" ) == 0){
			
			len = strlen(p);
			if( len < 3 ){
				cfg_alert("No valid value for SERVERSERVICENAME", fp);			
				return(0);
			}
			if( len >= 40 ){
				cfg_alert("string for SERVERSERVICENAME is too long", fp);			
				return(0);
			}
			
			strcpy( server.servicename, p );
			continue;
		}


		if(strcmp(s,"SERVERENVNAME" ) == 0){
			
			len = strlen(p);
			if( len < 3 ){
				cfg_alert("No valid value for SERVERENVNAME", fp);			
				return(0);
			}
			if( len >= 40 ){
				cfg_alert("string for SERVERENVNAME is too long", fp);			
				return(0);
			}
			
			strcpy( server.envname, p );
			continue;
		}


		if(strcmp(s,"SERVERPORT" ) == 0){
			
			i = atoi( p );
			if( i <= 300 ){
				cfg_alert("No valid value defined for SERVERPORT", fp);			
				return(0);
			}
			server.initport = i;
			continue;
		}


		if(strcmp(s,"SERVERTIMEOUTREAD" ) == 0){
			
			i = atoi( p );
			if( i <= 1 ){
				cfg_alert("No valid value defined for SERVERTIMEOUTREAD", fp);			
				return(0);
			}
			server.timeoutread = i;
			continue;
		}

		if(strcmp(s,"SERVERTIMEOUTWRITE" ) == 0){
			
			i = atoi( p );
			if( i <= 1 ){
				cfg_alert("No valid value defined for SERVERTIMEOUTWRITE", fp);			
				return(0);
			}
			server.timeoutwrite = i;
			continue;
		}

		if(strcmp(s,"SERVER" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				server.start = 1;
			}			
			continue;
		}

		if(strcmp(s,"SERVERTYPE" ) == 0){
			
			if( strcmp( p, "TCP" ) == 0){
				server.servertype = SERVERTYPETCP;
				continue;
			}			
			if( strcmp( p, "FIFO" ) == 0){
				server.servertype = SERVERTYPEFIFO;
				continue;
			}
			cfg_alert("No valid servertype set for SERVERTYPE, use TCP, or FIFO",fp);
			return(0);
		}

		if(strcmp(s,"SERVERFIFOIN" ) == 0){
			
			len = strlen(p);
			if( len < 3 ){
				cfg_alert("No valid value for SERVERFIFOIN", fp);			
				return(0);
			}
			if( len >= 126 ){
				cfg_alert("string for SERVERFIFOIN is too long", fp);			
				return(0);
			}
			
			strcpy( server.fifoinfile, p );
			continue;
		}

		if(strcmp(s,"SERVERFIFOOUT" ) == 0){
			
			len = strlen(p);
			if( len < 3 ){
				cfg_alert("No valid value for SERVERFIFOOUT", fp);			
				return(0);
			}
			if( len >= 126 ){
				cfg_alert("string for SERVERFIFOOUT is too long", fp);			
				return(0);
			}
			
			strcpy( server.fifooutfile, p );
			continue;
		}

		if(strcmp(s,"XEPHEMFIFOIN" ) == 0){
			
			len = strlen(p);
			if( len < 3 ){
				cfg_alert("No valid value for XEPHEMFIFOIN", fp);			
				return(0);
			}
			if( len >= 126 ){
				cfg_alert("string for XEPHEMFIFOIN is too long", fp);			
				return(0);
			}
			
			strcpy( xephem.fifoinfile, p );
			continue;
		}

		if(strcmp(s,"XEPHEMFIFOOUT" ) == 0){
			
			len = strlen(p);
			if( len < 3 ){
				cfg_alert("No valid value for XEPHEMFIFOOUT", fp);			
				return(0);
			}
			if( len >= 126 ){
				cfg_alert("string for XEPHEMFIFOOUT is too long", fp);			
				return(0);
			}
			
			strcpy( xephem.fifooutfile, p );
			continue;
		}

		if(strcmp(s,"XEPHEMFIFO" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				xephem.start = 1;
			}			
			continue;
		}

		if(strcmp(s,"XEPHEMKEYPADSPECIAL" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				xephem.keypadspecial = 1;
			}			
			continue;
		}

		if(strcmp(s,"XEPHEMFIFOOUTLOOP" ) == 0){
			
			i = atoi( p );
			if( i < 0 ){
				cfg_alert("No valid value defined for XEPHEMFIFOOUTLOOP", fp);			
				return(0);
			}
			xephem.fifooutloop = 0;
			xephem.fifooutloopsave = i;
			continue;
		}

		if(strcmp(s,"MONITOR" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				monitor.start = 1;
			}			
			continue;
		}


		if(strcmp(s,"MONITOROBJDBPATH" ) == 0){
			
			len = strlen(p);
			if( len < 1 ){
				cfg_alert("No valid value for MONITOROBJDBPATH", fp);			
				return(0);
			}
			if( len >= 126 ){
				cfg_alert("string for MONITOROBJDBPATH is too long", fp);			
				return(0);
			}
			
			strcpy( monitor.objdbpath, p );
			continue;
		}



		if(strcmp(s,"FRUSE" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				scope.fruse = 1;
			}			
			continue;
		}


		if(strcmp(s,"DEBUG" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				prog.debug = 1;
			}			
			continue;
		}


		if(strcmp(s,"EXFIFO" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				exfifo.start = 1;
			}			
			continue;
		}

		if(strcmp(s,"EXFIFOFILE" ) == 0){
			
			len = strlen(p);
			if( len < 1 ){
				cfg_alert("No valid value for EXFIFOFILE", fp);			
				return(0);
			}
			if( len >= 126 ){
				cfg_alert("string for EXFIFOFILE is too long", fp);			
				return(0);
			}
			
			strcpy( exfifo.fifoinfile, p );
			continue;
		}


		if(strcmp(s,"EXFIFOAZTSTEPS" ) == 0){

			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for EXFIFOAZTSTEPS", fp);			
				return(0);
			}
			exfifo.azsteps = d;
			continue;
		}


		if(strcmp(s,"EXFIFOALTSTEPS" ) == 0){

			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for EXFIFOALTSTEPS", fp);			
				return(0);
			}
			exfifo.altsteps = d;
			continue;
		}


		if(strcmp(s,"EXFIFOINVERT" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				exfifo.invert = 1;
			}			
			continue;
		}


		if(strcmp(s,"SERIALENCODER" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				encoder.type = SERIALENCODER;
			}			
			continue;
		}

		if(strcmp(s,"PARALLELENCODER" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				encoder.type = PARALLELENCODER;
			}			
			continue;
		}

		if(strcmp(s,"JOYSTICKENCODER" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				encoder.type = JOYSTICKENCODER;
			}			
			continue;
		}


		if(strcmp(s,"ENCODERPORT" ) == 0){

			if( strTestPtrHex( p ) == 1 ){
				sscanf(p, "%x", &i );
			}
			else {
				i = atoi( p );
			}
			
			us = ( unsigned short ) i;
			
			if( us <= 0 ){
				cfg_alert("Invalid value for ENCODERPORT",fp);
				return(0);
			}
			
			if(encoder.type == PARALLELENCODER){
				setParallelPort2( us );
				continue;
			}

			if(encoder.type == JOYSTICKENCODER){
				setJoystickPort2( us );
				continue;
			}

			cfg_alert("Using of ENCODERPORT, without previous setting of type",fp);			
			return(0);
		}


		if(strcmp(s,"ENCODERAZSIGNALS360" ) == 0){

			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for ENCODERAZSIGNALS360", fp);			
				return(0);
			}
			encoder.azpulse360 = d;
			continue;
		}

		if(strcmp(s,"ENCODERALTSIGNALS90" ) == 0){

			d = strtod((const char *)p,NULL);
			if( d <= 0.0 ){
				cfg_alert("No valid value defined for ENCODERALTSIGNALS90", fp);			
				return(0);
			}
			encoder.altpulse90 = d;
			continue;
		}

		if(strcmp(s,"ENCODERAZTOLERANCE" ) == 0){

			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for ENCODERAZTOLERANCE", fp);			
				return(0);
			}
			encoder.aztolerance = d;
			continue;
		}

		if(strcmp(s,"ENCODERALTTOLERANCE" ) == 0){

			d = strtod((const char *)p,NULL);
			if( d < 0.0 ){
				cfg_alert("No valid value defined for ENCODERALTTOLERANCE", fp);			
				return(0);
			}
			encoder.alttolerance = d;
			continue;
		}

		if(strcmp(s,"SKIPSTARTUPMOTOR" ) == 0){
			
			if( ( strcmp( p, "ON" ) == 0) || ( strcmp(p, "1") == 0 ) ) {
				prog.skipstartupmotor = 1;
			}			
			continue;
		}



	}

	fclose( fp );

	/*reinitialisation of scopevalues*/
	cfg_init_scopevalues( );
	return( 1 );
}