/*++
*******************************************************************************
* Copyright (c) 1990-98 Digi International Inc., All Rights Reserved				*
* 																										*
* This software contains proprietary and confidential information of Digi		*
* International Inc.  By accepting transfer of this copy, Recipient agrees		*
* to retain this software in confidence, to prevent disclosure to others,		*
* and to make no use of this software other than that for which it was			*
* delivered.  This is an unpublished copyrighted work of Digi International	*
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly	*
* prohibited.																						*
* 																										*
* Restricted Rights Legend																		*
*																										*
* Use, duplication, or disclosure by the Government is subject to					*
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in			*
* Technical Data and Computer Software clause at DFARS 252.227-7031 or			*
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -				*
* Restricted Rights at 48 CFR 52.227-19, as applicable.								*
*																										*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343				*
*																										*
*******************************************************************************

Module Name:

   autoinc.c

Abstract:

   This app is used to auto-increment a build number which can be compiled into any 
   driver/application.

	It takes a single filename, parses the file, rewrites it to the same filename, 
	updating the buildnumber - incremented by one.

	It optionally takes a buildnumber override.  If specified, this value will be
	written instead.

	It expects a C-style header file with a single line as follows:

		#define BuildNumber x

	where x is an unsigned integer in decimal.

Author:

   Michael Zarns

--*/

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

// Indices to CommandLine parameters
enum
{
	ParamFileName = 1,		// Filename is a required param
	ParamBuildNumber			// BuildNumber is optional and overrides increment.
};


// ErrorLevel returned from main.  Use in cmd files to verify successful operation
typedef enum ERROR_STATUS
{
	ErrorSuccess = 0,
	ErrorUnknown,
	ErrorGetTempFileName,
	ErrorFileOpen,
	ErrorParseFile,
	ErrorMoveFile,
} ERROR_STATUS;


#define PrintUsage() \
   printf( "usage:\n\n" \
           "   autoinc <filename> [<buildnumber>]\n\n" \
           "where <filename> is a file containing the number to increment, and\n" \
           "<buildnumber> is an optional override of the build number written.\n" );




int __cdecl main(int argc, char *argv[])
{
	ERROR_STATUS Status = ErrorUnknown;

	char szTmpFileName[MAX_PATH];

	FILE * fpTmp = NULL;
	FILE * fpInc = NULL;

	if ( argc == 1 || argc > 3 )
	{
		PrintUsage();
		goto Exit;
	}

	if (GetTempFileName(".", "inc", 0, szTmpFileName) == 0)
	{
		printf( "GetTempFileName failed.  GetLastError = %d\n", GetLastError() );
		Status = ErrorGetTempFileName;
		goto Exit;
	}

	fpTmp = fopen(szTmpFileName, "w");
	if (fpTmp == NULL)
	{
		printf("fopen failed.  Couldn't open %s, GetLastError = %d\n", szTmpFileName, GetLastError());
		Status = ErrorFileOpen;
		goto Exit;
	}

	fpInc = fopen(argv[ParamFileName], "r");
	if (fpInc == NULL)
	{
		printf("fopen failed.  Couldn't open %s, GetLastError = %d\n", argv[ParamFileName], GetLastError());
		Status = ErrorFileOpen;
		goto Exit;
	}

	{
		BOOL fFoundBuildNumber = FALSE;

		static char const * const pszMacroBuildNumber = "#define DXBVER_BUILDNUMBER";
		static char const * const pszMacroBuildDate   = "#define DXBVER_BUILDDATE";
		char sz[1024];

		while (fgets(sz, sizeof(sz), fpInc) != NULL)
		{
			if (strncmp(sz, pszMacroBuildNumber, strlen(pszMacroBuildNumber)) == 0)
			{
				int BuildNumber = atoi(&sz[strlen(pszMacroBuildNumber)]);
				printf("Old BuildNumber is %d\n", BuildNumber);

				// Override buildnumber, if specified on command line
				if ( argc > ParamBuildNumber )
					BuildNumber = atoi(argv[ParamBuildNumber]);
				else
					BuildNumber++;

				printf( "New BuildNumber is %d\n", BuildNumber );
				sprintf(sz, "%s %d\n", pszMacroBuildNumber, BuildNumber);

				fFoundBuildNumber = TRUE;
			}
			else if (strncmp(sz, pszMacroBuildDate, strlen(pszMacroBuildDate)) == 0)
			{
				time_t Time;
				char szDate[64];
				time(&Time);
				strftime( szDate, sizeof szDate, "%m/%d/%Y", localtime(&Time) );
				printf( "New date is %s\n", szDate );
				sprintf( sz, "%s %s\n", pszMacroBuildDate, szDate );
			}

			fputs(sz, fpTmp);
		}

		if (!fFoundBuildNumber)
		{
			printf( "Error parsing file!  BuildNumber not found.\n" );
			Status = ErrorParseFile;
			goto Exit;
		}

		fclose(fpTmp);
		fpTmp = NULL;

		fclose(fpInc);
		fpInc = NULL;

		if (!MoveFileEx(szTmpFileName, argv[ParamFileName], MOVEFILE_REPLACE_EXISTING))
		{
			printf( "MoveFileEx failed.  GetLastError = %d\n", GetLastError() );
			Status = ErrorMoveFile;
		}

		Status = ErrorSuccess;
	}

Exit:
	if (fpTmp != NULL)
	{
		fclose(fpTmp);
		remove(szTmpFileName);
	}

	if (fpInc != NULL)
		fclose(fpInc);

	return Status;
}
