<commit_before>{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Control.Exception
import           Control.Concurrent
import           Data.Char (toUpper)

import           Snap hiding (Config(..))
import           System.Directory (getTemporaryDirectory, removeDirectoryRecursive)
import           System.Environment (getArgs)
import           System.IO (hFlush, hSetEcho, stdout, stdin)
import           System.IO.Temp (createTempDirectory)

import           Bead.Configuration
import qualified Bead.Controller.Logging as L
import           Bead.Controller.ServiceContext as S
import           Bead.Controller.UserStories (runUserStory, testAgentComments)
import           Bead.Daemon.EmailDaemon
import           Bead.Daemon.LogoutDaemon
import           Bead.Domain.Entities (UserRegInfo(..), TimeZone(..))
import           Bead.Persistence.Persist (initPersistence, isPersistenceSetUp)
import           Bead.View.Snap.AppInit
import           Bead.View.Snap.Logger
import           Bead.View.Snap.Validators hiding (toLower)
import           Bead.View.Snap.Translation (trans)


-- Creates a service context that includes the given logger
createContext :: L.Logger -> IO ServiceContext
createContext logger = do
  userContainer <- ioUserContainer
  isPersistSetUp <- isPersistenceSetUp
  case isPersistSetUp of
    True -> return ()
    False -> initPersistence
  S.serviceContext userContainer logger

-- Reads the command line arguments, interprets the init tasks and start
-- the service with the given config
main :: IO ()
main = do
  hSetEcho stdin True
  args <- getArgs
  config <- readConfiguration beadConfigFileName
  newAdminUser <- either (const $ return Nothing) interpretTasks (initTasks args)
  printConfigInfo config
  startService config newAdminUser

-- Prints out the actual server configuration
printConfigInfo :: Config -> IO ()
printConfigInfo cfg = do
  putStrLn $ "Log file: " ++ userActionLogFile cfg
  putStrLn $ concat ["Session timeout: ", show $ sessionTimeout cfg, " seconds"]
  putStrLn $ "Hostname included in emails: " ++ emailHostname cfg
  putStrLn $ "FROM Address included in emails: " ++ emailFromAddress cfg
  putStrLn $ "Default login language: " ++ defaultLoginLanguage cfg
  putStrLn $ "Username regular expression for the registration: " ++ usernameRegExp cfg

interpretTasks :: [InitTask] -> IO AppInitTasks
interpretTasks tasks = case elem CreateAdmin tasks of<commit_msg>CONFIG preffix for config lines at startup.<commit_after>{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Control.Exception
import           Control.Concurrent
import           Data.Char (toUpper)

import           Snap hiding (Config(..))
import           System.Directory (getTemporaryDirectory, removeDirectoryRecursive)
import           System.Environment (getArgs)
import           System.IO (hFlush, hSetEcho, stdout, stdin)
import           System.IO.Temp (createTempDirectory)

import           Bead.Configuration
import qualified Bead.Controller.Logging as L
import           Bead.Controller.ServiceContext as S
import           Bead.Controller.UserStories (runUserStory, testAgentComments)
import           Bead.Daemon.EmailDaemon
import           Bead.Daemon.LogoutDaemon
import           Bead.Domain.Entities (UserRegInfo(..), TimeZone(..))
import           Bead.Persistence.Persist (initPersistence, isPersistenceSetUp)
import           Bead.View.Snap.AppInit
import           Bead.View.Snap.Logger
import           Bead.View.Snap.Validators hiding (toLower)
import           Bead.View.Snap.Translation (trans)


-- Creates a service context that includes the given logger
createContext :: L.Logger -> IO ServiceContext
createContext logger = do
  userContainer <- ioUserContainer
  isPersistSetUp <- isPersistenceSetUp
  case isPersistSetUp of
    True -> return ()
    False -> initPersistence
  S.serviceContext userContainer logger

-- Reads the command line arguments, interprets the init tasks and start
-- the service with the given config
main :: IO ()
main = do
  hSetEcho stdin True
  args <- getArgs
  config <- readConfiguration beadConfigFileName
  newAdminUser <- either (const $ return Nothing) interpretTasks (initTasks args)
  printConfigInfo config
  startService config newAdminUser

-- Prints out the actual server configuration
printConfigInfo :: Config -> IO ()
printConfigInfo cfg = do
  configLn $ "Log file: " ++ userActionLogFile cfg
  configLn $ concat ["Session timeout: ", show $ sessionTimeout cfg, " seconds"]
  configLn $ "Hostname included in emails: " ++ emailHostname cfg
  configLn $ "FROM Address included in emails: " ++ emailFromAddress cfg
  configLn $ "Default login language: " ++ defaultLoginLanguage cfg
  configLn $ "Username regular expression for the registration: " ++ usernameRegExp cfg
  where
    configLn s = putStrLn ("CONFIG: " ++ s)

interpretTasks :: [InitTask] -> IO AppInitTasks
interpretTasks tasks = case elem CreateAdmin tasks of
{'fraction_non_alphanumeric': 0.0750805152979066, 'fraction_numerical': 0.0, 'mean_word_length': 3.385701676963813, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3462892', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1315, 'n_words': 429}