{-# LANGUAGE PackageImports #-}
-- |Domain independent functions used by the haskell-debian package.
module Debian.Extra.Files
    ( withTemporaryFile
    ) where

import "mtl" Control.Monad.Trans (MonadIO, liftIO)
import System.Directory (getTemporaryDirectory, removeFile)
import System.IO (hPutStr, hClose, openBinaryTempFile)

withTemporaryFile :: MonadIO m
                  => (FilePath -> m a)  -- ^ The function we want to pass a FilePath to
                  -> String             -- ^ The text that the file should contain
                  -> m a                -- ^ The function's return value
withTemporaryFile :: forall (m :: * -> *) a.
MonadIO m =>
(FilePath -> m a) -> FilePath -> m a
withTemporaryFile FilePath -> m a
f FilePath
text =
    do path <- IO FilePath -> m FilePath
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO FilePath
writeTemporaryFile
       result <- f path
       liftIO $ removeFile path
       return result
    where
      writeTemporaryFile :: IO FilePath
writeTemporaryFile =
          do dir <- IO FilePath
getTemporaryDirectory
             (path, h) <- openBinaryTempFile dir "wtf.tmp"
             hPutStr h text
             hClose h
             return path

-- Example: write the path of the temporary file and its contents into /tmp/result:
-- test =
--   withTemporaryFile f "Some text\n"
--   where f path = readFile path >>= return . (("Contents of " ++ path ++ ":\n") ++) >>= writeFile "/tmp/result"