' *
 ' * This is an example showing how to get a single file from an FTP server. Example by Doyle Whisenant.
 ' *

INCLUDE "curl.bac"

' Any error will be shown
TRAP LOCAL

LOCAL curl
LOCAL res TYPE int

  'open a local file for writing
  OPEN "curl.tar.gz" FOR WRITING AS stream

  curl_global_init(CURL_GLOBAL_ALL)

  curl = curl_easy_init()
  
  IF curl ISNOT 0 THEN 
     ' *
     ' * Get curl-5.9 from sunet.se's FTP site.
     ' *
    curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.sunet.se/pub/www/utilities/curl/curl-5.9.tar.gz")
    
    ' Set a pointer to our file - pass a file pointer here!
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, stream)

    ' Switch on full protocol/debug output
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L)

    res = curl_easy_perform(curl)

    ' always cleanup
    curl_easy_cleanup(curl)
    
    'did we fail?
    IF res NE 0 THEN      
      msg$ = curl_easy_strerror(res)
      PRINT msg$
    END IF
  END IF

  'close the local file
  CLOSE FILE stream
  
  curl_global_cleanup()