/***************************************************************************
 * revive.c			Fred Brunet			8/27/91    										*
 * Purpose: Reactivate dead, captured, or retired pilots or crews from     *
 *          the game Secret Weapons of the Luftwaffe.(by Lucafilm Games)   *
 * Method:  Read in binary file, test status byte, and if byte has 	      *
 *	    certain values, set to zero and write altered file.		            *
 * NOTE:    The pilot status byte was determined by "reverse engineering". *
 *	    I do not work for Lucasfilm Games nor am I associated with          *
 *          the company in any way.  Using this program or a variation 	   *
 *          of it to alter the pilot files in any way may have subtle or   *
 *    	    unknown effects.  You may alter this file as you wish for your*
 *          own use but I am not responsible for any damage caused to      *
 *          any files.	If you copy or reproduce any code in this file I   *
 *          ask that you distribute it without charge.			   			*
 ***************************************************************************/


#include <stdio.h>
#include <dir.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
	int i,done,foundcount;
	char fname[20];

	struct ffblk find; /* dos file control block structure */

	if (argc==1)  { /* there were no parameters typed */
		printf("\nREVIVE v1.1: reactivate dead, captured or retired pilots.");
		printf("-- Fred Brunet 8/27/91\n\n");

		printf("usage: revive fname.usa or fname.ger, dos wildcards accepted.\n");
		exit(0);
	}
	strncpy(fname,argv[1],12);
	for(i=0; fname[i] && i<12; i++)
		fname[i]=toupper(fname[i]);

	if (strstr(fname,".USA") || strstr(fname,".GER")) /* for safety,only alter pilot files */
	{
		i = 0; 				/* number of files processed */
		foundcount=0;			/* number of files altered   */
		done = findfirst(fname,&find,0);
		if (done==0) {
			if (strstr(fname,"*.") || strstr(fname,"?")) { /* handle wildcards */
				do {
					i++;
					printf("%s:",&find.ff_name);
					if(undead(&find.ff_name))      /* call function to revive */
						foundcount++;
					done=findnext(&find);
				} while (done == 0);
				printf("\n%d Files processed, %d updated.\n",i,foundcount);
			}
			else {			 /* no wildcards, process one name only */
				printf("\n%s:",&find.ff_name);
				undead(&find.ff_name);
			}
		}
		else printf("\n\nFile not found in current directory.\n");
	}
	else
	  printf("\Can only revive .USA or .GER files-please type entire filename.\n");
}

int undead(char *fname)
/* clears active status bit in SWOTL file */
/* Return value- 0: no file written    1: file written  */
{
	#define ITEMSIZE 1
	#define ACTIVE_STATUS 2 /* file offset of status byte */
	#define FSIZE 540  /* size of pilot file */
	FILE *in,*out;
	char *buff;
	int status=-1;
	int retstatus=0;

	buff=malloc(FSIZE*sizeof(char));  /* holds entire file */

	if ((in = fopen(fname,"rb"))==NULL) /* open binary file for update */
	{
		printf("Cannot open input file.\n");
		retstatus=0;
		return retstatus;
	}
	fread(buff,ITEMSIZE,FSIZE,in);   /* read file into buff */
	fclose(in);

	switch(status=buff[ACTIVE_STATUS]) {
		case 0:
			printf("Pilot already active!\n");
			break;
		case 1:
			printf("Pilot is retired. \n");
			break;
		case 2:
			printf("Pilot was captured. \n");
			break;
		case 3:
			printf("Pilot was killed\n");
			break;
		default:
			printf("Unknown value in pilot status byte\n");
	} /* end of switch */


	if(status>0 && status <4)/* only writes if valid or necessary  */
	{
		if ((out = fopen(fname,"wb"))==NULL) /* open binary file for write */
		{
			printf("Cannot open output file.\n");
			retstatus=0;
			return retstatus;
		}
		buff[ACTIVE_STATUS]=0x00;  /* reset status to 0==active */
		fwrite(buff,ITEMSIZE,FSIZE,out);
		fclose(out);
		retstatus=1;
		printf("Pilot status updated.\n");
	}
	return retstatus;
}
