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

char infile[40];	/* input file name */
FILE *ipnt;		/* input file descriptor */

struct tm *localtime();
struct tm *time_ptr;

void put8( unsigned char, FILE * );
void put16( unsigned short, FILE * );
void put32( unsigned long, FILE * );
unsigned char get8( FILE * );
unsigned short get16( FILE * );
unsigned long get32( FILE * );

int main( int argc, char *argv[] )
{
	int i;
	int ibird,itype,idata;
	long oldtim;
	long bird_time;
	int weight;
	int box;
	unsigned long prog_id;

	if (argc<2)
	{
		while(1)
		{
			printf( "File name? " );
			fgets( infile, 20, stdin );
			if ( strlen( infile ) == 0 ) continue;
			break;
		}
	}
	else
	{
		strcpy(infile,argv[1]);
	}
	i = strlen( infile );
	if ( infile[i-1] == '\n' ) infile[i-1] = '\0';

	ipnt = fopen(infile,"rb");
	if(ipnt == NULL)
	{
		printf( "Cannot open %s\n", infile );
		exit( 1 );
	}
	oldtim = 0;

	ibird = get16( ipnt );
	bird_time = get32( ipnt );
	weight = get16( ipnt );
	box = get16( ipnt );
	prog_id = get32( ipnt );

	putenv( "TZ=EST0" );
	tzset();
	time_ptr = localtime(&bird_time);

	printf( "Bird #%d, Date: %d/%d/%d %d:%d:%d\n",
		ibird,
		time_ptr->tm_mon+1,
		time_ptr->tm_mday,
		time_ptr->tm_year,
		time_ptr->tm_hour,
		time_ptr->tm_min,
		time_ptr->tm_sec );
	printf( "Weight = %d, Box = %d, ID = %ld\n", weight, box, prog_id );

	while( feof( ipnt ) == 0)
	{
		itype = get8( ipnt );
		idata = get8( ipnt );
		bird_time = get32( ipnt );

		if( itype == 7 )
		{
			if (idata == 0)
			{
			    printf( "%3d      %ld\n", itype, bird_time );
			}
			else
			{
			    printf( "%3d %3d  %ld\n", itype, idata, bird_time );
			}
		}
		else
		{
		    printf( "%3d %3d  %ld,%ld\n",itype,idata,
					bird_time,bird_time-oldtim);
		    oldtim = bird_time;
		    if ( itype == 5 ) break;
		}
	}

	fclose( ipnt );
	return( 1 );
}
