
#include "lanio.h"

long openSocket(char *hostname, long portNumber);
char *queryInstrument(FILE *file, const char *command, char *result, size_t maxLength);
int commandInstrument(FILE *file, const char *command);

int commandInstrument(FILE *file, const char *command) {

  if (fprintf(file, "%s\n", command) < 0)
    return 0;
  fflush(file);
  return 1;
}

#define BUFSIZE 1000000

#include "mdb.h"
#include "scan.h"
#include "match_string.h"

#define CLO_PAUSE 0
#define COMMANDLINE_OPTIONS 1
static char *commandline_option[COMMANDLINE_OPTIONS] = {
  "pause",
};

int main(int argc, char **argv) {
  long instrument;
  FILE *instFile;
  char charBuf[BUFSIZE], *command, *destination;
  SCANNED_ARG *s_arg;
  long i_arg;
  double pause;

  if (argc < 3) {
    fprintf(stderr, "Usage: %s <hostname> <command> [-pause=<seconds> <command> ...]\n", argv[0]);
    exit(1);
  }

  argc = scanargs(&s_arg, argc, argv);
  destination = argv[1];

  if ((instrument = openSocket(destination, 5025)) == -1)
    exit(1);

  if (!(instFile = fdopen(instrument, "r+"))) {
    fprintf(stderr, "Unable to create file * structure : %s\n",
            strerror(errno));
    exit(1);
  }

  for (i_arg = 2; i_arg < argc; i_arg++) {
    if (s_arg[i_arg].arg_type == OPTION) {
      switch (match_string(s_arg[i_arg].list[0], commandline_option,
                           COMMANDLINE_OPTIONS, 0)) {
      case CLO_PAUSE:
        if (s_arg[i_arg].n_items != 2 ||
            !sscanf(s_arg[i_arg].list[1], "%lf", &pause)) {
          bomb("bad -pause syntax (hpSocketSend)", NULL);
        }
        usleep((unsigned)(1e6 * pause));
        break;
      default:
        bomb("unknown option (hpSocketSend)", NULL);
        break;
      }
    } else {
      command = s_arg[i_arg].list[0];
      if (strchr(command, '?')) {
        printf("%s\n", queryInstrument(instFile, command,
                                       charBuf, sizeof(charBuf)));
      } else {
        commandInstrument(instFile, command);
      }
    }
  }

  return (0);
}

long openSocket(char *hostname, long portNumber) {
  struct hostent *hostPtr;

  struct sockaddr_in peeraddr_in;
  long s;

  memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
  /*    {
        long address;
        address = inet_addr((char*)hostname);
        hostPtr=gethostbyaddr((char*)&address,4,0);
        }
  */
  hostPtr = gethostbyname((char *)hostname);

  if (hostPtr == NULL) {
    fprintf(stderr, "unable to resolve hostname '%s'\n", hostname);
    return (-1);
  }
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s == -1) {
    fprintf(stderr, "unable to create socket to '%s': %s\n",
            hostname, strerror(errno));
    return (-1);
  }

  memcpy((char *)&peeraddr_in.sin_addr.s_addr, hostPtr->h_addr, hostPtr->h_length);
  peeraddr_in.sin_family = AF_INET;
  peeraddr_in.sin_port = htons(portNumber);
  if (connect(s, (struct sockaddr *)&peeraddr_in, sizeof(struct sockaddr_in)) == -1) {
    fprintf(stderr, "unable to create socket to '%s': %s\n",
            hostname, strerror(errno));
    return (-1);
  }
  return s;
}

char *queryInstrument(FILE *file, const char *command, char *result, size_t maxLength) {
  long length;

  if (fprintf(file, "%s\n", command) < 0)
    return NULL;
  fflush(file);
  rewind(file);

  if (fgets(result, maxLength, file) == NULL)
    return NULL;
  fflush(file);
  rewind(file);

  length = strlen(result);
  if (result[length - 1] == '\n')
    result[length - 1] = '\0';
  return (result);
}
