module Docs.CLI.Directory where

import System.Directory (createDirectoryIfMissing, getXdgDirectory, XdgDirectory(..))
import System.FilePath


newtype AppCache = AppCache FilePath


-- | Create top directory of app cache if it doesn't exist
mkAppCacheDir :: Maybe FilePath -- ^ overwrite XDG_CACHE_HOME/haskell-docs-cli
              -> IO AppCache
mkAppCacheDir :: Maybe FilePath -> IO AppCache
mkAppCacheDir Maybe FilePath
mpath = do
  FilePath
dir <- (FilePath -> FilePath -> FilePath
</> FilePath
"html_cache") forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case Maybe FilePath
mpath of
    Just FilePath
path -> forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
path
    Maybe FilePath
Nothing -> XdgDirectory -> FilePath -> IO FilePath
getXdgDirectory XdgDirectory
XdgCache FilePath
"haskell-docs-cli"
  Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
True FilePath
dir
  return $ FilePath -> AppCache
AppCache FilePath
dir


getAppHistoryFile :: IO FilePath
getAppHistoryFile :: IO FilePath
getAppHistoryFile = do
  -- here's some discussion why history files belong in XDG_DATA_HOME:
  --   https://github.com/fish-shell/fish-shell/issues/744
  FilePath
dir <- XdgDirectory -> FilePath -> IO FilePath
getXdgDirectory XdgDirectory
XdgData FilePath
"haskell-docs-cli"
  Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
True FilePath
dir
  return (FilePath
dir FilePath -> FilePath -> FilePath
</> FilePath
"haskell-docs-cli.history")