module Lambdabot.File
( stateDir
, findLBFileForReading
, findLBFileForWriting
, findOrCreateLBFile
, findLBFile
, outputDir
) where
import Lambdabot.Config
import Lambdabot.Config.Core
import Lambdabot.Monad
import Lambdabot.Util
import Control.Applicative
import Control.Monad
import System.Directory
import System.FilePath
lambdabot :: FilePath
lambdabot :: FilePath
lambdabot = FilePath
".lambdabot"
stateDir :: LB FilePath
stateDir :: LB FilePath
stateDir = do
output <- Config FilePath -> LB FilePath
forall a. Config a -> LB a
forall (m :: * -> *) a. MonadConfig m => Config a -> m a
getConfig Config FilePath
outputDir
b <- io $ doesDirectoryExist output
if b then return output else homeDir
homeDir :: LB FilePath
homeDir :: LB FilePath
homeDir = do
output <- Config FilePath -> LB FilePath
forall a. Config a -> LB a
forall (m :: * -> *) a. MonadConfig m => Config a -> m a
getConfig Config FilePath
outputDir
home <- io getHomeDirectory
return $ home </> lambdabot </> output
findLBFileForReading :: FilePath -> LB (Maybe FilePath)
findLBFileForReading :: FilePath -> LB (Maybe FilePath)
findLBFileForReading FilePath
f = do
state <- LB FilePath
stateDir
home <- homeDir
output <- getConfig outputDir
rodir <- getConfig dataDir
findFirstFile [state </> f, home </> f, rodir </> output </> f]
findLBFileForWriting :: FilePath -> LB FilePath
findLBFileForWriting :: FilePath -> LB FilePath
findLBFileForWriting FilePath
f = do
state <- LB FilePath
stateDir
io $ createDirectoryIfMissing True state
success <- io $ doesDirectoryExist state
when (not success) $ fail $ concat ["Unable to create directory ", state]
return $ state </> f
findFirstFile :: [FilePath] -> LB (Maybe FilePath)
findFirstFile :: [FilePath] -> LB (Maybe FilePath)
findFirstFile [] = Maybe FilePath -> LB (Maybe FilePath)
forall a. a -> LB a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FilePath
forall a. Maybe a
Nothing
findFirstFile (FilePath
path:[FilePath]
ps) = do
b <- IO Bool -> LB Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO Bool -> LB Bool) -> IO Bool -> LB Bool
forall a b. (a -> b) -> a -> b
$ FilePath -> IO Bool
doesFileExist FilePath
path
if b then return (Just path) else findFirstFile ps
{-# DEPRECATED findLBFile
"Use `findLBFileForReading` or `findLBFileForWriting` instead" #-}
findLBFile :: FilePath -> LB (Maybe String)
findLBFile :: FilePath -> LB (Maybe FilePath)
findLBFile FilePath
f = do
state <- LB FilePath
stateDir
home <- homeDir
findFirstFile [state </> f, home </> f]
findOrCreateLBFile :: FilePath -> LB String
findOrCreateLBFile :: FilePath -> LB FilePath
findOrCreateLBFile FilePath
f = do
outFile <- FilePath -> LB FilePath
findLBFileForWriting FilePath
f
b <- io $ doesFileExist outFile
when (not b) $ do
b <- findLBFileForReading f
case b of
Maybe FilePath
Nothing -> IO () -> LB ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> LB ()) -> IO () -> LB ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> IO ()
writeFile FilePath
outFile FilePath
""
Just FilePath
roFile -> IO () -> LB ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> LB ()) -> IO () -> LB ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> IO ()
copyFile FilePath
roFile FilePath
outFile
return outFile