/* wvm - wavemaker au prog, EK */

const char* args[7];
char *hme, pp[512];
short show_log=2;

// if((hme = getenv("USERPROFILE"))==NULL) hme=getenv("HOME");
sprintf(cmd, "%s/exp/stan/nmr/wavemaker/bin/wvm" EXECUTABLE_EXTENSION, PathXWinNMRInst());
sprintf(text, "%s/%s/%d", disk, name, expno); 

args[0] = cmd;                  
args[1] = "-e";
args[2] = text;  
args[3] = 0;
args[4] = 0;                    
args[5] = 0;                    
args[6] = 0;                    

  
if(i_argc > 2) 
{
  if (strcmp(i_argv[2], "-al") == 0) args[3] = "-a";               // auto setup
  else if (strcmp(i_argv[2], "-x") == 0)  args[3] = "-x";          // do not write shapefiles
  else if (strcmp(i_argv[2], "-v") == 0)  show_log=1;              // run wvm and show full log file
  else if (strcmp(i_argv[2], "-l") == 0)  show_log=-1;             // only show log file 
  else if (strcmp(i_argv[2], "-p") == 0)  show_log=-2;             // only show wvm.par file 
  else if ((i_argc>3) && (strcmp(i_argv[2], "-i") == 0))  
  {
  	show_log=-1;
  	args[3] = "-i";
    args[4] = i_argv[3];          // only show shape definition file 
    ExecProgram(args, 1);
  }
	else if (strcmp(i_argv[2], "-a") == 0) 
	{
    args[3] = "-a";               // auto setup, do not show log-file
    show_log=0;
	}
	else if (strcmp(i_argv[2], "-f") == 0) 
	{
    args[3] = "-f";               // auto setup, do not show log-file
    if(i_argc > 3) 
    {
    	if(strcmp(i_argv[3], "pp") == 0)
    	{
    	  FETCHPAR("PULPROG", pp) 
    	  strcat(pp,".par");
    	  args[4] = pp;
    	}
    	else
        args[4] = i_argv[3];
    }
	}
}

if(show_log > -1)  ExecProgram(args, 1);
if(show_log == -2) strcat(text, "/wvm.par");
else strcat(text, "/wvm.log");
if(show_log == 2) wvm_report(text,0); 
else if(show_log==0) wvm_report(text,1);
else showfile(text);
if((i_argc < 3) && (args[3]== "-a")) 
  XCMD("sendgui ased");  

QUIT


int wvm_report(char *path, int ii)
{
  char    txt[128];
  FILE*   fp;
  int     i;

  if ((fp = fopen(path, "r")) == NULL)       /* open the logfile */
  {
    Proc_err(DEF_ERR_OPT, "%s\n%s", "Cannot open wvm.log file...", path);
    return 1;
  }

  if(ii)   /* show warnings only */
  {
    while(fgets(txt,128,fp))      // show the Wellcome part - first 5 lines
    {
      if(strstr(txt, "WARNING") != NULL) printf("%s", txt); 
    }
    fclose(fp);
    return 0;
  }
  
  i=0;
  while((fgets(txt,128,fp)) && ((strstr(txt, "Opening:") == NULL))) // show the header 
    printf("%s", txt);  
  printf("\n%s", txt); i=0;
  
  while((fgets(txt,128,fp)) && (txt[0] != 'E'))  // show the saved files 
  {
  	if(strstr(txt, "saved") != NULL) 
  	{  
  	  printf("%s", txt);  
      i++;
  	}
  }
  if(i) printf("\n"); 
  printf("%s", txt);  
  while(fgets(txt,128,fp))                // show the Experiment Setup section only
    printf("%s", txt);  
  printf("~~~\n");
  fclose(fp);
  
  return 0;
}	
