#include <curl/curl.h>
#include <string.h>
#include <vdr/tools.h>

#define USERAGENTSTRING "libcurl"

int isUrl(const char*url)
{
 return strstr(url,"http://")!=NULL || strstr(url,"https://")!=NULL;
}



int curl_download(const char *URL, const char *filename)
{
    CURL *curl;
    CURLcode res;
    int returnFlag = 1;

    curl = curl_easy_init ();
    if (curl)
    {
        FILE *fp = fopen (filename, "w"); // should overwrite file
        curl_easy_setopt (curl, CURLOPT_URL, URL);
        curl_easy_setopt (curl, CURLOPT_USERAGENT, USERAGENTSTRING);
        curl_easy_setopt (curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform (curl);
        if (res)
        {
            returnFlag = 0;
        }

        fclose (fp);
    }
    /* always cleanup */
    curl_easy_cleanup (curl);
    esyslog("downloading %s to %s : success? %d ", URL, filename, returnFlag);
    return returnFlag;
}


