#define CURL_STATICLIB
#include <iostream>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string>

using namespace std;

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    cout <<" DataLength :" << size*nmemb <<"\n";
    Sleep(20000);
    written = fwrite(ptr, size, nmemb, stream);
	cout <<"written:" << written <<"\n";
    return written;
}

int main(void) {

	string url = "";
	string userName = "";
	string pwd = "";
	cout <<" Enter the URL for the file you want to download:\n";
	//cin>>URL;
	getline(cin, url);
	cout <<" URL : " << url <<"\n";
	cout <<" Enter the User Name :\n";
	cin>>userName;
	cout <<" Enter the password:\n";
	cin>>pwd;
	string driveName = "";
	cout <<" Enter the drive Name inthe format of C:/ to download the File:\n";
	cin>>driveName;
	cout <<" Enter the download time out value:\n";
	int timeout = 0;
	cin>>timeout;
	string fileName = driveName;
      CURL *curl;
      FILE *fp;
      CURLcode res;
    // char *url = "http://localhost/aaa.txt";
	  string authKey = userName+":"+pwd;
	  driveName.append("test.vmdk");
	  //char outfilename[FILENAME_MAX] = ;
      curl = curl_easy_init();
      if (curl) {
		  cout <<"AAAAAAAAAAAAAAAAAAA\n";
		  fp = fopen(driveName.c_str(),"wb");
		  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
		  curl_easy_setopt(curl, CURLOPT_USERPWD,authKey.c_str()); // set credentials
		  curl_easy_setopt(curl, CURLOPT_BUFFERSIZE,131072); /*131072*/
		  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); // No Need to verify the SSL certificate
		  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
		  curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1); // no of bytes set to 1 
		  curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout);
		  curl_easy_setopt(curl, CURLOPT_IGNORE_CONTENT_LENGTH, 1);
		  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
	  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
	  curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
	  res = curl_easy_perform(curl);
	  curl_easy_cleanup(curl);
	  fclose(fp);
      }
      cout <<" BBBBBBBBBBBBBBBBBBBBBBBBB\n";
    return 0;
}