/******J. Denise Terry********/
/******October 8, 1999********/
/******Program 2**************/
/******MA 591K****************/

 
/* 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/jdterry_proj2_maple_input";


/* scratch file for Maple output */
static char maple_out_name[] = "/tmp/jdterry_proj2_maple_output";



#define MAX_LINE_LENGTH 80 /* max length copied from Maple out to stdout */

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char* argv[])
{
  char run_maple[128];
  char line[MAX_LINE_LENGTH];
  

       
  FILE  *maple_in, *maple_out;

 int sys; /* return code from system call */
 char a11[10];        /*first value in first equation*/
 char a12[10];        /*second value in first equation*/
 char b1[10];         /*value that first equation is equal to*/
 char a21[10];        /*first value in second equation*/
 char a22[10];        /*second value in second equation*/
 char b2[10];         /*value that second equation is equal to*/
 char var1str[3];     /*first variable in system*/
 char var2str[3];     /*second variable in system*/
 
   
 /* field values are received on stdin */
  scanf("a11=%[^&]", a11);
  getchar();
  scanf("x1=%[^&]",  var1str);
  getchar();
  scanf("a12=%[^&]", a12);
  getchar();
  scanf("x2=%[^&]",  var2str);
  getchar();
  scanf("b1=%[^&]",  b1);
  getchar();
  scanf("a21=%[^&]", a21);
  getchar();
  scanf("a22=%[^&]", a22);
  getchar();
  scanf("b2=%[^&]",  b2);

  
     
 
  /* 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, "a11:=%s", a11);
   fprintf(maple_in,";\n");
   fprintf(maple_in, "a12:=%s", a12);
   fprintf(maple_in,";\n");
   fprintf(maple_in, "b1:=%s",  b1);
   fprintf(maple_in,";\n");
   fprintf(maple_in, "a21:=%s", a21);
   fprintf(maple_in,";\n");
   fprintf(maple_in, "a22:=%s", a22);
   fprintf(maple_in,";\n");

  /* place Maple command into scratch file */
  fprintf(maple_in,
         "solve({(%s)*%s+(%s)*%s=(%s),(%s)*%s+(%s)*%s=(%s)},{%s,%s});\n",
         a11,var1str,a12,var2str,b1,a21,var1str,a22,var2str,b2,var1str,var2str); 
	 
  /* show it */
  printf("> solve({(%s)*%s+(%s)*%s=(%s),(%s)*%s+(%s)*%s=(%s)},{%s,%s});\n",
         a11,var1str,a12,var2str,b1,a21,var1str,a22,var2str,b2,var1str,var2str);

  fflush(stdout);
  fclose(maple_in);


  /* 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;
}
