/* ******************************************************************** */
/*                                                                      */
/*                              initall()                               */
/*                              =========                               */
/*                                                                      */
/*    establish the message queue and declare all the global variables  */
/*    and initialize them         				        */
/*                                                                      */
/* ******************************************************************** */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>  
#include <sys/ipc.h>
#include <sys/msg.h>

#include "apss.h"
#include "global.h"

#include <cstring> 


     typedef struct {
       double  re ;
       double  im ;
     } complex ;

void initall(void)
{


        key_t   msq_key1, msq_key2 ;
	int	i, j; 
	double  x ;
	char    buf[120] ;

	char ST_start[] = "# of APSSs with model #";
	char ST_inpl[] = "<-OK";
	char cr;
	char ST_1[20],ST_2[20],ST_3[20],ST_4[20],ST_5[20],ST_6[20],ST_7[20],ST_8[20] ;
	int controls, inum, ibus ;
	double dfsample,dfcalculate;
        long int lifile;

	FILE     *fp, *fp3 , *fp_debug;
	//complex x1, x2, x3, t1, t2, t3 ;

	//complex   CADD(complex, complex) ;
	//complex   CMULT(complex, complex) ;



                      /* mtype identifies the desired data from the message */
                      /* queue. it's value equals to the bus number the     */
                      /* APSS is connected                                  */


	// open the "debug"  file to obtaine system informations for the control algoithms: 
	if((fp_debug = fopen("debug", "r+"))== NULL){
	  printf(" \n\n Cannit open file >>debug<< !\n");
	  exit(1);
	}
	else
	  {
	    // Search the "debug" file for the relevent information
	    
	    while(!feof(fp_debug)){
	      fgets(buf, 120, fp_debug) ;
	      //printf("\n comparissomn = %d \n",strncmp(buf,ST1,23));
	      // fscanf(fp_debug,"%s",buf);
	      if(strncmp(buf,ST_start,23)==0){ 
		sscanf(buf, "%s %s %s %s %s %s %s %d",ST_1,ST_2,ST_3,ST_4,ST_5,ST_6,ST_7,&controls);
		printf("\n << APSS >> %s %s %s %s %s %s %s <%d> \n",ST_1,ST_2,ST_3,ST_4,ST_5,ST_6,ST_7,controls);
		//printf("\n WW ->"),puts(buf);
		fgets(buf, 120, fp_debug);//, puts(buf);
		fgets(buf, 120, fp_debug);//, puts(buf);
		fgets(buf, 120, fp_debug);//, puts(buf);
		
		for(j=1;j<=controls;j++){   // Search the APSS table for the next control setup:
		                  
		  lifile = ftell(fp_debug);

		  fgets(buf, 80, fp_debug);
		  
		  strcpy(ST_1,ST_2);
		  sscanf(buf,"%d %d %lf %lf %s",&inum,&ibus,&dfsample,&dfcalculate,ST_1);
		  printf("Test: \t %d\t%d\t%lf\t%lf ST_1:\t%s\n",inum,ibus,dfsample,dfcalculate,ST_1);

		  if(strcmp(ST_inpl,ST_1) !=0 ) { 
		    fseek(fp_debug, lifile,SEEK_SET);
		    
		    fprintf(fp_debug,"  %d   \t%d \t%lf \t%lf \t\t<-OK    ",inum,ibus,dfsample,dfcalculate);
		    printf("Scaned:  %d\t%d\t%lf\t%lf\t%s",inum,ibus,dfsample,dfcalculate,ST_1);
		    j = controls+1;
		  }
		   
		  printf("\n");
		}
	      } 
	      
	      //fputs(buf,  fp_debug) ;
	    } // end of while loop

	    fclose(fp_debug) ;
	  }

   

	msgout.mtype =ibus ;
       	msgin.mtype = msgout.mtype ;
	msgout.dataout.Upss = 0.0 ;

	T_sample = dfsample;			/* sampling time interval */


 









	//fp3 = fopen("TpssData_5", "r") ;

 			/* establish the msg queues */
	fp = fopen("msq_key", "r") ;
	fgets(buf, 80, fp) ;
	sscanf(buf, "%d %d", &msq_key1, &msq_key2) ;
	fclose( fp ) ;
	
        if ((queueid1 = msgget( msq_key1, 0 )) == -1) {
            	printf("\nError: in 'APSS: main()'\n");
                printf("\tCannot find queue for msq_key1 = %ld.\n", msq_key1);
                exit( 1 );
	}
        if ((queueid2 = msgget( msq_key2, 0 )) == -1) {
               	printf("\nError: in 'APSS: main()'\n");
                printf("\tCannot find queue for msq_key2 = %ld.\n", msq_key2);
                exit( 1 );
	}
                      /* mtype identifies the desired data from the message */
                      /* queue. it's value equals to the bus number the     */
                      /* APSS is connected                                  */
	//fgets(buf, 80, fp3) ;
	//sscanf(buf, "%d %*s", &msgout.mtype) ;
	//msgin.mtype = msgout.mtype ;
	//msgout.dataout.Upss = 0.0 ;

					/* sampling time interval */
	//fgets(buf, 80, fp3) ;
	//sscanf(buf, "%lf %*s", &T_sample) ;
 

	//fclose( fp3 ) ;
}



#if 0

complex CADD( complex x1, complex x2 )
{
  complex  x3 ;

  x3.re = x1.re + x2.re ;
  x3.im = x1.im + x2.im ;

  return x3 ;
}

complex CMULT( complex x1, complex x2 )
{
  complex x3 ;

  x3.re = x1.re * x2.re - x1.im * x2.im ;
  x3.im = x1.re * x2.im + x1.im * x2.re ;

  return x3 ;
}

#endif









