#include "wget_wrapper.hpp"

#include "config.hpp"
#include "common.hpp"

#include <fstream>
#include <iostream>

using std::string;

bool WgetWrapper::download(const std::string& url, std::string& contents)
{
  Config *conf = Config::get_instance();
  string tmp_location = conf->p_temp_image_loc() + "/wget-temp";
  run::my_system_no_kill(("wget -nv '" + url + "' -O " + tmp_location).c_str());

  std::ifstream in;
  in.open(tmp_location.c_str(), std::ios::in);

  if (!in.is_open()) {
    return false;
  } else {
    
    string str;

    while (getline(in, str))
      contents += str + "\n";

    in.close();

    run::my_system_no_kill(("rm " + tmp_location).c_str());

    return true;
  }
}

void WgetWrapper::download_to_file(const std::string& url, const std::string& file)
{
  run::my_system_no_kill(("wget -nv '" + url + "' -O \"" + file + "\"").c_str());
}

