# include <stdio.h>
# include <math.h>
# include <skycalc_internal.h>

void set_site (double *longit, double *lat, double *elevsea, double *elev) {

  *longit = 10.36478; /*  W longitude in decimal hours */                     
  *lat = 19.8267;     /*  N latitude in decimal degrees */                    
  *elevsea = 4215.;   /* elevation above sea level (for absolute location) */ 
  *elev = 4215.;      /* observatory elevation above horizon, meters */       

}

main (int argc, char **argv) {

  struct SC_date_time date, tmpdate;
  double longit, lat, elevsea, elev;
  double jdnow, jdset, jdrise, d1, d2;
  unsigned long int tzero;
  struct tm *stm;

  if (argc != 2) {
    fprintf (stderr, "USAGE: dusktime (date)\n");
    exit (1);
  }

  if (!str_to_time (argv[1], &tzero)) { 
    fprintf (stderr, "syntax error\n");
    exit (1);
  }
  stm = gmtime (&tzero);
  date.y  = (short) (stm->tm_year + 1900);
  date.mo = (short) (stm->tm_mon + 1);
  date.d  = (short) (stm->tm_mday);
  date.h  = (short) (stm->tm_hour);
  date.mn = (short) (stm->tm_min);
  date.s  = (float) (stm->tm_sec);

  /*
  fprintf (stderr, "%4d/%02d/%02d %02d:%02d:%02f\n", date.y, date.mo, date.d, date.h, date.mn, date.s);
  */

  set_site (&longit, &lat, &elevsea, &elev);

  jdnow  = date_to_jd (date);
  jdset  = sunset_tonight (date, lat, longit, elev);
  jdrise = sunrise_tonight (date, lat, longit, elev);


  d1 = fabs (jdnow - jdset);
  d2 = fabs (jdnow - jdrise);

  if (d1 < d2) {
    fprintf (stdout, "set %f  %f\n", jdset, 24*60*(jdnow - jdset));
    jdnow = jdset;
  } else {
    fprintf (stdout, "rise %f  %f\n", jdrise, 24*60*(jdrise - jdnow));
    jdnow = jdrise;
  }    

  /*
  jd_to_date (jdnow, &tmpdate);
  fprintf (stderr, "%4d/%02d/%02d %02d:%02d:%02f\n", tmpdate.y, tmpdate.mo, tmpdate.d, tmpdate.h, tmpdate.mn, tmpdate.s);
  */
  exit (0);

}

  /* set_zenith (date, lat, longit, objepoch, &objra, &objdec); */
