{-# LANGUAGE RecordWildCards #-}

module Defaults
  ( getDefaults,
    Defaults (..),
    dName,
    dHomePage,
  )
where

import Data.Maybe (fromJust)
import Data.Text (Text, pack, unpack)
import Data.Time (LocalTime (localDay), getCurrentTime, getCurrentTimeZone, utcToLocalTime)
import Data.Time.Calendar (Year)
import Data.Time.Calendar.OrdinalDate (toOrdinalDate)
import qualified Git (config)
import Network.URI (URI (uriPath), parseURI)
import System.Directory.Extra (getCurrentDirectory)
import System.FilePath (dropExtension, takeBaseName)

data Defaults = Defaults
  { Defaults -> URI
dOrigin :: URI,
    Defaults -> Text
dAuthor :: Text,
    Defaults -> Text
dMaintainer :: Text,
    Defaults -> FilePath
dPath :: FilePath,
    Defaults -> Year
dYear :: Year
  }
  deriving (Int -> Defaults -> ShowS
[Defaults] -> ShowS
Defaults -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [Defaults] -> ShowS
$cshowList :: [Defaults] -> ShowS
show :: Defaults -> FilePath
$cshow :: Defaults -> FilePath
showsPrec :: Int -> Defaults -> ShowS
$cshowsPrec :: Int -> Defaults -> ShowS
Show, Defaults -> Defaults -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Defaults -> Defaults -> Bool
$c/= :: Defaults -> Defaults -> Bool
== :: Defaults -> Defaults -> Bool
$c== :: Defaults -> Defaults -> Bool
Eq)

dName :: Defaults -> Text
dName :: Defaults -> Text
dName Defaults {Year
FilePath
Text
URI
dYear :: Year
dPath :: FilePath
dMaintainer :: Text
dAuthor :: Text
dOrigin :: URI
dYear :: Defaults -> Year
dPath :: Defaults -> FilePath
dMaintainer :: Defaults -> Text
dAuthor :: Defaults -> Text
dOrigin :: Defaults -> URI
..} = FilePath -> Text
pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
takeBaseName forall b c a. (b -> c) -> (a -> b) -> a -> c
. URI -> FilePath
uriPath forall a b. (a -> b) -> a -> b
$ URI
dOrigin

dHomePage :: Defaults -> URI
dHomePage :: Defaults -> URI
dHomePage Defaults {Year
FilePath
Text
URI
dYear :: Year
dPath :: FilePath
dMaintainer :: Text
dAuthor :: Text
dOrigin :: URI
dYear :: Defaults -> Year
dPath :: Defaults -> FilePath
dMaintainer :: Defaults -> Text
dAuthor :: Defaults -> Text
dOrigin :: Defaults -> URI
..} =
  URI
dOrigin
    { uriPath :: FilePath
uriPath = ShowS
dropExtension forall a b. (a -> b) -> a -> b
$ URI -> FilePath
uriPath URI
dOrigin
    }

getDefaults :: IO Defaults
getDefaults :: IO Defaults
getDefaults = do
  URI
dOrigin <- forall a. HasCallStack => Maybe a -> a
fromJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Maybe URI
parseURI forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> FilePath
unpack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO Text
Git.config FilePath
"remote.origin.url"
  Text
dAuthor <- FilePath -> IO Text
Git.config FilePath
"user.name"
  Text
dMaintainer <- FilePath -> IO Text
Git.config FilePath
"user.email"
  FilePath
dPath <- IO FilePath
getCurrentDirectory
  TimeZone
timezone <- IO TimeZone
getCurrentTimeZone
  (Year
dYear, Int
_day) <- Day -> (Year, Int)
toOrdinalDate forall b c a. (b -> c) -> (a -> b) -> a -> c
. LocalTime -> Day
localDay forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimeZone -> UTCTime -> LocalTime
utcToLocalTime TimeZone
timezone forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO UTCTime
getCurrentTime
  forall (f :: * -> *) a. Applicative f => a -> f a
pure Defaults {Year
FilePath
Text
URI
dYear :: Year
dPath :: FilePath
dMaintainer :: Text
dAuthor :: Text
dOrigin :: URI
dYear :: Year
dPath :: FilePath
dMaintainer :: Text
dAuthor :: Text
dOrigin :: URI
..}