

#include "drawwindow.h"  // also calls global.h
#include "tordyn.h" // routines for torsiondynamics
#include "sphere.h"  // routines for sphere pull down
#include "math.h"
#include "initialize.h"
#include "bone.h"
#include "peptide.h"  // routines for peptide creation and drawing
#include "pdbtolev.h"
#include "trace.h"
#include "extendfit.h"
#include "editbones.h"
#include "assignseq.h"
#include "control.h"
#include "combine.h"

#include "string.h"
#include "stdlib.h"
#define srand48 srand 

struct extbones ext;
struct maiddatafile maidfile;//Global variable - I do not know how to pass to menus
struct griddata map[NMAPS];
struct bonedata bone[NMAPS];
struct geometry geo;
struct tordyn tordata;
struct tordyn tordata2; // used only if fixend==3
struct spheredata sp[NSPHERES];
struct pepfit fit[NFIT]; // defined in peptide.h, see trace.c++ for definitin
struct pepset fitset;
struct rotomer rot[30];// rotomer data for each amino acid
float rotpos[NROT][NATOMS][3];// position of atoms in rotomer 
int rottype = PHE;  // Type of rotomer to draw
int fitnum[NFIT];// see trace.c++ for definition


/* global variables - Other files use these variable defined as "extern" */
FILE *fpout;
//int writelog=0; /* if =1 write to logfile, otherwise, to screen */
int writelog=0; /* if =1 write to logfile, otherwise, to screen */
int verbose = 1; // if = 1,print some stuff;  = 2 print a lot of stuff
char ranknum[20];

#if 1
void readmaidfilebatch(struct maiddatafile& maidfile,struct griddata map[],
       struct bonedata bone[])
/*
  Batch version of readmaid file - identical to readmaidfile but screen
    stuff removed 
 This file also include some initial PDB settings since it is only
    called at beginning of reading pdb files
*/
{ 
    int i;
    char junk[200],line[300];
    FILE *fp;

    if( (fp=fopen(maidfile.name,"r")) == NULL){
         printf("ERROR:  file %s cannot be found\n",maidfile.name);
         exit(0);
    }
    fgets(line,200,fp); /* read each line*/
    sscanf(line,"%s%s%s%d",junk,junk,junk,&maidfile.npdb);
    for(i=1;i<= maidfile.npdb;i++){
        fgets(line,200,fp); /* read each line*/
        sscanf(line,"%s%s",junk,maidfile.pdb[i].name);
        maidfile.pdb[i].start=1; // Indicates - just read, pdbtolev not run yet
        maidfile.pdb[i].onoff = 1; // Default turn on draw
        maidfile.pdb[i].linethick = 1.0; // Default line thickness
        maidfile.pdb[i].sideonoff =1; // Default - turn on side chains,connect all atoms
        maidfile.pdb[i].pickres[0].num = 0; // Initially, no residues picked
    }
    fgets(line,200,fp); /* read each line*/
    sscanf(line,"%s%s%s%d",junk,junk,junk,&maidfile.nden);
    for(i=1;i<= maidfile.nden;i++){
       fgets(line,200,fp); /* read each line*/
       if(maidfile.start!=2){ // if = 2, then have already picked new and read the one map
          sscanf(line,"%s%s",junk,map[i].mapname);
          readbinarymap(map[i]);
       }
       fgets(line,200,fp); /* read bonefile line*/
       sscanf(line,"%s%s",junk,bone[i].bonename);
       if(strcmp("none",bone[i].bonename) == 0) // if bonefile does not exist
          bone[i].iexist = 0;
       else 
          bone[i].iexist = 1; // indicates file exist but not yet read
       bone[i].num = i; // assign number corresponding to map number
    }
    fgets(line,200,fp); /* read each line*/
    sscanf(line,"%s%s",junk,maidfile.spherefile);
    fgets(line,200,fp); /* read each line*/
    sscanf(line,"%s%s",junk,maidfile.fitfile);
    fgets(line,200,fp); /* read each line*/
    sscanf(line,"%s%s",junk,maidfile.editbones);
    fgets(line,200,fp); /* read each line*/
    sscanf(line,"%s%s",junk,maidfile.pdbsequence);
    fgets(line,200,fp); /* read each line*/
    sscanf(line,"%s%s",junk,maidfile.nsym);
    fgets(line,200,fp); /* read each line*/
    sscanf(line,"%s%s",junk,maidfile.selenomet);
   fclose(fp);
// The following file names are created and used but not written in mainfile
    strcpy(maidfile.resqual,maidfile.name);
    strcat(maidfile.resqual,"resqual"); 
    strcpy(maidfile.firstedit,maidfile.name);
    strcat(maidfile.firstedit,"firstedit"); 
   quitread:;
}
#endif

int main(int argc,char *argv[])
{
   char logfile[300],maidname[300],tracext[300],savfile[300];
   int i;
   int usecvd;  // = 1 if want to use casevision
   FILE *fp;   

   usecvd = 0;  //if = 1,  use casevisiion
//   usecvd = 1;  //if = 1,  use casevisiion
printf("STARTING BATCH MAID Linux version  Copyright David G. Levitt, 2000\n");
//#if 1//This has output written immediatedly after each line - should comment out if not neede
//   setlinebuf(stdout); //This has output written immediatedly after each line
//#endif
   if(writelog ==1){
        strcpy(logfile,"maid.log"); /* print to log file */
        fpout=fopen(logfile,"w");
   }
   else
       fpout=stdout;/* print to console */
   initialize(geo,fitnum,fit,sp,maidfile,tordata,tordata2,bone,fitset,rot);

 if(usecvd==0){ // use standard input
   if(argc <3){
     printf("ERROR:  Required format:  maidbatch maidfilename  ('complete' or 'trace' or 'trace_cont' or 'extend' or 'ncs')\n");
     exit(0);
   }
   for(i=1;i<=argc;i++)     
       fprintf(fpout,"  argv[%d] = %s",i,argv[i]);
   fprintf(fpout,"\n");
   strcpy(maidname,argv[1]);   
   strcpy(tracext,argv[2]);
 }
 else{ // use casevision
//  strcpy(maidname,"endodm.maid");
//  strcpy(tracext,"trace");
//  strcpy(maidname,"ctd.maid");
//  strcpy(maidname,"shadb.maid");
  strcpy(maidname,"shad.maid");
//  strcpy(tracext,"setup");
  strcpy(tracext,"trace");
 }

 if(strcasecmp(tracext,"setup")==0){ // then call setup - create maidfile and quits
     setup(maidfile,map,bone,maidname);
     goto quitrout;
 }

   srand48(7); // initialize randum number generator (drand48)
   strcpy(maidfile.name,maidname);
printf("REading maidfile %s\n",maidfile.name);
   readmaidfilebatch(maidfile,map,bone);
printf(" DONe reading\n");
if(usecvd==0){
   if(argc==4){
      strcpy(maidfile.fitfile,argv[3]);
      printf(" USING input fitfile %s\n",maidfile.fitfile);
   }
   else
      printf(" USING default fitfile %s\n",maidfile.fitfile);
}
else{ // use casevision
#if 0
      strcpy(maidfile.fitfile,"fah.maidfit");
      printf(" USING input fitfile %s\n",maidfile.fitfile);
#endif
      printf(" USING default fitfile %s\n",maidfile.fitfile);
}

   if( (strcasecmp(tracext,"trace")==0)||(strcasecmp(tracext,"trace_cont")==0) ){ // then call tracepep - finds helix/sheet fit
      if(strcasecmp(tracext,"trace_cont")==0)
         geo.newfit =1; //Continue from last previous fit in fitfile
      else
         geo.newfit =0; //Start over with new fit
#if 1 // Comment out if use  tracepepbatch   
      if( bone[1].iexist == 0){
         fprintf(fpout,"ERROR:  No bone file listed in %s\n",maidfile.name);
         exit(0);
      }
       if( (fp=fopen(bone[1].bonename,"r")) == NULL){
           printf("ERROR: bonefile %s does not exist\n",bone[1].bonename);
           printf("   Run maidbatch 'name' setup - to create this file\n");
           exit(0);
       }
       else{
               fclose(fp);
               readbonefile(bone[1]);
       }
#endif
//      tracepepbatch(map[1],geo,bone[1]);
      tracepep(map[1],geo,bone[1]);
   }
   else if(strcasecmp(tracext,"extend")==0){ // then call tracepep - finds helix/sheet fit
//      extendfit(fitset,map[1],geo,bone[1]);
        extendandfusebatch(fitset,map[1],geo); // Tries to extend and fuse all fits
   }
   else if(strcasecmp(tracext,"retrace")==0){ // then call retrace - finds helix/sheet fit after good fits found
        retrace(map[1],geo,bone[1]);
   }
   else if(strcasecmp(tracext,"ncs")==0){ // then call ncs - finds helix/sheet fit after good fits found
       expand(map[1],geo,maidfile.fitfile);
       assignseqment(map[1],geo,"expand");
   }
   else if(strcasecmp(tracext,"complete")==0){ // then run trace - extend - ncs
         geo.newfit =0; //Start over with new fit
#if 1 // Comment out if use  tracepepbatch   
      if( bone[1].iexist == 0){
         fprintf(fpout,"ERROR:  No bone file listed in %s\n",maidfile.name);
         exit(0);
      }
       if( (fp=fopen(bone[1].bonename,"r")) == NULL){
           printf("ERROR: bonefile %s does not exist\n",bone[1].bonename);
           printf("   Run maidbatch 'name' setup - to create this file\n");
           exit(0);
       }
       else{
               fclose(fp);
               readbonefile(bone[1]);
       }
#endif
//      tracepepbatch(map[1],geo,bone[1]);
       tracepep(map[1],geo,bone[1]);
       extendandfusebatch(fitset,map[1],geo); // Tries to extend and fuse all fits
       expand(map[1],geo,maidfile.fitfile);
       assignseqment(map[1],geo,"expand");
   }
   else if(strcasecmp(tracext,"test")==0){ // then call one of the testing routines listed below
      testseqbatch(map[1],geo);
  }
   else { // ERROR
     printf("ERROR:  Required format:  maidbatch maidfilename  ('complete' or 'trace' or 'trace_cont' or 'extend' or 'ncs' or 'setup') \n");
     printf("        (optional) followed by name of fitfile \n");
   }
   quitrout:;
}

