
/*
	Copyright 1993  Jeremy Slade.  All rights reserved.
*/

/*
	Project: Locus

	File: IncVersion.m

	Description:
		This is a short program that reads the version data contained in a NXStringTable format file, and generates an header file from this data, that is then imported by VersionInfo.m, defining the project version information at compile time.

	Original Author: Jeremy Slade

	Revision History:
		Created
			V.101	JGS	Fri Apr  3 23:53:16 GMT-0700 1992

*/


#import <libc.h>
#import <objc/NXStringTable.h>
#import <stdio.h>
#import <streams/streams.h>

#define INPUT_FILE		"VersionInfo.data"
#define OUTPUT_FILE	"VersionInfo.data.h"


void main ( int argc, char *argv[] )
{
	id table;
	NXStream *stream;
	int ReleaseType, MajorVersion, MinorVersion, Revision, VersionNum;
	char buf[40];
	unsigned t;
	
	// Create an NXStringTable from the input file
	if ( !(table = [[NXStringTable alloc] init])
			|| !( [table readFromFile:INPUT_FILE]) ) {
		printf ( "IncVersion: error: unable to read input file %s\n", INPUT_FILE );
		exit ( -1 );
	}
	
	// Open a stream to create the output file
	if ( !(stream = NXOpenMemory ( NULL, 0, NX_WRITEONLY )) ) {
		printf ( "IncVersion: error: unable to create ouput stream\n" );
		exit ( -1 );
	}
	
	// Output compile time
	time ( &t );
	strcpy ( buf, ctime ( &t ) );
	buf[strlen(buf)-1] = '\0'; // Remove \n at end of buf
	NXPrintf ( stream, "\n#define V_CompileTime \"Compiled at: %s\"\n\n", buf );
	
	// Get version data from strings in table
	ReleaseType = atoi ( [table valueForStringKey:"V_ReleaseType"] );
	MajorVersion = atoi ( [table valueForStringKey:"V_MajorVersion"] );
	MinorVersion = atoi ( [table valueForStringKey:"V_MinorVersion"] );
	
	// Get Revision number, increment it, and write out the input file
	Revision = atoi ( [table valueForStringKey:"V_Revision"] );
	Revision++;
	sprintf ( buf, "%d", Revision );
	[table insertKey:"V_Revision" value:buf];
	if ( ![table writeToFile:INPUT_FILE] ) {
		printf ( "IncVersion: error: unable to save input file %s\n", INPUT_FILE );
		// Non-fatal error -- keep going
	}
	
	// Compute VersionNum -- add all values together
	VersionNum = Revision;
	VersionNum += MinorVersion * 100;
	VersionNum += MajorVersion * 1000;
	VersionNum += ReleaseType * 10000;
	
	// Output the rest of the data
	NXPrintf ( stream, "#define V_VersionNum %d\n", VersionNum );
	NXPrintf ( stream, "#define V_ReleaseType %d\n", ReleaseType );
	NXPrintf ( stream, "#define V_MajorVersion %d\n", MajorVersion );
	NXPrintf ( stream, "#define V_MinorVersion %d\n", MinorVersion );
	NXPrintf ( stream, "#define V_Revision %d\n", Revision );
	NXPrintf ( stream, "\n\n" );
	
	// Save output file
	if ( NXSaveToFile ( stream, OUTPUT_FILE ) != 0 ) {
		printf ( "IncVersion: error: unable to create output file %s\n", OUTPUT_FILE );
		exit ( -1 );
	}
	NXCloseMemory ( stream, NX_FREEBUFFER );

	exit ( 0 );
}

