// #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(argc < 2 || argc > 3) {
    printf("In display_signal_plot: error: wrong number of args.  Usage: display_signal_plot id [db_name]");
    exit(-1);
  }

  const char * db_name;
  if(argc >= 3)
    db_name = argv[2];
  else
    db_name = "apb_db@sci_master_tcp";

  int retval;
  ap_signal this_ap_signal;

  setenv("INFORMIXDIR", "/disks/thumper/a/apps/informix", 1);
  setenv("INFORMIXSQLHOSTS", "/disks/thumper/a/apps/informix/etc/sqlhosts", 1);
  setenv("INFORMIXSERVER", "sci_master_tcp", 1);

  retval = db_change(db_name);
  fprintf(stderr, "db_change(%s) returns %d\n<br>", db_name, retval);

  if(retval != 1) {
    fprintf(stderr, "Could not change db\n<br>");
    exit(1);
  }

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

  sprintf(c_string, "where id = %s", argv[1]); // writeout
  fprintf(stderr, "where clause: %s\n<br>", c_string);
  where_clause = c_string;
  this_ap_signal.open_query(where_clause);

  retval = this_ap_signal.get_next();
  fprintf(stderr, "retval from this_ap_signal.get_next(): %d\n<br>", retval);
  std::string wufilename = "unknown_wu";
  Astropulse::ap_signal_to_miniwufile(this_ap_signal, wufilename, 1, ""); // write out
}
