# include "delstar.h"

void help () {

  fprintf (stderr, "USAGE:\n");
  fprintf (stderr, "  delstar (filename)\n");
  fprintf (stderr, "  delstar -name (filename)\n");
  fprintf (stderr, "  delstar -time (start) (stop/range)\n");
  fprintf (stderr, "  delstar -orphan (region)\n");
  fprintf (stderr, "  delstar -missed (region)\n\n");
  fprintf (stderr, "  optional flags:\n");
  fprintf (stderr, "  -v               : verbose mode\n");
  fprintf (stderr, "  -photcode (code) : restrict by photcode\n");
  fprintf (stderr, "\n"); 
  exit (2);

}

void usage () {
  fprintf (stderr, "USAGE: delstar (filename) / [optional mode] : -h for help\n");
  exit (2);
}

int args (int *argc, char **argv) {
  
  int N;
  double trange;
  time_t tmp;

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

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

  MODE = MODE_IMAGEFILE;
  if ((N = get_argument (*argc, argv, "-name"))) {
    if (MODE != MODE_IMAGEFILE) usage();
    MODE = MODE_IMAGENAME;
    remove_argument (N, argc, argv);
  }
  if ((N = get_argument (*argc, argv, "-orphan"))) {
    if (MODE != MODE_IMAGEFILE) usage();
    MODE = MODE_ORPHAN;
    remove_argument (N, argc, argv);
  }
  if ((N = get_argument (*argc, argv, "-missed"))) {
    if (MODE != MODE_IMAGEFILE) usage();
    MODE = MODE_MISSED;
    remove_argument (N, argc, argv);
  }
  if ((N = get_argument (*argc, argv, "-time"))) {
    if (MODE != MODE_IMAGEFILE) usage();
    MODE = MODE_TIME;
    remove_argument (N, argc, argv);

    if (!ohana_str_to_time (argv[N], &START)) usage ();
    remove_argument (N, argc, argv);

    /* interpret second value */
    if (ohana_str_to_dtime (argv[N], &trange)) { 
      if (trange < 0) {
	END = START;
	START = END + trange;
      } else {
	END = START + trange;
      }
      remove_argument (N, argc, argv);
      goto goodtime;
    }
    if (ohana_str_to_time (argv[N], &END)) { 
      if (START > END) {
	tmp   = START;
	START = END;
	END   = START;
      }
      remove_argument (N, argc, argv);
      goto goodtime;
    }
    usage ();
  }
goodtime:

  /* restrict to a single photcode (not compatible with -image) */
  PHOTCODE = NULL;
  if ((N = get_argument (*argc, argv, "-photcode"))) {
    remove_argument (N, argc, argv);
    PHOTCODE = GetPhotcodebyName (argv[N]);
    remove_argument (N, argc, argv);
  }

  if ((MODE == MODE_TIME) && (*argc != 1)) usage ();
  if ((MODE != MODE_TIME) && (*argc != 2)) usage ();
  return (TRUE);
}
