#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <time.h>

/* read TEAC DR-M3 Data     */


#define LINE	2048
#define HDR	".HDR"
#define DAT	".DAT"
#define MAXCH	32

int main( int argc, char *argv[] ) {

	FILE *hdr, *dat;
	char datfile[LINE], datfile2[LINE], hdrfile[LINE], bname[LINE];
	char line[LINE], *delim=":- ,\r\n\t";
	char *lp, *key, *lp2;
	int month, day, year, hour, minute;
	int month1, day1, year1, hour1, minute1, second1;
	int month2, day2, year2, hour2, minute2, second2;
	float second, dt;
	float firsttime=0;
	float freq;
	int i,nch;
	double a[MAXCH], b[MAXCH], range[MAXCH];
	char din[LINE], ftype[LINE], dum[LINE];

	short data[MAXCH];
	double xdata[MAXCH];
	long int count, sample_data_count;

	time_t t;
	struct tm tstruct;

	if( argc < 2 ) {
		fprintf(stderr, "a.out filename (without .hdr or .dat)\n");
		exit( EXIT_FAILURE );
	}


	strcpy(hdrfile, argv[1]);  strcat(hdrfile, HDR);
	strcpy(datfile, argv[1]);  strcat(datfile, DAT);

	fprintf(stderr, "HDR file: %s \t DAT file: %s\n", hdrfile, datfile);


/* read headder file 	*/

	if( NULL == (hdr = fopen( hdrfile, "r" ))) {
		fprintf(stderr, "Can't open %s\n", hdrfile);
		exit( EXIT_FAILURE );
	}

	while( NULL != fgets( line, LINE-1, hdr ) ) {
		lp = line;
		key = strsep( &lp, delim );

		if( strcmp( key, "DATASET" ) == 0 ) {
				sscanf( lp, "%s", bname );
				strcpy(datfile2, bname);
				strcat(datfile2, DAT);
				fprintf(stderr, "DATA FILE : %s\n", datfile2 );
		}

		if( strcmp( key, "SERIES" ) == 0 ) {
			fprintf(stderr, " SERIES (just outputed!) = %s\n", lp);
		}

		if( strcmp( key, "DATE" ) == 0 ) {
			month = atoi( strsep( &lp, delim ) );
			day = atoi( strsep( &lp, delim ) );
			year = atoi( lp);
			fprintf(stderr, "DATE %d/%d/%d\n", year, month, day);
		}

		if( strcmp( key, "TIME" ) == 0 ) {
			if(firsttime == 0 ) {
				hour = atoi( strsep( &lp, delim ) );
				minute = atoi( strsep( &lp, delim ) );
				second = (float)atof( lp );
				fprintf(stderr, "time %d:%d:%f\n", 
						hour, minute, second);
			firsttime++;
			} else {
				sscanf( strsep( &lp, delim ),
					"%2d%2d%2d%2d%2d%2d",
					 &year1, &month1, &day1,
					 &hour1, &minute1, &second1 );
				sscanf( lp, 
					"%2d%2d%2d%2d%2d%2d",
					 &year2, &month2, &day2,
					 &hour2, &minute2, &second2 );
				fprintf(stderr, 
					"from %d/%d/%d %d:%d:%d "
					"to %d/%d/%d %d:%d:%d\n",
					 year1, month1, day1, 
					 hour1, minute1, second1, 
					 year2, month2, day2, 
					 hour2, minute2, second2);
			}
		}

		if( strcmp( key, "INTERVAL" ) == 0 ) {
			dt = atof(lp);
			fprintf(stderr, "INTERVAL %f\n", dt);
		}

		if( strcmp( key, "NUM_SERIES") == 0 ) {
			nch = atoi(lp);
			fprintf(stderr, "NUM_SERIES = %d\n", nch);
			if( nch > MAXCH ) {
				fprintf(stderr, "please change deine of MAXCH\n");
				exit( EXIT_FAILURE );
			}
		}

		if( strcmp( key, "FILE_TYPE" ) == 0 ) {
			sscanf( lp, "%s", ftype );
			if( strcmp( ftype, "INTEGER") != 0 ) {
			fprintf(stderr, 
				"FILE_TYPE = %s ( should be INTEGER!! )\n",
				lp);
			}
		}

		if( strcmp( key, "SLOPE" ) == 0 ) {
			for(i=0; i<nch; i++) {
				a[i] = atof( strsep( &lp, delim ) );
				fprintf(stderr, "slope %d +1 ch %lf\n", i, a[i]);
			}
		}

		if( strcmp( key, "Y_OFFSET" )  == 0 ) {
			for(i=0; i<nch; i++) {
				b[i] = atof( strsep( &lp, delim ) );
				fprintf(stderr, " y offset %f (%d +1 ch)\n", b[i], i);
				}
		}

		if( strcmp( key, "DIGITAL_IN" )  == 0 ) {
			sscanf( lp, "%s", din );
			if( strcmp( din, "OFF" ) != 0 ) {
				fprintf(stderr, "This soft can't read digital "
					"input. Data must be bad!!\n" );
			}
		}

		if( strcmp( key, "INPUT_RANGE" )  == 0 ) {
			for(i=0; i<nch; i++) {
				for(strcpy(dum, strsep( &lp, " ,V\r\n"));
					dum[0] == 0 ;
					strcpy(dum, strsep( &lp, " ,V\r\n") )) {
					;
				}
				range[i] = atof( dum );
			}

			for(i=0; i<nch; i++) {
				fprintf(stderr, "Range %d +1 ch = %lf\n",
					i, range[i] );
			}
		}

		if( strcmp( key, "SAMPLE_DATA_COUNT" )  == 0 ) {
			sscanf(lp, "%ld", &sample_data_count );
			fprintf(stderr, "SAMPLE_DATA_COUNT = %ld\n", 
				sample_data_count);
		}

		if( strcmp( key, "SAMPLE_FREQUENCY" )  == 0 ) {
			for(strcpy(dum, strsep( &lp, " ,V\r\n"));
				dum[0] == 0 ;
				strcpy(dum, strsep( &lp, " ,V\r\n") )) {
				;
			}
			freq = atof( dum );
		}
					
				
	}

	fclose(hdr);


/* output headder information into stdout */
	fprintf( stdout, "%4d/%2d/%2d %2d:%2d:%2.0f\n", 
		year, month, day, hour, minute, second);
	
		tstruct.tm_sec = second+0.5;	/* rounding */
		tstruct.tm_min = minute;
		tstruct.tm_hour = hour;
		tstruct.tm_mday = day;
		tstruct.tm_mon  = month -1 ;
		tstruct.tm_year =  year - 1900;
		tstruct.tm_isdst = 0;
		tstruct.tm_gmtoff = -9;

		t = mktime( &tstruct );

		fprintf( stdout, "%s", ctime( &t ) );
		fprintf( stdout, "CH: %d  DATA: %d  Freq: %f\n", 
			nch, sample_data_count, freq );




/* data file reading */
	if( NULL == (dat = fopen( datfile, "r" )) ) {
		fprintf(stderr, "Can't open %s\n", datfile);
		exit( EXIT_FAILURE );
	}

	count = 0;
	while( nch == fread( data, 2, nch, dat ) ) {
		for(i=0; i< nch; i++ ) {
			xdata[i] = a[i] * data[i] + b[i] ;
		}
		count++;

		for(i=0; i< nch; i++ ) {
			fprintf( stdout, " %e", xdata[i] );
		} fprintf( stdout, "\n");


	}

	fclose(dat);
			
	fprintf(stderr, "Output Data Count = %ld, expected = %ld\n",
		count, sample_data_count);
		
	
}


