//To be compiled using VS 2013

#pragma warning(disable:4996)
#include <iostream>
#include <string>
#include <ctime>
#include <conio.h>
#include <curl/curl.h>
using namespace std;

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
	size_t written = fwrite(ptr, size, nmemb, stream);
	return written;
}

void initDirs()
{
}

void downloadFile(const char* url, const char* outfilename)
{
	CURL *curl;
	FILE *fp;
	CURLcode res;
	curl = curl_easy_init();
	if (curl) {
		fp = fopen(outfilename, "wb");
		curl_easy_setopt(curl, CURLOPT_URL, url);
		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);
	}
}

int main()
{
	system("if not exist %appdata%\\VCRedistDownloader mkdir %appdata%\\VCRedistDownloader");
	const char* appdataPath = getenv("appdata");
	string appdataPaths = appdataPath;
	string fullAppdataPath = appdataPaths + "\\VCRedistDownloader\\";
	string fullAppdataPath1 = fullAppdataPath + "vcredist_x86_2005.exe";
	string fullAppdataPath2 = fullAppdataPath + "vcredist_x64_2005.exe";
	string fullAppdataPath3 = fullAppdataPath + "vcredist_x86_2008.exe";
	string fullAppdataPath4 = fullAppdataPath + "vcredist_x64_2008.exe";
	string fullAppdataPath5 = fullAppdataPath + "vcredist_x86_2010.exe";
	string fullAppdataPath6 = fullAppdataPath + "vcredist_x64_2010.exe";
	string fullAppdataPath7 = fullAppdataPath + "vcredist_x86_2012.exe";
	string fullAppdataPath8 = fullAppdataPath + "vcredist_x64_2012.exe";
	string fullAppdataPath9 = fullAppdataPath + "vcredist_x86_2013.exe";
	string fullAppdataPath10 = fullAppdataPath + "vcredist_x64_2013.exe";
	string filesURL1 = "http://hehehemlsharp.cba.pl/vcredist/vcredist_x86_2005.exe";
	string filesURL2 = "http://hehehemlsharp.cba.pl/vcredist/vcredist_x64_2005.exe";
	string filesURL3 = "http://hehehemlsharp.cba.pl/vcredist/vcredist_x86_2008.exe";
	string filesURL4 = "http://hehehemlsharp.cba.pl/vcredist/vcredist_x64_2008.exe";
	string filesURL5 = "http://hehehemlsharp.cba.pl/vcredist/vcredist_x86_2010.exe";
	string filesURL6 = "http://hehehemlsharp.cba.pl/vcredist/vcredist_x64_2010.exe";
	string filesURL7 = "http://hehehemlsharp.cba.pl/vcredist/vcredist_x86_2012.exe";
	string filesURL8 = "http://hehehemlsharp.cba.pl/vcredist/vcredist_x64_2012.exe";
	string filesURL9 = "http://hehehemlsharp.cba.pl/vcredist/vcredist_x86_2013.exe";
	string filesURL10 = "http://hehehemlsharp.cba.pl/vcredist/vcredist_x64_2013.exe";
	clock_t timer1 = clock();
	cout << "Pobieranie Visual C++ Redistributable 2005 x86... ";
	downloadFile(filesURL1.c_str(), fullAppdataPath1.c_str());
	cout << "gotowe." << endl;
	cout << "Pobieranie Visual C++ Redistributable 2005 x64... ";
	downloadFile(filesURL2.c_str(), fullAppdataPath2.c_str());
	cout << "gotowe." << endl;
	cout << "Pobieranie Visual C++ Redistributable 2008 x86... ";
	downloadFile(filesURL3.c_str(), fullAppdataPath3.c_str());
	cout << "gotowe." << endl;
	cout << "Pobieranie Visual C++ Redistributable 2008 x64... ";
	downloadFile(filesURL4.c_str(), fullAppdataPath4.c_str());
	cout << "gotowe." << endl;
	cout << "Pobieranie Visual C++ Redistributable 2010 x86... ";
	downloadFile(filesURL5.c_str(), fullAppdataPath5.c_str());
	cout << "gotowe." << endl;
	cout << "Pobieranie Visual C++ Redistributable 2010 x64... ";
	downloadFile(filesURL6.c_str(), fullAppdataPath6.c_str());
	cout << "gotowe." << endl;
	cout << "Pobieranie Visual C++ Redistributable 2012 x86... ";
	downloadFile(filesURL7.c_str(), fullAppdataPath7.c_str());
	cout << "gotowe." << endl;
	cout << "Pobieranie Visual C++ Redistributable 2012 x64... ";
	downloadFile(filesURL8.c_str(), fullAppdataPath8.c_str());
	cout << "gotowe." << endl;
	cout << "Pobieranie Visual C++ Redistributable 2013 x86... ";
	downloadFile(filesURL9.c_str(), fullAppdataPath9.c_str());
	cout << "gotowe." << endl;
	cout << "Pobieranie Visual C++ Redistributable 2013 x64... ";
	downloadFile(filesURL10.c_str(), fullAppdataPath10.c_str());
	cout << "gotowe." << endl;
	clock_t timer2 = clock();
	double timeElapsed = ((double)timer2 - (double)timer1) / CLOCKS_PER_SEC;
	cout << "Wszystkie pliki zostaly pobrane. Zajelo to " << timeElapsed << " sekund.";
	system("explorer %appdata%\\VCRedistDownloader");
	_getch();
	return 0;
}