{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module File (replace, convert) where

import Configuration (Configuration (..))
import Control.Monad.Reader (MonadIO (liftIO), MonadReader (ask))
import Data.Text (Text)
import qualified Data.Text as T (replace)
import Data.Text.IO (readFile, writeFile)
import Initialise (Initialise)
import Prelude hiding (readFile, writeFile)

replace :: FilePath -> Initialise ()
replace :: FilePath -> Initialise ()
replace FilePath
path = do
  -- TODO replaceWith convert
  Text
contents <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> IO Text
readFile FilePath
path
  Text
contents' <- Text -> ReaderT Configuration IO Text
convert Text
contents
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> Text -> IO ()
writeFile FilePath
path Text
contents'

convert :: Text -> Initialise Text
convert :: Text -> ReaderT Configuration IO Text
convert Text
contents = do
  Configuration {Year
FilePath
LicenseId
Text
URI
year :: Configuration -> Year
path :: Configuration -> FilePath
licence :: Configuration -> LicenseId
maintainer :: Configuration -> Text
author :: Configuration -> Text
homepage :: Configuration -> URI
name :: Configuration -> Text
year :: Year
path :: FilePath
licence :: LicenseId
maintainer :: Text
author :: Text
homepage :: URI
name :: Text
..} <- forall r (m :: * -> *). MonadReader r m => m r
ask
  forall (f :: * -> *) a. Applicative f => a -> f a
pure
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> Text -> Text
T.replace Text
"templatise" Text
name
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> Text -> Text
T.replace Text
"template-hs" Text
name
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> Text -> Text
T.replace Text
"template.hs" Text
name
    forall a b. (a -> b) -> a -> b
$ Text
contents