/*****************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * $Id: simple.c,v 1.6 2004/08/23 14:22:52 bagder Exp $
 */

#include <stdio.h>
#include <curl/curl.h>

static int pause = 0;

/* CURLOPT_WRITEFUNCTION */
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
{
  size_t realsize = size * nmemb;

  printf("write cb pause on entry: %d\n", pause);

  if(pause ^= 1)
    /* pause on every second invoke */
    return CURL_WRITEFUNC_PAUSE;

  return realsize;
}

/* CURLOPT_PROGRESSFUNCTION */
static int prog_cb (void *p, double dltotal, double dlnow, double ult,
                    double uln)
{
  static int counter;
  printf("Progress: %g / %g (%d)\n", dlnow, dltotal, counter);

  if(++counter > 4) {
    curl_easy_pause(p, CURLPAUSE_CONT); /* please continue */
    counter = 0;
  }
  return 0;
}

int main(int argc, char *argv[])
{
  CURL *curl;
  CURLcode res;

  if(argc < 2) {
    printf("Usage: prog <url>\n");
    return 1;
  }

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, prog_cb);
    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, curl);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}
