-- | Downloads a file to the specified location. If the file path is Nothing,
-- the download isn't performed.
download :: Maybe FilePath -- ^ The file to save to.
            -> URL -- ^ The URL to download from.
            -> YukariSettings -> IO ()
download path url ys =
  let clobber = ys ^. siteSettings . clobberFiles
      dry = DryRun `elem` ys ^. programSettings
  in
  case path of
    Nothing -> verbPrint Low ys ["Skipping empty path."]
    Just p -> do
      b <- doesFileExist p
      when b $ verbPrint Low ys ["Skipping already downloaded", p]
      if dry
        then verbPrint Debug ys [ "Dry run enabled, would download", url
                                , "to", p, "otherwise."]
        else when (clobber || not b) $ openURI url >>= \case
          Left e -> putStrLn $ unwords [ "Error ", e
                                       , " occured when downloading from "
                                       , url]
          Right bs -> verbPrint Low ys ["Downloading ", p]
                      >> createDirectoryIfMissing True (takeDirectory p)
                      >> BS.writeFile p bs
download :: Maybe FilePath -- ^ The file to save to.
            -> URL -- ^ The URL to download from.
            -> YukariSettings -> IO ()
download path url ys =
  let clobber = ys ^. siteSettings . clobberFiles
      dry = DryRun `elem` ys ^. programSettings
  in
  case path of
    Nothing -> verbPrint Low ys ["Skipping empty path."]
    Just p -> do
      b <- doesFileExist p
      when b $ verbPrint Low ys ["Skipping already downloaded", p]
      if dry
        then verbPrint Debug ys [ "Dry run enabled, would download", url
                                , "to", p, "otherwise."]
        else when (clobber || not b) $ openURI url >>= \case
          Left e -> putStrLn $ unwords [ "Error ", e
                                       , " occured when downloading from "
                                       , url]
          Right bs -> verbPrint Low ys ["Downloading ", p]
                      >> createDirectoryIfMissing True (takeDirectory p)
                      >> BS.writeFile p bs
download path url ys =
  let clobber = ys ^. siteSettings . clobberFiles
      dry = DryRun `elem` ys ^. programSettings
  in
  case path of
    Nothing -> verbPrint Low ys ["Skipping empty path."]
    Just p -> do
      b <- doesFileExist p
      when b $ verbPrint Low ys ["Skipping already downloaded", p]
      if dry
        then verbPrint Debug ys [ "Dry run enabled, would download", url
                                , "to", p, "otherwise."]
        else when (clobber || not b) $ openURI url >>= \case
          Left e -> putStrLn $ unwords [ "Error ", e
                                       , " occured when downloading from "
                                       , url]
          Right bs -> verbPrint Low ys ["Downloading ", p]
                      >> createDirectoryIfMissing True (takeDirectory p)
                      >> BS.writeFile p bs