#include <stdio.h>
#include <math.h>
#include <tmp/libpq-fe.h>

char *prog_name = "<>";

main(argc, argv)
     int argc;
     char **argv;
{
  int n_groups, n_tuples, n_fields;
  double p, di, f;
  extern char *PQhost, *PQport;
  char *dbName;

  PQhost = getenv("DATABASE_HOST");
  PQport = getenv("DATABASE_PORT");

  prog_name = argv[0];

  dbName = getenv("DATABASE_NAME");
  if (!dbName)
    dbName = getenv("USER");
  PQsetdb(dbName);
  PQexec(" ");
#ifdef USE_THREADS
  printf("*** After successfully connecting:\n");
  _libpq_dump_threads_state(0);
#endif
  PQfinish();
#ifdef USE_THREADS
  printf("*** After shutting down:\n");
  _libpq_dump_threads_state(0);
#endif

  printf("sleeping for a few seconds...\n");
  sleep(3);

  printf("trying again.\n");

  PQsetdb(dbName);
  PQexec(" ");
#ifdef USE_THREADS
  printf("*** After successfully connecting:\n");
  _libpq_dump_threads_state(0);
#endif
  PQfinish();
#ifdef USE_THREADS
  printf("*** After shutting down:\n");
  _libpq_dump_threads_state(0);
#endif

  printf("%s OK\n", prog_name);
  exit(0);
}
