#include "c4.h"
#include <string.h>

int main(int argc,
         char *argv[])
{
  int i;
  int w=222,h=222;
  int itr_limit=12;
  double r_min=-2,r_max=.7,i_min=-1.35,i_max=1.35;
  char *my_name,*out="c4-image.ppm";
  struct Image *img;
  C4Fractal fr;

  my_name = argv[0];

  for(i=1;i<argc;i++) {
    if(!strcmp(argv[i],"-h") ||
       !strcmp(argv[i],"--help")) {
      printf(

"usage:\n"
"  %s [ options ]\n"
"options:\n"
"  -h || --help              this help message\n"
"  -d || --dim <w> <h>       sets image width and height\n"
"                             to w and h\n"
"  -o || --out <file>        sets the output file name\n"
"  -r || --real <min> <max>  set the real value range\n"
"  -i || --imag <min> <max>  set the imaginary value range\n"
"  -t || --itr <max>         set the iteration limit\n",

             my_name);
      exit(0);
    }
    else if(!strcmp(argv[i],"-d") ||
            !strcmp(argv[i],"--dim")) {
      w = atoi(argv[++i]);
      h = atoi(argv[++i]);
    }
    else if(!strcmp(argv[i],"-o") ||
            !strcmp(argv[i],"--out"))
      out = argv[++i];
    else if(!strcmp(argv[i],"-r") ||
            !strcmp(argv[i],"--real")) {
      r_min = atof(argv[++i]);
      r_max = atof(argv[++i]);
    }
    else if(!strcmp(argv[i],"-i") ||
            !strcmp(argv[i],"--imag")) {
      i_min = atof(argv[++i]);
      i_max = atof(argv[++i]);
    }
    else if(!strcmp(argv[i],"-t") ||
            !strcmp(argv[i],"--itr"))
      itr_limit = atoi(argv[++i]);
    else
      fprintf(stderr,
              "%s: unknown option '%s'; use -h for help\n",
              my_name,
              argv[i]);
  }

  img = c4_image_create(w,h);

  fr = c4_fractal_create(img,3,
                         RealLimit,r_min,r_max,
                         ImaginaryLimit,i_min,i_max,
                         ItrLimit,itr_limit);
  c4_fractal_render(fr);

  c4_io_out(img,out);

  c4_image_destroy(img);

  return(0);
}
