/* nicolas.janey@univ-fcomte.fr */

#include <io.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

void substitutionSlash(char *s) {
  int n = strlen(s);
  for ( int i = 0 ; i < n ; i++ )
    if ( s[i] == '\\' )
      s[i] = '/';
}

char *pathSplit(char *path) {
  static char p[250];
  for ( int i = strlen(path)-1 ; ( i >= 0 ) && ( path[i] != '\\' ) ; i-- ) ;
  strcpy(p,path);
  p[i+1] = 0x00;
  return(p);
}

void compile(char *name,FILE *all) {
  if ( strcmp(name,".") && strcmp(name,"..") ) {
    FILE *f = fopen("compile.bat","wt");
    fprintf(f,"cd %s\n",name);
    fprintf(f,"nmake /a /f \"%s.mak\" CFG=\"%s - Win32 Release\"\n",name,name);
    fprintf(f,"cd ..\n");
    fclose(f);

    fprintf(all,"cd %s\n",name);
    fprintf(all,"nmake /a /f \"%s.mak\" CFG=\"%s - Win32 Release\"\n",name,name);
    fprintf(all,"cd ..\n\n");
     }
}

char *lastIndexOf(char *s,char c) {
  int n = strlen(s);
  int i = n-1;
  while ( s[i] != c )
    i--;
  return(&s[i+1]);
}

char *lastIndexOf(char *s,char *c) {
  int ns = strlen(s);
  int nc = strlen(c);
  int p = ns-nc-1;
  int stop = 0;
  do {
    stop = 1;
    for ( int i = 0 ; i < nc ; i++ )
      if ( s[p+i] != c[i] )
        stop = 0;
    p--; }
  while ( ( !stop ) && ( p >= 0 ) );
  return(( p < 0 ) ? NULL : &s[p+nc+2]);
}

void makefile(char *name,FILE *all) {
  if ( strcmp(name,".") && strcmp(name,"..") ) {
    char make[200];
    sprintf(make,"%s\\%s.mak",name,name);
    fprintf(all,"echo %s\n",name);
    FILE *file = fopen(make,"rt");
    char s[1000];
    int exe = 0;
    int lib = 0;
    int libs = 0;
    int cpt = 0;
    int link = 0;
    char executable[1000];
    char ligne[5000] = "";
    char librairies[5000] = "";
    printf("\n");
    while ( !feof(file) ) {
      fscanf(file,"%s",s);
      substitutionSlash(s);
      int n = strlen(s);
      if ( !strcmp(s,"LINK32_OBJS=") ) {
        link = 1;
        printf("%s\n",s); }
      { if ( ( link ) && ( n >= 5 ) ) {
          if ( ( s[n-2] == 'b' ) && ( s[n-3] == 'i' ) && ( s[n-4] == 'l' ) && ( s[n-5] == '.' ) ) {
            char *cpp = lastIndexOf(s,'/');
            s[n-1] = 0x00;
            s[n-5] = 0x00;
            printf("%s\n",cpp);
            libs = 1;
            strcat(librairies,"-l");
            strcat(librairies,cpp);
            strcat(librairies," "); } } }
      { if ( n >= 2 ) {
          if ( ( s[n-1] == 'c' ) && ( s[n-2] == '.' ) ) {
            char *cpp = lastIndexOf(s,'/');
            char *source = lastIndexOf(s,"SourcesMesa");
            if ( !source )
              source = lastIndexOf(s,"Sources");
            printf("%s\n",source);
            cpt++;
            fprintf(all,"g++ %s -c",source);
            s[n-2] = 0x00;
            cpp = lastIndexOf(s,'/');
            fprintf(all," -o Temp/%s.o\n",cpp);
            strcat(ligne,"Temp/");
            strcat(ligne,cpp);
            strcat(ligne,".o "); } } }
      { if ( n >= 3 ) {
          if ( ( s[n-2] == 'c' ) && ( s[n-3] == '.' ) ) {
            char *cpp = lastIndexOf(s,'/');
            char *source = lastIndexOf(s,"SourcesMesa");
            if ( !source )
              source = lastIndexOf(s,"Sources");
            cpt++;
            s[n-1] = 0x00;
            printf("%s\n",source);
            fprintf(all,"g++ %s -c",source);
            s[n-3] = 0x00;
            cpp = lastIndexOf(s,'/');
            fprintf(all," -o Temp/%s.o\n",cpp);
            strcat(ligne,"Temp/");
            strcat(ligne,cpp);
            strcat(ligne,".o "); } } }
      { if ( n >= 4 ) {
          if ( ( s[n-1] == 'p' ) && ( s[n-2] == 'p' ) && ( s[n-3] == 'c' ) && ( s[n-4] == '.' ) ) {
            char *cpp = lastIndexOf(s,'/');
            char *source = lastIndexOf(s,"SourcesMesa");
            if ( !source )
              source = lastIndexOf(s,"Sources");
            printf("%s\n",source);
            cpt++;
            fprintf(all,"g++ %s -c",source);
            s[n-4] = 0x00;
            cpp = lastIndexOf(s,'/');
            fprintf(all," -o Temp/%s.o\n",cpp);
            strcat(ligne,"Temp/");
            strcat(ligne,cpp);
            strcat(ligne,".o "); } } }
      { if ( n >= 5 ) {
          if ( ( s[n-2] == 'p' ) && ( s[n-3] == 'p' ) && ( s[n-4] == 'c' ) && ( s[n-5] == '.' ) ) {
            char *cpp = lastIndexOf(s,'/');
            char *source = lastIndexOf(s,"SourcesMesa");
            if ( !source )
              source = lastIndexOf(s,"Sources");
            cpt++;
            s[n-1] = 0x00;
            printf("%s\n",source);
            fprintf(all,"g++ %s -c",source);
            s[n-5] = 0x00;
            cpp = lastIndexOf(s,'/');
            fprintf(all," -o Temp/%s.o\n",cpp);
            strcat(ligne,"Temp/");
            strcat(ligne,cpp);
            strcat(ligne,".o "); } } }
      { if ( ( n >= 5 ) && ( !exe ) && ( !lib ) )
          if ( ( s[n-1] == '"' ) && ( s[n-2] == 'e' ) && ( s[n-3] == 'x' ) && ( s[n-4] == 'e' ) && ( s[n-5] == '.' ) ) {
            s[n-5] = 0x00;
            strcpy(executable,lastIndexOf(s,'/'));
            exe = 1; }
        if ( ( n >= 5 ) && ( !lib ) && ( !exe ) )
          if ( ( s[n-1] == '"' ) && ( s[n-2] == 'b' ) && ( s[n-3] == 'i' ) && ( s[n-4] == 'l' ) && ( s[n-5] == '.' ) ) {
            s[n-5] = 0x00;
            strcpy(executable,lastIndexOf(s,'/'));
            lib = 1; } }
      }
    if ( ( exe ) && ( cpt != 0 ) ) {
      if ( libs )
        fprintf(all,"g++ %s-lm -lglut -lGL -lGLU -o Binaires/%s -L./Librairies %s\n\n",ligne,executable,librairies);
        else
        fprintf(all,"g++ %s-lm -lglut -lGL -lGLU -o Binaires/%s\n\n",ligne,executable); }
    if ( ( lib ) && ( cpt != 0 ) )
      fprintf(all,"ar cr Librairies/lib%s.a %s\n\n",executable,ligne);
    fclose(file); }
}

struct info {
  char name[250];
  int size; };

int main(int argc, char* argv[]) {
  struct _finddata_t fileinfo ;
  int nf = 0;
  
  long hld = _findfirst(argv[1],&fileinfo) ;
  if ( hld != -1 )
    if ( fileinfo.attrib == _A_SUBDIR )
      nf++;
  while ( !_findnext(hld,&fileinfo) ) {
    if ( fileinfo.attrib == _A_SUBDIR )
      nf++; }
  _findclose(hld) ;
  
  info *liste =(info *) calloc(nf,sizeof(info));

  hld = _findfirst(argv[1],&fileinfo) ;
  int i = 0;
  if ( hld != -1 )
    if ( fileinfo.attrib == _A_SUBDIR ) {
      strcpy(liste[i].name,pathSplit(argv[1]));
      strcat(liste[i].name,fileinfo.name);
      liste[i].size = (int) fileinfo.size;
      i++; }
  while ( !_findnext(hld,&fileinfo) ) {
    if ( fileinfo.attrib == _A_SUBDIR ) {
      strcpy(liste[i].name,pathSplit(argv[1]));
      strcat(liste[i].name,fileinfo.name);
      liste[i].size = (int) fileinfo.size;
      i++; } }
  _findclose(hld) ;

  FILE *f = fopen("CompileTout.bat","wt");
  FILE *m = fopen("..\\Sources\\MakeTout.bat","wt");

  fprintf(m,"md Temp\nmd Binaires\n\n");

  for ( i = 0 ; i < nf ; i++ ) {
    compile(liste[i].name,f);
    makefile(liste[i].name,m); }
  
  fclose(m);
  fclose(f);

  free(liste);
  f = fopen("..\\Sources\\MakeTout.bat","rb");
  FILE *f2 = fopen("..\\Sources\\MakeTout2.bat","wb");
  while ( !feof(f) ) {
    unsigned char c;
    fread(&c,1,1,f);
    fwrite(&c,1,1,f2);
    if ( c == 0x0D ) {
      fread(&c,1,1,f); } }
  fclose(f2);
  fclose(f);
	return 0;
}

