# include <signal.h>

typedef struct {
  char *buffer;
  int   Nalloc;
  int   Nmaxread;
  int   Nextra;
  int   Nlast;
  int   Nbuffer;
} Fifo;

struct {
  int ModeSelect;    int Mode;
  int TypeSelect;    int Type;
  int CCDSelect;     int CCD;
  int FilterSelect;  char *Filter;
  int EntrySelect;   int Entry;
  int LabelSelect;   char *Label;
  int ExptimeSelect; float Exptime;
  int TimeSelect;    unsigned long Time;
  int NameSelect;    char *Name;
  int ProcSelect;    int Proc;
  int DistSelect;    int Dist;
  int Ntimes;        time_t *tstart, *tstop;
  int MatchNumber;
  int Close;
} criteria;

struct {
  int delete;
  int modify;

  int modify_path;
  char *oldpath, *newpath;

  int unique;
  int mef2split, split2mef;
  int modify_dist, dist;
  int modify_filter, modify_type, type;
  char *filter;

  int HST;
  int verbose;
  char *table;
  char *bintable;
  char *cadctable;
} output;

typedef struct {
  int *ccd;
  int Nccd;
  char name[64];
} MosaicRegion;

typedef struct {
  MosaicRegion center;
  MosaicRegion outer;
  MosaicRegion top;
  MosaicRegion bottom;
  MosaicRegion left;
  MosaicRegion right;
} MosaicLayout;

enum {UNLOCK, LOCK, IGNORE};

int SingleIsSplit;
int NeedType;
int NoReg;
int IMSORT;
int CLIENT;
char *PIDFILE;
int LOOP_DELAY;

int args (int argc, char **argv);

void DeleteSubset (FITS_DB *db, RegImage *image, int Nimage, int *match, int Nmatch);
RegImage *iminfo (char *filename);

int *match_criteria (RegImage *image, int Nimage, int *Nmatch);
int *match_images (RegImage *image, int Nimage, RegImage *subset, int Nsubset, int *Nmatch);

RegImage *newimages (RegImage *image, int *Nimage);

void ModifySubset (FITS_DB *db, RegImage *image, int Nimage, int *match, int Nmatch);
void SetOutputMode (char *mode);
void OutputSubset (RegImage *image, int Nimage, int *match, int Nmatch);
void DumpFitsBintable (char *filename, RegImage *image, int *match, int Nmatch);
void DumpFitsTable (char *filename, RegImage *image, int *match, int Nmatch);
int PrintSubset (RegImage *image, int *match, int Nmatch);
int dump_data (RegImage *image, int Nimage);
int SubmitImages (RegImage *image);
int load_probes (char *filename, unsigned long tzero, int *wantprobe, double *values, int Nprobe);
int define_table (Header *header, Matrix *matrix, Header *theader, FTable *table);
void set_timezone (double dt);

void DumpCADCTable (char *filename, RegImage *image, int *match, int Nmatch);
int *GetObsIDSubset (RegImage *image, int start, int *index, int *entry, int Nindex, int *Nsubset);
int *GetUniqueObsID (RegImage *image, int *index, int *entry, int Nindex, int *Nmatch);
void GetObsIDIndex (RegImage *image, int *match, int Nmatch, int **Index, int **Entry);
double SigmaClipList (double *list, int N);
double MosaicIQStats (RegImage *image, int *match, int Nmatch, MosaicRegion *region);
MosaicLayout *CreateCFH12K ();
MosaicLayout *CreateMegaCam ();
int GetREFCCD (RegImage *image, int *index, int *entry, int Nindex, int start);

int imregclient (char *fitsfile, char *statfile, char *datfile);
void SIG_DIE (int sig);
void SIG_PIPE (int sig);
void SetSignals ();
void KillProcess (char *pidfile);
void StatusProcess (char *pidfile);
int close_lock_db ();
int ConfigPID (char *pidfile);
int print_db_status (char *message);
int escape (int mode, char *message);

int LoadPID (char *file, pid_t *pid, char *username, char *machine);
int Shutdown (int status);
void RemovePID ();
int rconnect (char *hostname, char *command, int *rsock, int *wsock);

int InitFifo (Fifo *fifo, int Nalloc, int Nextra);
int FlushFifo (Fifo *fifo);
int ShiftFifo (Fifo *fifo);
int ReadtoFifo (Fifo *fifo, int sock);
void FreeFifo (Fifo *fifo);
int SockScan (char *string, Fifo *fifo, int sock);
int memstr (char *m1, char *m2, int n);

int *unique_entries (RegImage *image, int Nimage, int *subset, int *Nmatch);
