#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#include <sys/wait.h> 
// #include <sys/ddi.h>
#include <unistd.h>
#include "server.h"
#include "netutils.h"
#include "config.h"



void print_usage();
void process_server_params(int argc, char *argv[]);


int main(int argc, char *argv[])
{
  int sockfd;  /* listen on sock_fd, new connection on new_fd */

  /* Process arguments */
  process_server_params(argc, argv);

  /* Setup server */
  sockfd = open_server(config.server_port);

  /* Accept requests and respond */
  server_response_loop(sockfd, config.response_size, config.number_of_messages);

  // close server 
  if (close(sockfd) != 0) {
    fprintf(stderr, "Failed to close server\n");
  }
 
  if (config.terse_debug) {
    printf("Closed server\n");
  }

  return 0;
}

void process_server_params(int argc, char *argv[])
{
  int i = 1;

  config.server_port = DEFAULT_SERVER_PORT;

  // process options
  while(i < argc && argv[i][0] == '-') {

    if (strcmp("-verbose_debug", argv[i]) == 0) {
      config.terse_debug = TRUE;
      config.verbose_debug = TRUE;
    } else if (strcmp("-terse_debug", argv[i]) == 0 ||
	       strcmp("-debug", argv[i]) == 0) {
      config.terse_debug = TRUE;
    } else if (strcmp("-sending_verbose_stats", argv[i]) == 0) {
      config.sending_verbose_stats = TRUE;
    } else if (strcmp("-receiving_verbose_stats", argv[i]) == 0) {
      config.receiving_verbose_stats = TRUE;
    } else if (strcmp("-sport", argv[i]) == 0) {
      i++;
      config.server_port = atoi(argv[i]);
    } else {
      fprintf(stderr, "unknown argument: %s\n\n", argv[i]);
      print_usage();
    }
    i++;
  }
  
  // process mandatory arguments
  if (i != argc-2) {
    print_usage();
  }

  if (argv[i][0] == '-') {
    print_usage();
  }
  config.response_size = atoi(argv[i++]);

  if (argv[i][0] == '-') {
    print_usage();
  }
  config.number_of_messages = atoi(argv[i]);


  if (config.terse_debug) {
    fprintf(stderr, "terse_debug:        %d\n", config.terse_debug);
    fprintf(stderr, "verbose_debug:      %d\n", config.verbose_debug);
    fprintf(stderr, "sending_verbose_stats:   %d\n", config.sending_verbose_stats);
    fprintf(stderr, "receiving_verbose_stats: %d\n", config.receiving_verbose_stats);
    fprintf(stderr, "sport:               %d\n", config.server_port);
    fprintf(stderr, "response_size:      %d\n", config.response_size);
    fprintf(stderr, "number_of_messages: %d\n", config.number_of_messages);
  }
}

void print_usage()
{
  fprintf(stderr, "usage: serve [options] response_size number_of_messages\n\n");
  fprintf(stderr, "options:\n");
  fprintf(stderr, "\t%-40sDebug with lots of output\n", "-verbose_debug");
  fprintf(stderr, "\t%-40sDebug with less output\n", "-debug, terse_debug");
  fprintf(stderr, "\t%-40sPrint all sending items\n", "-sending_verbose_stats");
  fprintf(stderr, "\t%-40sPrint all receiving items\n", "-receiving_verbose_stats");
  fprintf(stderr, "\t%-40sUse specified server port\n", "-sport <port_number>");
  fprintf(stderr, "\n'0' for number_of_messages loops forever\n\n");
  exit(1);
}
