module Yi.Paths ( getEvaluatorContextFilename
, getConfigFilename
, getConfigModules
, getArticleDbFilename
, getPersistentStateFilename
, getConfigDir
, getConfigPath
, getCustomConfigPath
, getDataPath
) where
import Control.Monad.Base (MonadBase, liftBase)
import System.Directory (createDirectoryIfMissing,
doesDirectoryExist,
getAppUserDataDirectory)
import qualified System.Environment.XDG.BaseDir as XDG (getUserConfigDir, getUserDataDir)
import System.FilePath ((</>))
appUserDataCond ::(MonadBase IO m) => (String -> IO FilePath) -> m FilePath
appUserDataCond dirQuery = liftBase $
do oldDir <- getAppUserDataDirectory "yi"
newDir <- dirQuery "yi"
oldDirExists <- doesDirectoryExist oldDir
newDirExists <- doesDirectoryExist newDir
if newDirExists
then return newDir
else if oldDirExists
then return oldDir
else do createDirectoryIfMissing True newDir
return newDir
getConfigDir ::(MonadBase IO m) => m FilePath
getConfigDir = appUserDataCond XDG.getUserConfigDir
getDataDir ::(MonadBase IO m) => m FilePath
getDataDir = appUserDataCond XDG.getUserDataDir
getDataPath :: (MonadBase IO m) => FilePath -> m FilePath
getDataPath fp = fmap (</> fp) getDataDir
getConfigPath :: MonadBase IO m => FilePath -> m FilePath
getConfigPath = getCustomConfigPath getConfigDir
getCustomConfigPath :: MonadBase IO m => m FilePath -> FilePath -> m FilePath
getCustomConfigPath cd fp = (</> fp) `fmap` cd
getEvaluatorContextFilename, getConfigFilename, getConfigModules,
getArticleDbFilename, getPersistentStateFilename :: (MonadBase IO m) => m FilePath
getConfigFilename = getConfigPath "yi.hs"
getConfigModules = getConfigPath "modules"
getArticleDbFilename = getConfigPath "articles.db"
getPersistentStateFilename = getDataPath "history"
getEvaluatorContextFilename = getConfigPath $ "local" </> "Env.hs"