
typedef struct {
  int state; /* none, close, perfect */
  int crit;  /* which criterion? */
  int image; /* which detrend image? */
} Match;

typedef struct {
  int TimeSelect;    time_t tstart, tstop;
  char *label; 
  char *imageID;
  int order;
  int type;
  char mode;
  int CCDSelect;     int CCD;
  int ExptimeSelect; float Exptime;
  int filter;
} Descriptor;

/* MosaicSelect & ImageSelect define these values */
typedef struct {
  int ModeSelect;    int Mode;
  int TypeSelect;    int Type;
  int CCDSelect;     int CCD;
  int FilterSelect;  int Filter;
  int EntrySelect;   int Entry;
  int LabelSelect;   char *Label;
  int NameSelect;    char *Name;
  int ExptimeSelect; float Exptime;
  int TimeSelect;    time_t tstart, tstop;
  int MatchNumber;
} Criteria;

int Ncriteria;
Criteria *criteria;

struct {
  int Select;
  int Delete;
  int Modify;
  int Altpath;
  int ElixirSmart;
  int TimeMode;
  int Recipe;
  int Close;
  int Criteria;
  int Chipname;

  char *ModifyEntry, *ModifyValue;
  time_t TimeValue;
  
  int verbose;
  char *table;
  char *bintable;
} output;

/* altpath values */
enum {NONE, ADD, DELETE, UPDATE};
enum {START, STOP, REG};
enum {UNLOCK, LOCK, IGNORE};
enum {MATCH_NONE, MATCH_CLOSE, MATCH_PERFECT};

# define DEBUG 0

char **RecipeType;
int Nrecipe;

int SingleIsSplit;
int NoReg;

int regargs (int argc, char **argv, Descriptor *descriptor);
int args (int argc, char **argv);
int DefineImage (char *filename, Descriptor *descriptor);
DetReg DefineEntry (Descriptor descriptor);

char *set_dBFile ();
char *get_dBPath ();
int delete_image (DetReg *image);

int SaveEntry (char *input, DetReg *newdata, char *ID);

char **LoadRecipe (char *filter, int *nrecipe);

Match *MatchCriteria (DetReg *image, int Nimage, int *nmatch);
Match *UniqueSubset (DetReg *image, int Nimage, Match *match, int *nmatch);
Match *ExptimeCriteria (DetReg *image, int Nimage, Match *match, int *nmatch);
Match *CloseCriteria (DetReg *image, int Nimage, Match *match, int *nmatch);

Match CheckCriteria (DetReg *image);

int OutputSubset (DetReg *image, int Nimage, Match *match, int Nmatch);
int DumpFitsTable (char *filename, DetReg *detdata, Match *match, int Nmatch);
int PrintSubset (DetReg *detdata, Match *match, int Nmatch);
Match SelectEntry (DetReg *image, int Nimage, Match *list, int Nlist, Criteria *crit);

int usage ();
Criteria *MosaicCriteria (Criteria base, char *filename, int *ncrit);
Criteria *ImageCriteria (Criteria base, char *filename, char *ImageExtend, char *ImageMode, int *ncrit);
Criteria *ExpandBase (Criteria base, int *ncrit, time_t *tstart, time_t *tstop, int *filt);
Criteria *ExpandRecipe (Criteria *base, int *Ncrit);

int escape (int mode, char *message);
int DumpFitsBintable (char *filename, DetReg *image, Match *match, int Nmatch);
int cmp_crit (Criteria *crit, DetReg *image);
int set_crit (Criteria *crit, DetReg *image);
int ckpathname (char *newpath);
int PrintCriteria ();

int SetAltpath (FITS_DB *db, DetReg *image, int Nimage, Match *match, int Nmatch);
int ModifySubset (FITS_DB *db, DetReg *image, int Nimage, Match *match, int Nmatch);
void DeleteSubset (FITS_DB *db, DetReg *image, int Nimage, Match *match, int Nmatch);
