#!/usr/bin/runghc

module Main where

import Foreign.C.Types (CInt)
import Network.Curl as Curl
import Network.Curl.Code
import Network.Curl.Easy as CurlEasy
import Network.Curl.Opts (CurlOption(..))
import System.IO

curl_print_status ptr dltotal dlnow ultotal ulnow = do
  putStrLn "Progress"
  return ((fromIntegral 0) :: CInt)

download_to_file = Curl.withCurlDo $ do
  let url = "http://ftp.us.debian.org/debian/pool/main/g/glibc/libc6_2.24-11+deb9u4_i386.deb"
  let outfile = "./libc6.deb"
  curl_inst <- CurlEasy.initialize

  r <- setopt curl_inst (CurlURL url)
  r2 <- setopt curl_inst (CurlHttpGet True)
  r3 <- setopt curl_inst (CurlProgressFunction curl_print_status)

  out <- openBinaryFile outfile WriteMode
  r4 <- setopt curl_inst (CurlWriteFunction (callbackWriter (hPutStr out)))
  
  r5 <- Curl.perform curl_inst
  hFlush out
  hClose out

main = do
  download_to_file
