/*
 *             Automatically Tuned Linear Algebra Software v3.2
 *                    (C) Copyright 2000 R. Clint Whaley                     
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions, and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *   3. The name of the University of Tennessee, the ATLAS group,
 *      or the names of its contributers may not be used to endorse
 *      or promote products derived from this software without specific
 *      written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE. 
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int LineIsCont(char *ln)
{
   int i, iret=0;
   for(i=0; ln[i]; i++);
   if (i)
   {
      for(i--; isspace(ln[i]); i--);
      if (ln[i] == '\\') iret = 1;
   }
   return(iret);
}

int NumUserCases0(char *nam)
{
   int iret=0;
   char ln[512];
   FILE *fp;

   fp = fopen(nam, "r");
   if (fp)
   {
      fgets(ln, 512, fp);  /* skip comment line */
      assert(fscanf(fp, " %d", &iret) == 1);
      fclose(fp);
   }
   return(iret);
}

int NumUserCases(char pre)
{
   char ln[64];

   sprintf(ln, "%ccases.dsc", pre);
   return(NumUserCases0(ln));
}

void NoEndLineWhiteSpace(char *ln)
{
   int i;

   for (i=0; ln[i]; i++);
   if (i)
      for (i--; isspace(ln[i]); i--) ln[i] = '\0';
}

int GetUserCase(char pre, int icase, int *iflag, int *mb, int *nb, int *kb, 
                int *ma, int *lat, int *mu, int *nu, int *ku, 
                char *fnam, char *auth, char **MCC, char **MMFLAGS)
{
   int i;
   char ln[512];
   static char sMCC[128], sMMFLAGS[512];
   FILE *fp;

   sprintf(ln, "%ccases.dsc", pre);
   fp = fopen(ln, "r");
   if (!fp) return(0);
   assert(fp);
   fgets(ln, 256, fp);  /* skip comment line */
   fgets(ln, 256, fp);  /* skip number of cases */
   for (i=0; i <= icase; i++)
   {
      if ( fgets(ln, 256, fp) == NULL )
      {
         fclose(fp);
         return(0);
      }
      if (i != icase && LineIsCont(ln))
      {
         assert( fgets(ln, 256, fp) != NULL );
         assert( fgets(ln, 256, fp) != NULL );
      }
   }
   assert(sscanf(ln, " %d %d %d %d %d %d %d %d %d %s \"%[^\"]", 
                 iflag, mb, nb, kb, ma, lat, mu, nu, ku, fnam, auth) == 11);
   if (LineIsCont(ln))
   {
      assert( fgets(ln, 256, fp) != NULL );
      strcpy(sMCC, ln);
      NoEndLineWhiteSpace(sMCC);
      assert( fgets(ln, 512, fp) != NULL );
      strcpy(sMMFLAGS, ln);
      NoEndLineWhiteSpace(sMMFLAGS);
      *MCC = sMCC;
      *MMFLAGS = sMMFLAGS;
   }
   else *MCC = *MMFLAGS = NULL;
   fclose(fp);
   return(1);
}

void GoGetThem(char *infile, char *outfile)
{
   char ln[512], ln2[512], tnam[256], MCC[256], MMFLAGS[256];
   char *chkfile = "FlagCheck.c";
   FILE *fpin, *fpout;
   int i, j, n, nmin=0;

   fpout = fopen(chkfile, "w");
   assert(fpout);
   fprintf(fpout, "main(){}\n");
   fclose(fpout);
   n = NumUserCases0(infile);
   fpin = fopen(infile, "r");
   assert(fpin);
   assert(tmpnam(tnam));
   fpout = fopen(tnam, "w");
   assert(fpout);
   assert(fgets(ln, 512, fpin));
   assert(fgets(ln, 512, fpin));
   for (i=0; i < n; i++)
   {
      assert(fgets(ln, 512, fpin));
      if (LineIsCont(ln))
      {
         assert(fgets(MCC, 256, fpin));
         assert(fgets(MMFLAGS, 256, fpin));
         sprintf(ln2, "%s %s -c %s\n", MCC, MMFLAGS, chkfile);
         for (j=0; ln2[j]; j++) if (ln2[j] == '\n') ln2[j] = ' ';
         ln2[j-1] = '\n';
         fprintf(stdout, "%s", ln2);
         if (system(ln2) == 0)
         {
            fputs(ln, fpout);
            fputs(MCC, fpout);
            fputs(MMFLAGS, fpout);
         }
         else nmin--;
      }
      else fputs(ln, fpout);
   }
   fclose(fpin);
   fclose(fpout);
/*
 * Now, create standard-style file with surviving routines
 */
   fpin = fopen(tnam, "r");
   assert(fpin);
   fpout = fopen(outfile, "w");
   assert(fpout);
   n += nmin;
   fprintf(fpout, "\n%d\n", n);
   for (i=0; i < n; i++)
   {
      assert(fgets(ln, 512, fpin));
      fputs(ln, fpout);
      if (LineIsCont(ln))
      {
         assert(fgets(ln, 512, fpin));
         fputs(ln, fpout);
         assert(fgets(ln, 512, fpin));
         fputs(ln, fpout);
      }
   }
   fclose(fpin);
   fclose(fpout);
   remove(tnam);
   sprintf(tnam, "%s", chkfile);
   for (i=0; tnam[i]; i++);
   for (i--; i > 0; i--)
   {
      if (tnam[i] == 'c')
      {
         tnam[i] = 'o';
         break;
      }
   }
   remove(tnam);
}
void PrintUsage(char *nam)
{
   fprintf(stderr, "USAGE: %s -o <outfile> -i <infile>\n", nam);
   exit(-1);
}

void GetFlags(int nargs, char **args, char **infile, char **outfile)
{
   int i;

   *infile = "../CASES/dcases.flg";
   *outfile = "dcases.gfg";

   for (i=1; i < nargs; i++)
   {
      if (args[i][0] != '-') PrintUsage(args[0]);
      switch(args[i][1])
      {
      case 'o':
         *outfile = args[++i];
         break;
      case 'i':
         *infile = args[++i];
         break;
      default:
         PrintUsage(args[0]);
      }
   }
}

main(int nargs, char **args)
/*
 * Eliminates compiler/flag combos that don't work, 
 * NOTE: infile & outfile can be same
 */
{
   char *infile, *outfile;
   GetFlags(nargs, args, &infile, &outfile);
   GoGetThem(infile, outfile);
   exit(0);
}
