/* Get C-Evo max year */

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

int isprime(int a) {
	int b;
	for(b=2;b * b <= a;b++) {
		if(a % b == 0) {
			return 0;
		}
	}
	return 1;
}

int main(int argc, char **argv) {
	FILE *cevo, *cevohack = 0;
	int a,b;
	int maxyear = 0;
	cevo = fopen("Cevo.exe","rb");
	if(argc != 1 && argc != 2) {
		printf("Usage: max.year [new max year]\n");
		exit(0);
	}
	if(argc == 2) {
		cevohack = fopen("Cevo-hack.exe","wb");
		maxyear = atoi(argv[1]);
		printf("Changing turn to %d\n",maxyear);
	}
	if(cevo == 0) {
		printf("Could not find Cevo.exe!\n");
		exit(1);
	}
	for(a=0;a<0x7a748;a++) {
		b = getc(cevo);	
		if(feof(cevo)) {
			printf("This is too small!\n");
			exit(2);
		}
		if(cevohack != 0) {
			putc(b,cevohack);
		}
	}
	b = getc(cevo);
	if(cevohack != 0) {
		putc(maxyear,cevohack);
	} else {
		printf("Current max time %d\n",b);
	}
	while(!feof(cevo)) {
		b = getc(cevo);
		if(cevohack != 0 && !feof(cevo)) {
			putc(b,cevohack);
		}
	}
	fclose(cevo);
	if(cevohack != 0) {
		fclose(cevohack);
	}
}
