#include "tree.h"
#include "cftpmain.h"
#include "getp.h"
#include "dom.h"
#include "process.h"
#include "ellipse.h"

int main(int argc, 
	 char *argv[]
	 )   /* This program is the front-end to my cftp thingy.  */
{
  double time;       /* Number of iterations to run the process. */

  int i,j;

  double gamma1;          /* the value of gamma1 */

  double gamma2;          /* the value of gamma2 */

  double a1;              /* the value of the parameter `a' of G1 */

  double a2;              /* the value of the parameter `a' of G2 */

  double b1;              /* the value of the parameter `b' of G1 */

  double b2;              /* the value of the parameter `b' of G2 */

  double lambda;     /* lambda - the value of lambda in the prior */

  double pmin;         /* Proportion of points in dominating process */
                       /* which are retained in minimum process */

  point *points=NULL;

  short stop;

  long seed, seedtwo, seedthree;

  double exp_param;

  if(argc <= 1){
    fprintf(stderr,"There was no specified input!\n");
    return 1;
  }

/* getparameter obtains the values of the parameters */
  else if(getparameters(&lambda,&gamma1,&gamma2,&a1,&a2,&b1,
			&b2,&time,&seed,&seedtwo,&seedthree,
			argv[1])){

/* We now calculate two basic values which are used in the rest 
     of the algorithms: exp\_param and pmin.  This saves passing 
     parameters which are only used to calculate these values. */
    exp_param=lambda*pow(gamma2,-measure(a2,b2)*SCALE);
    pmin=pow(gamma1,-measure(a1,b1)*SCALE)*pow(gamma2,measure(a2,b2)*SCALE);

    stop = 0;
    for(i=1;!stop;i*=2){
      /* Before each iteration we must free memory used by points */
      j=0;                              /* in previous iterations */
      while(points != NULL){
	points=remove_left(points);
	++j;
      }

/* Dominating generates the dominating process and returns 
   a pointer to the root of the tree containing the points. */
      points=dominating(i*time,pmin,exp_param,seed,seedtwo,
			seedthree);

/* Finally we get to the actual process.  This function 
   determines which points will be born and which will not in 
   the true process and determines whether the algorithm has 
   converged. */
      points = process(i*time,gamma1,gamma2,a1,a2,b1,b2,exp_param,
		       seed,seedtwo,seedthree,points,&stop);
    }

    /* Print the coordinates of the points */
    applyinorder(points,printcoord,"%.8e %.8e\n");

    /* Output the length of time taken to converge to standard 
       error: */
    fprintf(stderr,"-M=%g\n",i*time/2);
    return 0;
  }
  else
    return 1;
}
