//
//  main.c
//
//  Created by hark on 2014/06/13.
//  Copyright (c) 2014 hark. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <bluetooth/bluetooth.h>

#include "bt_extern.h"
#include "inet_server.h"
#include "buffer.h"
#include "bt_main.h"

static void printusage(void)
{
  printf("bt [-h --help] [-p --port port] [-d --daemon] [-v --verbose] [-r --resampling] [-w --wait-vr] [-a --async] [-b --bufsize frames]\n");
  fflush(stdout);
}

int main(int argc, char **argv)
{

  struct option long_options[] = {
    {"async",     no_argument,              NULL, 'a'},
    {"bufsize",   required_argument,        NULL, 'b'},
    {"daemon",    no_argument,              NULL, 'd'},
    {"help",      no_argument,              NULL, 'h'},
    {"port",      required_argument,        NULL, 'p'},
    {"resampling",no_argument,              NULL, 'r'},
    {"verbose",   no_argument,              NULL, 'v'},
    {"wait-vr",   no_argument,              NULL, 'w'},
    {0, 0, 0, 0}
  };

  int opt;
  int option_index;
 
  while((opt = getopt_long(argc, argv, "dhp:rvwab:", long_options, &option_index)) != -1){
    switch(opt){
    case 'p':
      inet_port = atoi(optarg);
      break;
    case 'b':
      bt_send_size_byte = atoi(optarg) * sizeof(short int);
      break;
    case 'd':
      is_daemonic = 1;
      break;
    case 'v':
      bt_verbose = 1;
      break;
    case 'r':
      bt_resampling = 1;
      break;
    case 'w':
      bt_wait_voice_recognition = 1;
      break;
    case 'a':
      bt_async_send = 1;
      break;
    case 'h':
    case '?':
      printusage();
      exit(1);
    }
  }

  if (is_daemonic) {
    int ret = daemon(0, 0);
  }
	
  int ret;

  ret = buf_init();

  ret = bt_init();

  ret = inet_init();
	
  while(1) {
    pause();
  }

  return(0);
}
