#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/time.h>

#include "Syslog.h"
#include "TerrainNavClient.h"

int main(int argc, char* argv[])
{
  // Get the connection paramteres from the command line
  //
  char *host=0, *map=0, *cfg=0;
  long port=27027;
  int  mtype=0;
  int c;
  while ( (c = getopt(argc, argv, "h:p:m:t:c:")) != EOF )
  {
    if (c == 'h')
      host = strdup(optarg);
    else if (c == 'm')
      map = strdup(optarg);
    else if (c == 't')
      mtype = atoi(optarg);
    else if (c == 'c')
      cfg = strdup(optarg);
    else if (c == 'p')
      port = atol(optarg);
  }

  // Gotta have everything but the port number to connect
  //
  if (!host || !map || !cfg || (mtype != 1 && mtype != 2) )
  {
    printf("usage:\n test_client  -h host.ip  -m map.bo  -t maptype {1=DEM|2=Oct}  -c auv_specs.cfg  [-p port]\n");
    printf("example:\n");
    printf(" test_client  -h 134.89.32.110  -m Topo_Wall.bo  -t 2  -c mappinAUV_specs.cfg\n");
    return -1;
  }

  TerrainNavClient *_tercom;

  // Server will use TRN_MAPFILES environment variable on the server
  // for location of map files, so just use the filename here.
  //
  Syslog::write("TerrainAidDriver: Using TerrainNavClient at %s on port %d",
   host, port);
  Syslog::write("TerrainAidDriver: Using TerrainNavClient with map %s and config %s",
   map, cfg);

  _tercom = new TerrainNavClient(host, port, map, cfg, 2, mtype);

  // After moving is_connected() to public section, we can use this code block
  //
  if (!_tercom->is_connected())
  {
    Syslog::write("TerrainAidDriver::Not connected. See trn_server error messages...");
    return -1;
  }

  // If we reach here then we've connected
  //
  Syslog::write("TerrainAidDriver::Should be Connected to server if no error messages...");

  Syslog::write("TerrainAidDriver::Lets just set the interpret measure attitude flag to true...");
  sleep(2);
  _tercom->setInterpMeasAttitude(true);

  int fs = _tercom->getFilterState();
  Syslog::write("TerrainAid test client - filter state is %d", fs);

  Syslog::write("Done. Close the connection...");

  return 0;
}

