# include <ohana.h>
# include <dvo.h>
# include <signal.h>
# include "mysql.h"
# include "limits.h"

# define DVO_MAX_PATH 1024
# define MAX_BUFFER 0x080000

typedef struct {
  int imageID;
  unsigned int ippDetectID;
  uint64_t detectID;
  uint64_t ippObjID;
  uint64_t objID;
  int      photcode;
  unsigned int flags;
  float zp;
  float zpErr;
  float airMass;
  float expTime;
  double ra;
  double dec;
  float raErr;
  float decErr;
} Detections;

/* global variables set in parameter file */
char         ImageCat[DVO_MAX_PATH];

char        *CATDIR;
int          HOST_ID;
char        *HOSTDIR;
int          VERBOSE;

int          SAVE_REMOTE;

int          PARALLEL;
int          PARALLEL_MANUAL;
int          PARALLEL_SERIAL;

char        *TIME_START;
char        *TIME_END;

int          PHOTCODE_START;
int          PHOTCODE_END;

char        *SINGLE_CPT;

char        *RESULT_FILE;

char        *DATABASE_HOST;
char        *DATABASE_USER;
char        *DATABASE_PASS;
char        *DATABASE_NAME;

SkyRegion    UserPatch;

/***** prototypes ****/
int    main                          	  PROTO((int argc, char **argv));
       
void   GetConfig                     	  PROTO((char *config, char *field, char *format, int N, void *ptr));
void   ConfigInit                    	  PROTO((int *argc, char **argv));
       
void   usage_dvopsps                 	  PROTO(());
void   initialize_dvopsps            	  PROTO((int argc, char **argv));
int    args_dvopsps                  	  PROTO((int argc, char **argv));
       
void   usage_dvopsps_client          	  PROTO(());
void   initialize_dvopsps_client     	  PROTO((int argc, char **argv));
int    args_dvopsps_client           	  PROTO((int argc, char **argv));
       
MYSQL *mysql_dvopsps_connect              PROTO((MYSQL *mysqlBase));

int    insert_detections_dvopsps          PROTO(());
int    insert_detections_dvopsps_parallel PROTO((SkyTable *sky));
int    insert_detections_dvopsps_catalog  PROTO((Catalog *catalog, MYSQL *mysql));
      
int    insert_detections_mysql_commit     PROTO((IOBuffer *buffer, MYSQL *mysql));
int    insert_detections_mysql_value      PROTO((IOBuffer *buffer, Average *average, Measure *measure));
int    insert_detections_mysql_init       PROTO((IOBuffer *buffer));

int    insert_detections_mysql_array      PROTO((MYSQL *mysql, Detections *detections, int Ndetections));
int    insert_detections_mysql_detvalue   PROTO((IOBuffer *buffer, Detections *detection));

int    init_detections                    PROTO(());
int    append_detections_dvopsps_catalog  PROTO((Catalog *catalog));
int    save_detections_dvopsps            PROTO(());

int    insert_objects_dvopsps             PROTO(());
int    insert_objects_dvopsps_parallel    PROTO((SkyList *sky));
int    insert_objects_dvopsps_catalog     PROTO((Catalog *catalog, char *basename, MYSQL *mysql));
      				          
int    insert_objects_mysql_create_tables PROTO((char *basename, MYSQL *mysql));

int    insert_objects_mysql_commit        PROTO((IOBuffer *ave_buffer, IOBuffer *sec_buffer, MYSQL *mysql));
int    insert_objects_mysql_value         PROTO((IOBuffer *ave_buffer, IOBuffer *sec_buffer, Average *average, SecFilt *secfilt, int Nsecfilt));
int    insert_objects_mysql_init          PROTO((IOBuffer *ave_buffer, IOBuffer *sec_buffer, char *basename));

int    insert_skytable                    PROTO(());

int    insert_skytable_mysql_commit       PROTO((IOBuffer *buffer, MYSQL *mysql));
int    insert_skytable_mysql_value        PROTO((IOBuffer *buffer, SkyRegion *region));
int    insert_skytable_mysql_init         PROTO((IOBuffer *buffer));

Detections *DetectionsLoad        	  PROTO((char *filename, int *Ndetections));
int         DetectionsSave        	  PROTO((char *filename, Detections *detections, int Ndetections));
