# include "skycells.h"
static void help (void);

int args_skycells (int argc, char **argv) {
  
  int N;
  char *ptr;

  /* check for help request */
  if (get_argument (argc, argv, "-help") ||
      get_argument (argc, argv, "-h")) {
    help ();
  }

  /* extra error messages */
  VERBOSE = FALSE;
  if ((N = get_argument (argc, argv, "-v"))) {
    VERBOSE = TRUE;
    remove_argument (N, &argc, argv);
  }

  /* what type of output files? */
  MODE = SQUARES;
  if ((N = get_argument (argc, argv, "-triangles"))) {
    MODE = TRIANGLES;
    remove_argument (N, &argc, argv);
  }

  /* what type of output files? */
  FIX_NS = FALSE;
  if ((N = get_argument (argc, argv, "-fix-ns"))) {
    FIX_NS = TRUE;
    remove_argument (N, &argc, argv);
  }

  /* pixel scale (arcsec/pixel) */
  SCALE = 1.0;
  if ((N = get_argument (argc, argv, "-scale"))) {
    remove_argument (N, &argc, argv);
    SCALE = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }

  /* pixel scale (arcsec/pixel) */
  EULER_A = EULER_B = 0.0;
  if ((N = get_argument (argc, argv, "-euler"))) {
    remove_argument (N, &argc, argv);
    EULER_A = RAD_DEG*atof (argv[N]);
    remove_argument (N, &argc, argv);
    EULER_B = RAD_DEG*atof (argv[N]);
    remove_argument (N, &argc, argv);
  }

  /* pixel scale (arcsec/pixel) */
  PADDING = 0.0;
  if ((N = get_argument (argc, argv, "-padding"))) {
    remove_argument (N, &argc, argv);
    PADDING = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }

  /* max number of skycells kept in memory */
  NMAX = 200000;
  if ((N = get_argument (argc, argv, "-nmax"))) {
    remove_argument (N, &argc, argv);
    NMAX = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }

  /* max number of skycells kept in memory */
  NX_SUB = NY_SUB = 1;
  if ((N = get_argument (argc, argv, "-nx"))) {
    remove_argument (N, &argc, argv);
    NX_SUB = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if ((N = get_argument (argc, argv, "-ny"))) {
    remove_argument (N, &argc, argv);
    NY_SUB = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }

  if (argc == 2) {
    strtol (argv[1], &ptr, 10);
    if (*ptr == 0) return (TRUE);
  }

  fprintf (stderr, "USAGE: skycells (level) [-scale arcsec/pix] [-nx (Nx cells)] [-ny (Ny cells)]\n");
  fprintf (stderr, "  [-h for details and other options]\n");
  exit (2);
}

static void help () {

  fprintf (stderr, "USAGE\n");
  fprintf (stderr, "  skycells\n\n");

  fprintf (stderr, "  -v                    	  : verbose mode\n");
  fprintf (stderr, "  -triangles                  : save base triangles instead of skycells\n");
  fprintf (stderr, "  -fix-ns                     : orient skycells with y-axis aligned with Dec\n");
  fprintf (stderr, "  -scale                      : set pixel scale (default 1.0 arcsec / pixel)\n");
  fprintf (stderr, "  -padding                    : pad skycells by this fraction in each dimension\n");
  fprintf (stderr, "  -nmax                    	  : only keep nmax skycells in memory\n");
  fprintf (stderr, "  -nx                    	  : subdivide skycell projection in x\n");
  fprintf (stderr, "  -ny                    	  : subdivide skycell projection in y\n");
  fprintf (stderr, "  -help                 	  : this list\n");
  fprintf (stderr, "  -h                    	  : this list\n\n");
  exit (2);
}
