{-|
Module: IHP.LibDir
Description: Functions to access the IHP lib/ directory at runtime
Copyright: (c) digitally induced GmbH, 2020
-}
module IHP.LibDir (findLibDirectory) where

import IHP.Prelude
import qualified IHP.EnvVar as EnvVar

-- | Finds the lib
--
-- The location depends on whether the framework is installed through nix
-- or checked out from git inside the current project directory.
--
-- When it's installed with nix, the lib dir is located at @lib/ihp@
-- while the dev server binary is located at @bin/RunDevServer@.
findLibDirectory :: IO Text
findLibDirectory :: IO Text
findLibDirectory = do
    -- The IHP_LIB env var is set in flake-module.nix
    Maybe Text
ihpLibVar <- ByteString -> IO (Maybe Text)
forall (monad :: * -> *) result.
(MonadIO monad, EnvVarReader result) =>
ByteString -> monad (Maybe result)
EnvVar.envOrNothing ByteString
"IHP_LIB"
    case Maybe Text
ihpLibVar of
        Just Text
ihpLib -> Text -> IO Text
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text
ihpLib Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/")
        Maybe Text
Nothing -> Text -> IO Text
forall a. Text -> a
error Text
"IHP_LIB env var is not set. Please run 'nix develop --impure' before running the dev server, or make sure that your direnv integration is working correctly."