#include <sys/select.h>
#include <stdio.h>

#include <curl/curl.h>

int main ()
{

  CURLM * mhdl;
  CURL * ehdl;

  CURLMsg * msg;

  
  const char* url1 = "tftp://172.25.128.107/test1";
  const char* url2 = "tftp://172.25.128.107/test2";


  const char* fpath1 = "/tmp/test1";
  const char* fpath2 = "/tmp/test2";
  FILE* file1;
  FILE* file2;
  

  
  int rh;
  int rc;
  int rm;
  



    
  int maxfd;
  fd_set fdread, fdwrite, fdexcep;
  struct timeval fdtimeout;
  

 

  // curl global init

  if (curl_global_init(CURL_GLOBAL_NOTHING)) {
    printf("main: error curl_global_init()\n");
  }

  if ((mhdl = curl_multi_init()) == NULL) {
    printf("main: error in curl_multi_init()\n");
  }


  //handler 1 init

  if ((file1 = fopen(fpath1,"w+")) == NULL) {
    printf("main error fopen file1\n");
    exit(1);
   
  }

  if ((ehdl = curl_easy_init()) == NULL) {
    printf("main: error in curl_easy_init()\n");
  }

  if (curl_easy_setopt(ehdl,CURLOPT_URL,url1)) {
    printf("main: error in curl_easy_setopt() URL\n");
  }

        
  if (curl_easy_setopt(ehdl,CURLOPT_WRITEFUNCTION,NULL)) {
    printf("main: error in curl_easy_setopt() DATA WRITER\n");
  }

  
  if (curl_easy_setopt(ehdl,CURLOPT_WRITEDATA,file1)) {
    printf("main: error in curl_easy_setopt() DATA\n");
  }


  // add first handler to multi

  if (curl_multi_add_handle(mhdl,ehdl)) {
    printf("main: error in curl_multi_add_handle()\n");
  }



  //handler 2 init

  
  if ((file2 = fopen(fpath2,"w+")) == NULL) {
    printf("main error fopen file1\n");
    exit(1);
    
  }

  if ((ehdl = curl_easy_init()) == NULL) {
    printf("main: error in curl_easy_init()\n");
  }

  if (curl_easy_setopt(ehdl,CURLOPT_URL,url2)) {
    printf("main: error in curl_easy_setopt() URL\n");
  }

        
  if (curl_easy_setopt(ehdl,CURLOPT_WRITEFUNCTION,NULL)) {
    printf("main: error in curl_easy_setopt() DATA WRITER\n");
  }

  
  if (curl_easy_setopt(ehdl,CURLOPT_WRITEDATA,file2)) {
    printf("main: error in curl_easy_setopt() DATA\n");
  }


  

  FD_ZERO(&fdread);
  FD_ZERO(&fdwrite);
  FD_ZERO(&fdexcep);

  fdtimeout.tv_sec  = 1; //timeout_ms / 1000; //FIXME
  fdtimeout.tv_usec = 0;


  rh = 1;
  rm = 1;
  int aa = 1;



  
  int once=1;
  while (rh) {
    if (CURLM_CALL_MULTI_PERFORM == curl_multi_perform (mhdl, &rh))
      printf("Performing CURLM_CALL_MULTI_PERFORM %d\n",rh);
    else {
      printf("CURLM_OK %d\n",rh);      
    }
    



    while ((msg = curl_multi_info_read(mhdl, &rm)) != 0
           ) {

    if (once) {
      
      printf("add second job");
      if (curl_multi_add_handle(mhdl,ehdl)) {
        printf("main: error in curl_multi_add_handle()\n");
      }
      rh++;
      once = 0;
    }

    
      printf("More %d messages to read\n",rm);
      printf("FTP Got message !!!:"
             "errnum %s "
             "msg %d "
             "res %d"
             "\n",
             curl_easy_strerror (msg->data.result),
             msg->msg,
             msg->data.result
             );


      
      switch (msg->msg) {
      case CURLMSG_DONE:
        {
          
          
          switch (msg->data.result) {
          case CURLE_OK: // job done
            {
              printf("Job done %d\n",msg->data.result);
            }
            break;
          default:
            printf ("FTP session ERROR: %s \n",
                    curl_easy_strerror (msg->data.result));
          }
          break;
          
        }
        break;
      default:
        printf("FTP Bad message code %d\n", msg->msg);
      }
    }
    


    
    if (curl_multi_fdset (mhdl, &fdread, &fdwrite, &fdexcep, &maxfd)) {
      printf("main: error in curl_multi_fdset()\n");
      exit(1);
    

    }
  
    if (maxfd != -1)
      {
        printf("Going through select\n");
      
        rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &fdtimeout);
      
        switch(rc) {
        case -1:
          /* select error */
          break;
        case 0:
        default:
          {
            printf("we had data from select or timeout");
          }
          break;
        }
      }
    else {
      printf("No file descriptor from curl_multi_fdset()\n");    
    }
  
  }


  curl_multi_cleanup(mhdl);
  
  
  fclose(file1);
  
}
