module Development.Shake.ATS.Environment ( fixDir
                                         , pkgHome
                                         , patsHome
                                         ) where

import           Data.Maybe                 (fromMaybe)
import qualified Data.Text.Lazy             as TL
import           Development.Shake
import           Development.Shake.ATS.Type
import           Development.Shake.FilePath

-- | The directory @~/.atspkg@
pkgHome :: Action String
pkgHome = fromMaybe "/usr/local/" <$> mh
    where mh = fmap (++ "/.atspkg/") <$> getEnv "HOME"

-- | The directory that will be @PATSHOME@.
patsHome :: Version -> Action String
patsHome v = fmap (++ (show v ++ "/")) pkgHome

fixDir :: FilePath -> String -> String
fixDir p =
      TL.unpack
    . TL.replace (TL.pack "./") (TL.pack $ p ++ "/")
    . TL.replace (TL.pack "../") (TL.pack $ joinPath (init $ splitPath p) ++ "/")
    . TL.pack