#include "galaxy.h"
#include "copyright.h"

#ifdef __STDC__
void 
reports (void)
#else
void 
reports ()
#endif
{
   player         *P;
   int             i;
   char *cap_gamename;
   char *F;
   static char     buf2[256];
   char *uncorrupted_address; /* <-----is a TEMPORARY FIX and will need
					to be fixed properly at some point. */

   mkdir("reports", 0700);
   sprintf(buf, "reports/%s", gamename);
   mkdir(buf, 0700);
   F = findfirst(buf);
   while (F) {
      sprintf(buf2, "reports/%s/%s", gamename, F);
      unlink(buf2);
      F = findnext();
   }

   mkdir("nreports", 0700);
   sprintf(buf, "nreports/%s", gamename);
   mkdir(buf, 0700);
   F = findfirst(buf);
   while (F) {
      sprintf(buf2, "nreports/%s/%s", gamename, F);
      unlink(buf2);
      F = findnext();
   }

   reportfileno = 0;
   for (P = players; P; P = P->next)
      report(P);

   cap_gamename = strdup(gamename);
   cap_gamename[0] = toupper(cap_gamename[0]);

   f = Fopen("send", "a");
   printf("Writing send file...\n");
   reportfileno = 0;
   for (P = players; P; P = P->next)
      if (P->addr[0]) {
         reportfilename(P);
/* temp fix block begin */
	 uncorrupted_address = strdup(P->addr);
         for(i=0; uncorrupted_address[i]; ++i) 
		if(uncorrupted_address[i]==' ') uncorrupted_address[i] = '_';
/* end temp fix block */
         fprintf(f, "mail_report %s %d \"%s\" %s <reports/%s\n",
	    cap_gamename, turn, P->name, uncorrupted_address, buf + 8);
/* temp fix next line */
	 free(uncorrupted_address);

      }
   free(cap_gamename);
   fclose(f);
}

