# include <ohana.h>
# include <dvo.h>
# include <signal.h>
# include <sys/time.h>
# include <time.h>
# include <zlib.h>

/* solaris requires both of these instead of ip.h:
   # include <sys/socket.h>
   # include <netinet/in.h>
*/

/* linux is happy with this, not solaris */
# include <netinet/ip.h>
# include <netdb.h>
# include <arpa/inet.h>
# include <glob.h>

int    VERBOSE;
char   CATDIR[256];
char   GSCFILE[256];
char   CATMODE[16];    /* raw, mef, split, mysql */
char   CATFORMAT[16];  /* internal, elixir, loneos, panstarrs */
char   ImageCat[256];
float  RADIUS;
int    SKY_DEPTH;

SkyRegion UserPatch;  // used by MODE CAT

typedef struct {
  off_t Nmap;
  off_t *old;
  off_t *new;
} IDmapType;

int        main                   PROTO((int argc, char **argv));
int        dvomergeCreate         PROTO((int argc, char **argv));
int        dvomergeUpdate         PROTO((int argc, char **argv));

int        ConfigInit             PROTO((int *argc, char **argv));
void       GetConfig              PROTO((char *config, char *field, char *format, int N, void *ptr));

int        SetSignals             PROTO((void));
void       SetProtect             PROTO((int mode));
void       TrapSignal             PROTO((int sig));
int        Shutdown               PROTO((char *format, ...)) OHANA_FORMAT(printf, 1, 2);

void       dvomerge_usage         PROTO((void));
void 	   dvomerge_help          PROTO((int argc, char **argv));
int  	   dvomerge_args 	  PROTO((int *argc, char **argv));

void       dvoconvert_usage       PROTO((void));
void 	   dvoconvert_help 	  PROTO((int argc, char **argv));
int  	   dvoconvert_args	  PROTO((int *argc, char **argv));

int        SkyTablePopulatedRange PROTO((off_t *ns, off_t *ne, SkyTable *sky, off_t Nstart));

SkyList   *SkyTablePopulatedList  PROTO((SkyTable *sky));
SkyList   *SkyTablePopulatedList_old  PROTO((SkyTable *sky, off_t Ns, off_t Ne));

int        LoadCatalog            PROTO((Catalog *catalog, SkyRegion *region, char *filename, char *mode));

int        merge_catalogs_new     PROTO((SkyRegion *region, Catalog *output, Catalog *input));
int        merge_catalogs_old     PROTO((SkyRegion *region, Catalog *output, Catalog *input, double RADIUS));

off_t 	  *init_measure_links     PROTO((Average *average, off_t Naverage, Measure *measure, off_t Nmeasure));
off_t 	  *init_missing_links     PROTO((Average *average, off_t Naverage, Missing *missing, off_t Nmissing));
off_t 	  *build_measure_links    PROTO((Average *average, off_t Naverage, Measure *measure, off_t Nmeasure));
int   	   add_meas_link     	  PROTO((Average *average, off_t *next, off_t Nmeasure, off_t NMEASURE));
int   	   add_miss_link     	  PROTO((Average *average, off_t *next, off_t Nmissing));
Measure   *sort_measure     	  PROTO((Average *average, off_t Naverage, Measure *measure, off_t Nmeasure, off_t *next));
Missing   *sort_missing     	  PROTO((Average *average, off_t Naverage, Missing *missing, off_t Nmissing, off_t *next_miss));

uint64_t   CreatePSPSDetectionID  PROTO((double tobs, int ccdid, int detID));
uint64_t   CreatePSPSObjectID     PROTO((double ra, double dec));

int        dvomergeImagesCreate   PROTO((IDmapType *IDmap1, char *input1, IDmapType *IDmap2, char *input2, char *output));
int        dvomergeImagesUpdate   PROTO((IDmapType *IDmap, char *input, char *output));
int        dvo_image_merge_dbs    PROTO((IDmapType *IDmap, FITS_DB *out, FITS_DB *in));
off_t 	   dvo_map_image_ID       PROTO((IDmapType *IDmap, off_t oldID));
int   	   dvo_update_image_IDs   PROTO((IDmapType *IDmap, Catalog *catalog));
