{-# OPTIONS -w #-}
{- Workaround.hs

This file was created automatically by configure.  We disable warnings to
avoid issues with duplicate imports.

-}

module Workaround(
           openFd,
installHandler, Handler(..), Signal,
                         sigINT, sigHUP, sigABRT, sigALRM, sigTERM, sigPIPE,
           getCurrentDirectory,
           createLink,
           createDirectoryIfMissing,
           renameFile,
           bracketOnError,
           fileMode, getFileStatus, setFileMode,
           isExecutable, setExecutable,
 ) where

import qualified GHC.Handle ( openFd )
import System.IO ( Handle, IOMode )
import System.Posix.Internals ( FDType )

import System.Posix.Signals(installHandler, Handler(..), Signal,
                         sigINT, sigHUP, sigABRT, sigALRM, sigTERM, sigPIPE,)
import System.Directory(getCurrentDirectory)
import System.Posix.Files( createLink )
import System.Directory(createDirectoryIfMissing)
import System.Directory ( renameFile )
import Control.Exception( bracketOnError )
import System.Posix.Files(fileMode,getFileStatus,setFileMode)

import System.Posix.Files (fileMode,getFileStatus, setFileMode, unionFileModes,
                           intersectFileModes, nullFileMode,
                           ownerReadMode, ownerWriteMode, ownerExecuteMode,
                           groupReadMode, groupWriteMode, groupExecuteMode,
                           otherReadMode, otherWriteMode, otherExecuteMode)
isExecutable :: FilePath -> IO Bool
isExecutable f = do st <- getFileStatus f
                    let x = fileMode st
                            `intersectFileModes` ownerExecuteMode
                    return $ x /= nullFileMode
setExecutable :: FilePath -> Bool -> IO ()
setExecutable f ex =
  do st <- getFileStatus f
     let rw = fileMode st `intersectFileModes`
              (ownerReadMode `unionFileModes` ownerWriteMode `unionFileModes`
               groupReadMode `unionFileModes` groupWriteMode `unionFileModes`
               otherReadMode `unionFileModes` otherWriteMode)
         total = if ex then rw `unionFileModes` ownerExecuteMode
                               `unionFileModes` groupExecuteMode
                               `unionFileModes` otherExecuteMode
                       else rw
     setFileMode f total


{-
Work around change in the GHC.Handle.openFd API.
-}
openFd :: Int -> Maybe FDType -> FilePath -> IOMode -> Bool -> Bool -> IO Handle
openFd fd x y z a b = GHC.Handle.openFd fd x b y z a

