/* Adapted from code written by Erich Kaltofen.
CGI handling adapted from code written by Sky Coyote,
(C) Copyright Sky Coyote, 1995. See http://www.intergalact.com/hp/
for original source code.
*/
   
/* Maple script */
static char maple[] = "/afs/eos.ncsu.edu/users/k/kaltofen/www/maple/bin/maple";

/* scratch file for Maple input */
static char maple_in_name[] = "/tmp/phbproj2_maple_input5";
/* scratch file for Maple output */
static char maple_out_name[] = "/tmp/phbproj2_maple_output5";

#include "maple.h" /* header file */
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h> /* contains case conversion routines used in htoi */
int main (int argc, char* argv[])
{
  char run_maple[128];			/* for executing Maple */
  char line[MAX_LINE_LENGTH];	/* Maple output */
  int len;						/* stdin string length */
  int i;						/* indexing variable */
  int charcount;				/* indexing variable */
  int c;						/* a character */
  char a[8][MAX_LINE_LENGTH];	/* eight strings read in from stdin */
  int mylength;					/* temporary string length variable */
  /* the following are strings which will be passed to Maple */
  char a11[MAX_LINE_LENGTH], a12[MAX_LINE_LENGTH], a21[MAX_LINE_LENGTH], a22[MAX_LINE_LENGTH];
  char  x1[MAX_LINE_LENGTH], x2[MAX_LINE_LENGTH];
  char  b1[MAX_LINE_LENGTH], b2[MAX_LINE_LENGTH];

  FILE  *maple_in, *maple_out;	/* Maple input/output files */

  int sys; /* return code from system call */

  /* get length of input text */

  len = 45;
  if (getenv("CONTENT_LENGTH")) len = atoi(getenv("CONTENT_LENGTH"));

  c = 0;
  charcount = 0;

  for (i = 0; i < 8; i ++) { /* load the eight inputs */
    mylength = 0;
    /* cycle through '<varname>=' */
    do {
      c = getchar();
      printf("var getchar: %c\n", c);

      charcount++;
    } while (c != '=');

    for(;charcount < len;) { /* loop is broken when reader hits
    							an '&' or end of line */
      c = getchar();
      printf("arg getchar: %c\n", c);

      charcount++;
  
      if (c == '&') break; /* next variable */
      if (c == '+') continue; /* whitespace. ignore */
      if (c == '%') { /* convert hex code */
             char s[3];
             s[0] = getchar();
             s[1] = getchar();
             s[2] = 0;
             c = htoi(s);
             charcount += 2;
      }

      a[i][mylength++] = c;
      a[i][mylength]='\0';

    };
    printf("Parsing a[%d]: %s\n", i, a[i]);
  }

  strcpy(a11,a[0]);
  strcpy(a12,a[2]);
  strcpy(a21,a[5]);
  strcpy(a22,a[6]);
  strcpy(x1,a[1]);
  strcpy(x2,a[3]);
  strcpy(b1,a[4]);
  strcpy(b2,a[7]);
 
 
  /* must give proper header information to browser for returned text */
  printf ("Content-type: text/plain\n\n");


  /* try to open scratch file for Maple input */
  maple_in = fopen (maple_in_name, "w");
  if (maple_in == NULL)
     { printf("fopen(\"%s\") failed: %s\n",
              maple_in_name, strerror(errno));
       return errno;
     }
                          
  /* prepare Unix Maple command line */
  sprintf (run_maple,
           "%s -f -a -s -q < %s > %s",
           maple, maple_in_name, maple_out_name);

  /* show it */
  /* printf ("%s\n", run_maple); */

  /* place Maple command into scratch file */
  fprintf(maple_in, "solve({%s * %s + (%s)*%s = %s,%s*%s+ (%s)*%s = %s},{%s,%s});\n",
    a11,x1,a12,x2,b1,a21,x1,a22,x2,b2,x1,x2);

  /* show it */
  /*  printf("> solve({%s * %s + (%s)*%s = %s,%s*%s+ (%s)*%s = %s},{%s,%s});\n",
    a11,x1,a12,x2,b1,a21,x1,a22,x2,b2,x1,x2);*/
  fflush(stdout);
  fclose(maple_in);

  /* for debugging: just cat what was put into in_file */
  /* sprintf (run_maple, "cat %s", maple_in_name);
  printf(run_maple); */

  /* check if shell available */
  sys = system(NULL);
  if(sys == 0)
    { printf("system(NULL) failed: no shell available");
      remove(maple_in_name);
      return -1;
    }

  /* execute Maple command */
  sys = system (run_maple);
  if(sys == -1)
    { printf("system(\"%s\") failed: %s\n",
              run_maple, strerror(errno));
      remove(maple_in_name);
      return errno;
    }

  maple_out = fopen (maple_out_name, "r");
  if (maple_out == NULL)
     { printf("fopen(\"%s\") failed: %s\n",
              maple_out_name, strerror(errno));
       remove(maple_in_name);
       return errno;
     }

  /* copy Maple output to stdout */
  while(fgets(line,MAX_LINE_LENGTH,maple_out))
       printf("%s",line);

  /* clean up scratch files */
  fclose(maple_out);
  /* remove(maple_in_name); */
  remove(maple_out_name);


  printf("THE END\n");

  return 0;
}

int htoi(char* s) {
/* convert hex string to int */

char* digits = "0123456789ABCDEF";

if (islower(s[0])) s[0] = toupper(s[0]);
if (islower(s[1])) s[1] = toupper(s[1]);

return 16 * (strchr(digits, s[0]) - strchr(digits, '0')) 
        + (strchr(digits, s[1]) - strchr(digits, '0'));
}
