{-# LANGUAGE CPP #-}
module IOUtil(-- ** Some utilities that are a little dirty, but not very
              getEnvi, progName, progArgs,
              -- ** Backward compatibility
              IOUtil.catch, try, getModificationTime) where

import qualified Control.Exception as E
import qualified System.Directory as D(getModificationTime)
import System.Environment(getEnv,getProgName,getArgs)
#ifdef VERSION_old_time
import Data.Time(UTCTime)
import Data.Time.Clock.POSIX(utcTimeToPOSIXSeconds)
import System.Time(ClockTime(..))
#endif
import UnsafePerformIO(unsafePerformIO)

getEnvi :: String -> Maybe String
getEnvi :: String -> Maybe String
getEnvi String
s = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall {a}. IO a -> IO (Either IOError a)
try (String -> IO String
getEnv String
s)

progName :: String
progName :: String
progName = forall a. IO a -> a
unsafePerformIO IO String
getProgName

progArgs :: [String]
progArgs :: [String]
progArgs = forall a. IO a -> a
unsafePerformIO IO [String]
getArgs

-- * GHC 6.12-7.6 compatibility
catch :: IO a -> (IOError -> IO a) -> IO a
catch = forall e a. Exception e => IO a -> (e -> IO a) -> IO a
E.catch :: IO a -> (IOError -> IO a) -> IO a
try :: IO a -> IO (Either IOError a)
try   = forall e a. Exception e => IO a -> IO (Either e a)
E.try   :: IO a -> IO (Either IOError a)

#ifdef VERSION_old_time
getModificationTime :: String -> IO ClockTime
getModificationTime String
path = forall a. ToClockTime a => a -> ClockTime
toClockTime forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` String -> IO UTCTime
D.getModificationTime String
path

class    ToClockTime a         where toClockTime :: a -> ClockTime
instance ToClockTime ClockTime where toClockTime :: ClockTime -> ClockTime
toClockTime = forall a. a -> a
id
instance ToClockTime UTCTime   where 
    toClockTime :: UTCTime -> ClockTime
toClockTime = forall a b c. (a -> b -> c) -> b -> a -> c
flip Integer -> Integer -> ClockTime
TOD Integer
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (RealFrac a, Integral b) => a -> b
floor forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Real a, Fractional b) => a -> b
realToFrac forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> POSIXTime
utcTimeToPOSIXSeconds
#else
getModificationTime path = D.getModificationTime path
#endif