module Language.Dickinson.Lib ( defaultLibPath
                              , dckPath
                              ) where

import           Data.List.Split          (splitWhen)
import           Paths_language_dickinson (getDataDir)
import           System.Environment       (lookupEnv)
import           System.FilePath          ((</>))

-- | Parsed @DCK_PATH@ environment variable
dckPath :: IO [FilePath]
dckPath :: IO [FilePath]
dckPath = forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] FilePath -> [FilePath]
splitEnv forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO (Maybe FilePath)
lookupEnv FilePath
"DCK_PATH"

splitEnv :: String -> [FilePath]
splitEnv :: FilePath -> [FilePath]
splitEnv = forall a. (a -> Bool) -> [a] -> [[a]]
splitWhen (forall a. Eq a => a -> a -> Bool
== Char
':')

preludeLibPath :: FilePath -> [FilePath] -> [FilePath]
preludeLibPath :: FilePath -> [FilePath] -> [FilePath]
preludeLibPath FilePath
fp = (FilePath
preludeDir forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FilePath
libDir forall a. a -> [a] -> [a]
:)
    where preludeDir :: FilePath
preludeDir = FilePath
fp FilePath -> FilePath -> FilePath
</> FilePath
"prelude"
          libDir :: FilePath
libDir = FilePath
fp FilePath -> FilePath -> FilePath
</> FilePath
"lib"

homeMod :: IO ([FilePath] -> [FilePath])
homeMod :: IO ([FilePath] -> [FilePath])
homeMod = do
    Maybe FilePath
mHome <- FilePath -> IO (Maybe FilePath)
lookupEnv FilePath
"HOME"
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ case Maybe FilePath
mHome of
        Just FilePath
h  -> FilePath -> [FilePath] -> [FilePath]
preludeLibPath (FilePath
h FilePath -> FilePath -> FilePath
</> FilePath
".emd")
        Maybe FilePath
Nothing -> forall a. a -> a
id

defaultLibPath :: IO ([FilePath] -> [FilePath])
defaultLibPath :: IO ([FilePath] -> [FilePath])
defaultLibPath = do
    FilePath
datadir <- IO FilePath
getDataDir
    forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) (FilePath -> [FilePath] -> [FilePath]
preludeLibPath FilePath
datadir) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO ([FilePath] -> [FilePath])
homeMod