/*
 * main.c
 *
 * Author: Erich Styger
 * License: PDX-License-Identifier: BSD-3-Clause
 */

#include "main.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "delay.h"
#include "udp.h"
#include "robotNav.h"
#include "platform.h"

#define VERSION_INFO  "v1.2"

static void printHelp(void) {
  printf("Usage:\n");
  printf("  --help                          : prints this list\n");
  printf("  --udp tx <server> <port> <txt>  : send UDP datagram\n");
  printf("  --test                          : test led and navigation switch\n");
  printf("  --nav <server> <port>           : navigation with UDP server using joystick\n");
}

int main(int argc, char *argv[]) {
  int res;

  printf("UDP application, %s, use --help for a list of options\n", VERSION_INFO);
 	if (argc > 1) {
#if 0 /* debugging only */
	   int count;

	   printf ("This program was called with \"%s\".\n",argv[0]);
	   printf("nof arguments: %d\n", argc);
	   for (count = 1; count < argc; count++) {
		    printf("nof: %d, argv[%d] = %s\n", argc, count, argv[count]);
	   }
#endif
	} else {
	  printHelp();
	  return 0;
	}

 	PL_Init();
  if (argc==2 && strcmp(argv[1], "--help")==0) {
    printHelp();
  } else if (argc==6 && strcmp(argv[1], "--udp")==0 && strcmp(argv[2], "tx")==0) {
	  udp_send(argv[3], atoi(argv[4]), argv[5]);
  } else if (argc==2 && strcmp(argv[1], "--test")==0) {
    res = ROBONAV_Init();
    if (res==0) {
      ROBONAV_test();
      ROBONAV_Deinit();
    }
  } else if (argc==4 && strcmp(argv[1], "--nav")==0) {
    res = ROBONAV_Init();
    if (res==0) {
      ROBONAV_navigate(argv[2], atoi(argv[3])); /* pass server and port */
      ROBONAV_Deinit();
    }
	} else { /* unknown command */
	  printHelp();
	}
	return 0;
}
