// #include "ap_config.h"

#ifdef _WIN32
#include "windows.h"
#endif

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>

// #include <ctype.h>

// #include "setilib.h"
// #include "db_table.h"
// #include "sqlhdr.h"
// #include "schema_master.h"
// #include "seti_doppler.h"

#include "parse.h"
#include "xml_util.h"
#include "db_table.h"
#include "ap_schema.h"
// #include "astropulse.h"
#include "util.h"
#include "base64.h"
#include "ap_raw_data_db.h"
#undef read

#include <cstring>
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <cerrno>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

int main(int argc, char* argv[]) {

#if 0
  char buf[16];
  sprintf(buf, "%s", std::string("").c_str());
  if(strlen(buf) == 0)
    printf("It's zero!\n");
  exit(0);
#endif

#if 0
  std::string line = "<percent_blanked>21.123456789</prcent_blanked>";
  int pos_start, pos_end;
  double sig_fraction_blanked;
  std::string fraction_blanked;

    if((pos_start = line.find("<percent_blanked>")) != std::string::npos) {
      pos_start += strlen("<percent_blanked>");
      if((pos_end = line.find("</percent_blanked>")) == std::string::npos) {
         sig_fraction_blanked = -1;
         printf("sig_fraction_blanked = %f\n", sig_fraction_blanked);
         exit(0);
      }
      fraction_blanked = line.substr(pos_start, pos_end - pos_start);
      sig_fraction_blanked = strtod(fraction_blanked.c_str(), NULL);
      printf("sig_fraction_blanked = %f\n", sig_fraction_blanked);
      exit(0);
    }

  sig_fraction_blanked = -1;
  printf("sig_fraction_blanked = %f\n", sig_fraction_blanked);
  exit(0);
#endif 

  int retval,updateretval;
  ap_signal this_ap_signal;
  const char * test_db = "apb_db@sci_master_tcp";

  retval = db_change(test_db);
  fprintf(stderr, "db_change(%s) returns %d\n", test_db, retval);

  char c_string[256];
  std::string where_clause;

#if 0
  // insertion
  sprintf(c_string, "where id = 246375");
  where_clause = c_string;
  this_ap_signal.open_query(where_clause);
  printf("Here.\n");
  retval = this_ap_signal.get_next();
  // printf("this_ap_signal.ra = %f\n", this_ap_signal.ra);
  // printf("this_ap_signal.peak_power = %f\n", this_ap_signal.peak_power);
  std::string wufilename = "testwu_20295509.wu";
  // NUM_SAMPLES_TO_COPY / 4 = num bytes to copy
  this_ap_signal.raw_data_len = (Astropulse::NUM_SAMPLES_TO_COPY / 4);
  this_ap_signal.raw_data = sqlblob<unsigned char>(Astropulse::make_raw_data_string(wufilename, 20295509)); // insertion
  printf("inserting raw_data of length %d\n", this_ap_signal.raw_data.size());
  int n = this_ap_signal.insert(); // insertion
  printf("Inserted signal %d\n", n);
#endif

  // writeout
  sprintf(c_string, "where id = 373665");
  where_clause = c_string;
  this_ap_signal.open_query(where_clause);
  // while (retval = this_ap_signal.get_next()) {
  retval = this_ap_signal.get_next();
  // printf("this_ap_signal.ra = %f\n", this_ap_signal.ra);
  // printf("this_ap_signal.peak_power = %f\n", this_ap_signal.peak_power);
  std::string wufilename = "testwu_20295509.wu";
  Astropulse::ap_signal_to_miniwufile(this_ap_signal, wufilename, 0, ""); // write out
  // }

}
