{-# LANGUAGE OverloadedStrings #-} module Main ( main ) where import System.IO (stderr) import qualified Data.Text as T import qualified Data.Text.IO as T import Date (getCurrentDate) import Birthdate (filterBirthday) import BirthdateParser (parseBirthdates) import Mail (mailSubject, mailBody) import SendMail (sendMail) import Config birthdatePath :: FilePath birthdatePath = "birthdates.csv" configPath :: FilePath configPath = "config.txt" main :: IO () main = do eitherBirthdates <- parseBirthdates <$> T.readFile birthdatePath eitherConfig <- getConfig configPath case (eitherBirthdates, eitherConfig) of (Left err, _) -> T.hPutStr stderr $ T.concat [ "Error while parsing file " , T.pack birthdatePath , ":\n" , err ] (_, Left err) -> T.hPutStr stderr $ T.concat [ "Error while parsing config file " , T.pack birthdatePath , ":\n" , err ] (Right birthdates, Right config) -> do currentDate <- getCurrentDate let birthdays = filterBirthday currentDate birthdates if not (null birthdays) then sendMail (mailTo config) (mailFrom config) (mailSubject birthdays) (mailBody currentDate birthdays) else return ()