/*
   hsf2RC

   Copyright (C) 2002 Loïc YHUEL (hwti)

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
*/

//#define WIN32_LEAN_AND_MEAN		// Exclure les en-têtes Windows rarement utilisés
#include <stdio.h>
#include <io.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

void WriteInfo(char *str1,char *str2);

FILE *os,*doorsos,*alias;
bool MakeDB92INFO;
char *(*table)[2000];
int Highest=-1;


int main(int argc, char *argv[])
{
	_finddata_t data;
	long file, folder;
 	FILE *f1,*db92;
	char dummy[100],dummy2[100],path[50];
	char * s,* c;
	int b;

	puts("Builds os.h, doorsos.h [and db92 rom_call info] from TIGCC sources\nhsf2RC [/db92]");
	if ((argc > 1) && (!(stricmp("/db92",argv[1])))) {
		MakeDB92INFO = true;
		if (!(table = (char*(*)[2000])calloc(2000,sizeof(char *)))) {
			puts("Out of memory");
			return 1;
		}
		db92 = fopen("Romcalls.bin","wt");
	}
	else MakeDB92INFO = false;
	
	folder = _findfirst("*.h",&data);
	if (folder == -1) {
		puts("No directory found. Run hsf2rc in doc\\system\\include directory of TIGCC sources.");
		return 1;
	}
	
	os = fopen("os.h","wt");
	doorsos = fopen("doorsos.h","wt");
	alias = fopen("db92_alias","a+t");
	
	do {
		if (!(data.attrib && _A_SUBDIR)) continue;
		strcpy(path,data.name);
		strcat(path,"\\");
		c = strchr(path,0);
		strcpy(c,"*.hsf");
		file = _findfirst(path,&data);
		if (file == -1) continue;
		do {
			strcpy(c,data.name);
			f1 = fopen(path, "rt");
			do {
				if (!fgets(dummy,100,f1)) goto nextfile;
			} while (strncmp("Name=",dummy,5));
			do {
				if (!fgets(dummy2,100,f1)) goto nextfile;
			} while (strcmp("[ROM Call]\n",dummy2));
			fgets(dummy2,100,f1);
			*strchr(dummy+5,'\n') = 0;
			WriteInfo(dummy+5,dummy2);
	nextfile :
			fclose(f1);
		} while (!_findnext(file,&data));
		_findclose(file);
	} while (!_findnext(folder,&data));
	_findclose(folder);
	
	if (f1 = fopen("notdefinedrom_calls","rt")) {
		while (fgets(dummy,100,f1)) {
			if (dummy[0] == ';') continue;
			*(s = strchr(dummy,' ')) = 0;
			WriteInfo(dummy,s+1);
		}
		fclose(f1);
	}
	
	if (f1 = fopen("os.h.end","rt")) {
		while (fgets(dummy,100,f1)) fputs(dummy,os);
		fclose(f1);
	}

	if (f1 = fopen("doorsos.h.end","rt")) {
		while (fgets(dummy,100,f1)) fputs(dummy,doorsos);
		fclose(f1);
	}

	fclose(os);
	fclose(doorsos);
	
	if (MakeDB92INFO) {
		for (b = 0;b <= Highest;b++) {
			if (!((*table)[b] == 0)) {
				fputs((*table)[b],db92);
				free((*table)[b]);
			}
			else printf("Warning : ROM_CALL %i not defined\n",b);
			fputc(0,db92);
		}
		f1 = fopen("romcalls.h","w");
		fprintf(f1,";Include file automatically generated by hsf2RC.\n"
		";Copyright (C) 2002 Loïc YHUEL (hwti)\n"
		"RCNum equ %i\n"
		"ROM_CALLs_Names\n"
		" incbin romcalls.bin",b-1);
		fclose(f1);
		free(table);
		fclose(db92);
	}
	fclose(alias);
	return 0;
}

void WriteInfo(char *str1,char *str2)
{
	int a,b;
	char dummy[100];

	str2 = strchr(str2,'$');
	fprintf(os,"%s\t\tequ\t%s",str1,str2);
	fprintf(doorsos,"doorsos::%s\tequ\t_ROM_CALL_%s",str1,str2 + 1);
	if (MakeDB92INFO) {
		str2 = strchr(str2,0) - 1;
		if (*str2 == '\n') str2--;
		a = 1;
		b = 0;
		while (1) {
			if ((*str2 >= '0') && (*str2 <= '9')) b += a * (*str2 - '0');
			else if ((*str2 >= 'A') && (*str2 <= 'F')) b += a * (*str2 - 'A' + 10);
			else break;
			a *= 16;
			str2--;
		}
			
		if (!((*table)[b] == NULL)) {
			if (alias) {
				rewind(alias);
				while (fgets(dummy,100,alias)) {
					if ((atoi(dummy) == b)) {
						if (*(strchr(dummy,' ')+1) == 'n') return;
						else goto overw;
					}
				}
			}
			printf("ROM_CALL %i is already named %s. Name it %s ?",b,(*table)[b],str1);
			do {
				a = getchar();
				if (a == 'n') {
					if (alias) fprintf(alias,"%i n\n",b);
					return;
				}
			} while (!(a == 'y'));
			if (alias) fprintf(alias,"%i y\n",b);
			}
			
overw :
		if (!((*table)[b] = (char *)realloc((*table)[b],strlen(str1)+1))) {
			puts("Out of memory");
			exit(1);
		}

		strcpy((*table)[b],str1);
		if (b > Highest) Highest = b;
	}
}
