#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//#include "posix_sockets.h"
#include "opengl.h"
#include "extern_globals.h"
//extern char *Local_Port;
//extern char *Remote_Port;

extern int My_argc;
extern char **My_argv;
extern char *Port;
extern char *Remote_IP;
extern int Send_OK;
extern int IS_Server;
extern int my_turn;
extern int saved_remote;

void Usage(void){
	printf("Error: usage  ./test s <port>\n");
	printf("Error: or     ./test c <remote-ip> <port>\n");
}

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

/*	if(argc != 3 && argc != 4){
		Usage();
		exit(1);
	}
*/
	My_argc = argc;
	My_argv = argv;
	saved_remote = 0;

	Send_OK = 1;
	IS_Server = 0;
	my_turn = 1;
	pthread_t *Threads = (pthread_t *)malloc(sizeof(pthread_t) * 3);

/*
	char type[2];
	(void)strcpy(type, (const char*)argv[1]);
	if(type[0] == 's'){
		Send_OK = 0;
		IS_Server = 1;
		Port = (char *)malloc(strlen(argv[2]) + 1);
		//(void)memset(Local_Port, '\0', strlen(argv[2]) + 1);
		(void)strcpy(Port, (const char*)argv[2]);
	}else if( type[0] == 'c'){
		Send_OK = 0;
		IS_Server = 0;
		//Port = (char *)malloc(strlen(argv[2]) + 1);
		Remote_IP = (char *)malloc(strlen(argv[2]) + 1);
		Port = (char *)malloc(strlen(argv[3]) + 1);
		//(void)strcpy(Local_Port, (const char*)argv[2]);
		(void)strcpy(Remote_IP, (const char*)argv[2]);
		(void)strcpy(Port, (const char*)argv[3]);
	}else{
		Usage();
		exit(1);
	}


	pthread_t *Threads = (pthread_t *)malloc(sizeof(pthread_t) * 3);


	if (pthread_create(&Threads[0], NULL, Net_Recv, NULL) != 0){
		printf("Error: pthread_create failed for thread: %d !\n", 0);
		exit(1);
	}

	if (pthread_create(&Threads[1], NULL, Net_Send, NULL) != 0){
		printf("Error: pthread_create failed for thread: %d !\n", 1);
		exit(1);
	}
*/
	if (pthread_create(&Threads[2], NULL, Create_OpenGL, NULL) != 0){
		printf("Error: pthread_create failed for thread: %d !\n", 2);
		exit(1);
	}

/*
	if (pthread_join(Threads[0], NULL) != 0){
		printf("Error: pthread_join failed for thread: %d !\n", 0);
		exit(1);
	}
	if (pthread_join(Threads[1], NULL) != 0){
		printf("Error: pthread_join failed for thread: %d !\n", 1);
		exit(1);
	}
*/
	if (pthread_join(Threads[2], NULL) != 0){
		printf("Error: pthread_join failed for thread: %d !\n", 2);
		exit(1);
	}

	return 0;
}
