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

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

enum {SQUARES, TRIANGLES};

typedef struct {
  double x, y, z;
} Point;

typedef struct {
  Point vertex[3];	      // triangle vertices (3d)
  Point center;		      // triangle center (3d)
  double r, d;		      // triangle center (2d)
  double rv[3], dv[3];	      // triangle center (2d)
} SkyTriangle;

/* globals which define database info / data sources (KEEP) */
char   ImageCat[256];
char   GSCFILE[256];
char   CATDIR[256];
char   CATMODE[16];    /* raw, mef, split, mysql */
char   CATFORMAT[16];  /* internal, elixir, loneos, panstarrs */
char   PASSWORD[80];
char   HOSTNAME[80];
int    NVALID, *VALID_IP;
char   SKY_TABLE[256];
int    SKY_DEPTH;  /** XXX EAM : depth of catalog tables, fix usage */
char   CameraLayout[256];
SkyTable *ServerSky;

/* used to select entries from header (gstars or parse_time) (KEEP) */
char   DateKeyword[64];
char   DateMode[64];
char   UTKeyword[64];
char   MJDKeyword[64];
char   JDKeyword[64];
char   ExptimeKeyword[64];
char   AirmassKeyword[64];
char   CCDNumKeyword[64];
char   STKeyword[64];

int    VERBOSE;
int    MODE;
int    NMAX;
int    NX_SUB, NY_SUB;
double SCALE;

void       SetProtect             PROTO((int mode));
int        SetSignals             PROTO(());
int        Shutdown               PROTO((char *message, ...); ) 
void       TrapSignal             PROTO((int sig));
int args_skycells (int argc, char **argv);
int ConfigInit_skycells (int *argc, char **argv);

int sky_triangle_to_image (Image *image, SkyTriangle *triangle);
int sky_triangle_coords (SkyTriangle *triangle);
SkyTriangle *sky_divide_triangles (SkyTriangle *in, int *ntriangles);
Point sky_divide_edge (Point v1, Point v2);
SkyTriangle *sky_base_triangles (int *ntriangles);
int sky_tessalation_init (double scale);
int sky_triangle_to_rectangle (Image *image, SkyTriangle *triangle);

int sky_tessalation (FITS_DB *db, int level, int Nmax, int mode, double scale);
int sky_tessalation_triangles (FITS_DB *db, int level, int Nmax);
int sky_tessalation_squares (FITS_DB *db, int level, int Nmax);

// migrate to libdvo eventually
int dvo_image_clear_vtable (FITS_DB *db);
