beadContextInit :: Config -> ServiceContext -> Daemons -> FilePath -> SnapletInit BeadContext BeadContext
beadContextInit config s daemons tempDir = makeSnaplet "bead" description dataDir $ do
  copyDataContext

  sm <- nestSnaplet "session" sessionManager $
          initCookieSessionManager "cookie" "session" (Just (sessionTimeout config))

  as <- nestSnaplet "auth" auth $
          initSafeJsonFileAuthManager defAuthSettings sessionManager usersJson

  ss <- nestSnaplet "context" serviceContext $ contextSnaplet s (logoutDaemon daemons)

  let dictionaryDir = "lang"
  dExist <- liftIO $ doesDirectoryExist dictionaryDir
  dictResult <- case dExist of
    True -> liftIO $ loadDictionaries dictionaryDir
    False -> return $ Right $ Map.empty

  liftIO $ putStrLn "Searching for dictionaries..."
  ds <- case dictResult of
    Left err -> error $ "ERROR: Conflicts while processing dictionaries:\n\n" ++ err
    Right dictionaries -> do
      -- TODO: Use a start logger
      liftIO $ putStrLn $ "Found dictionaries: " ++ (show $ Map.keys dictionaries)
      nestSnaplet "dictionary" dictionaryContext $
          dictionarySnaplet dictionaries (Language $ defaultLoginLanguage config)

#ifdef EmailEnabled
  se <- nestSnaplet "sendemail" sendEmailContext (emailSenderSnaplet config (emailDaemon daemons))
#endif

  rp <- nestSnaplet "randompassword" randomPasswordContext passwordGeneratorSnaplet

  fs <- nestSnaplet "fay" fayContext $ initFay

  ts <- nestSnaplet "tempdir" tempDirContext $ tempDirectorySnaplet tempDir

  cs <- nestSnaplet "config" configContext $ configurationServiceContext config

#ifndef SSO
  un <- nestSnaplet "usernamechecker" checkUsernameContext $ regexpUsernameChecker config
#endif

  timeZoneConverter <- liftIO $ createTimeZoneConverter (timeZoneInfoDirectory config)

  tz <- nestSnaplet "timezoneconveter" timeZoneContext $ createTimeZoneContext timeZoneConverter

  dl <- nestSnaplet "debuglogger" debugLoggerContext $ createDebugLogger

#ifdef SSO
  ldap <- do
    nestSnaplet "ldap-config" ldapContext $
      createLDAPContext (LDAP $ ldapDaemon daemons)
#endif

  addRoutes (routes config)

  wrapSite (<|> pages)

  return $
#ifdef SSO
#ifdef EmailEnabled
    BeadContext sm as ss ds se rp fs ts cs tz dl ldap
#else
    BeadContext sm as ss ds rp fs ts cs tz dl ldap
#endif
#else
#ifdef EmailEnabled
    BeadContext sm as ss ds se rp fs ts cs un tz dl
#else
    BeadContext sm as ss ds rp fs ts cs un tz dl
#endif
#endif
  where
    description = "The BEAD website"

    dataDir = Just referenceDataDir

-- | Creating data context in the current directory,
--   copying reference files.
beadContextInit :: Config -> ServiceContext -> Daemons -> FilePath -> SnapletInit BeadContext BeadContext
beadContextInit config s daemons tempDir = makeSnaplet "bead" description dataDir $ do
  copyDataContext

  sm <- nestSnaplet "session" sessionManager $
          initCookieSessionManager "cookie" "session" (Just (sessionTimeout config))

  as <- nestSnaplet "auth" auth $
          initSafeJsonFileAuthManager defAuthSettings sessionManager usersJson

  ss <- nestSnaplet "context" serviceContext $ contextSnaplet s (logoutDaemon daemons)

  let dictionaryDir = "lang"
  dExist <- liftIO $ doesDirectoryExist dictionaryDir
  dictResult <- case dExist of
    True -> liftIO $ loadDictionaries dictionaryDir
    False -> return $ Right $ Map.empty

  liftIO $ putStrLn "Searching for dictionaries..."
  ds <- case dictResult of
    Left err -> error $ "ERROR: Conflicts while processing dictionaries:\n\n" ++ err
    Right dictionaries -> do
      -- TODO: Use a start logger
      liftIO $ putStrLn $ "Found dictionaries: " ++ (show $ Map.keys dictionaries)
      nestSnaplet "dictionary" dictionaryContext $
          dictionarySnaplet dictionaries (Language $ defaultLoginLanguage config)

#ifdef EmailEnabled
  se <- nestSnaplet "sendemail" sendEmailContext (emailSenderSnaplet config (emailDaemon daemons))
#endif

  rp <- nestSnaplet "randompassword" randomPasswordContext passwordGeneratorSnaplet

  fs <- nestSnaplet "fay" fayContext $ initFay

  ts <- nestSnaplet "tempdir" tempDirContext $ tempDirectorySnaplet tempDir

  cs <- nestSnaplet "config" configContext $ configurationServiceContext config

#ifndef SSO
  un <- nestSnaplet "usernamechecker" checkUsernameContext $ regexpUsernameChecker config
#endif

  timeZoneConverter <- liftIO $ createTimeZoneConverter (timeZoneInfoDirectory config)

  tz <- nestSnaplet "timezoneconveter" timeZoneContext $ createTimeZoneContext timeZoneConverter

  dl <- nestSnaplet "debuglogger" debugLoggerContext $ createDebugLogger

#ifdef SSO
  ldap <- do
    nestSnaplet "ldap-config" ldapContext $
      createLDAPContext (LDAP $ ldapDaemon daemons)
#endif

  addRoutes (routes config)

  wrapSite (<|> pages)

  return $
#ifdef SSO
#ifdef EmailEnabled
    BeadContext sm as ss ds se rp fs ts cs tz dl ldap
#else
    BeadContext sm as ss ds rp fs ts cs tz dl ldap
#endif
#else
#ifdef EmailEnabled
    BeadContext sm as ss ds se rp fs ts cs un tz dl
#else
    BeadContext sm as ss ds rp fs ts cs un tz dl
#endif
#endif
  where
    description = "The BEAD website"

    dataDir = Just referenceDataDir

-- | Creating data context in the current directory,
--   copying reference files.
beadContextInit config s daemons tempDir = makeSnaplet "bead" description dataDir $ do
  copyDataContext

  sm <- nestSnaplet "session" sessionManager $
          initCookieSessionManager "cookie" "session" (Just (sessionTimeout config))

  as <- nestSnaplet "auth" auth $
          initSafeJsonFileAuthManager defAuthSettings sessionManager usersJson

  ss <- nestSnaplet "context" serviceContext $ contextSnaplet s (logoutDaemon daemons)

  let dictionaryDir = "lang"
  dExist <- liftIO $ doesDirectoryExist dictionaryDir
  dictResult <- case dExist of
    True -> liftIO $ loadDictionaries dictionaryDir
    False -> return $ Right $ Map.empty

  liftIO $ putStrLn "Searching for dictionaries..."
  ds <- case dictResult of
    Left err -> error $ "ERROR: Conflicts while processing dictionaries:\n\n" ++ err
    Right dictionaries -> do
      -- TODO: Use a start logger
      liftIO $ putStrLn $ "Found dictionaries: " ++ (show $ Map.keys dictionaries)
      nestSnaplet "dictionary" dictionaryContext $
          dictionarySnaplet dictionaries (Language $ defaultLoginLanguage config)

#ifdef EmailEnabled
  se <- nestSnaplet "sendemail" sendEmailContext (emailSenderSnaplet config (emailDaemon daemons))
#endif

  rp <- nestSnaplet "randompassword" randomPasswordContext passwordGeneratorSnaplet

  fs <- nestSnaplet "fay" fayContext $ initFay

  ts <- nestSnaplet "tempdir" tempDirContext $ tempDirectorySnaplet tempDir

  cs <- nestSnaplet "config" configContext $ configurationServiceContext config

#ifndef SSO
  un <- nestSnaplet "usernamechecker" checkUsernameContext $ regexpUsernameChecker config
#endif

  timeZoneConverter <- liftIO $ createTimeZoneConverter (timeZoneInfoDirectory config)

  tz <- nestSnaplet "timezoneconveter" timeZoneContext $ createTimeZoneContext timeZoneConverter

  dl <- nestSnaplet "debuglogger" debugLoggerContext $ createDebugLogger

#ifdef SSO
  ldap <- do
    nestSnaplet "ldap-config" ldapContext $
      createLDAPContext (LDAP $ ldapDaemon daemons)
#endif

  addRoutes (routes config)

  wrapSite (<|> pages)

  return $
#ifdef SSO
#ifdef EmailEnabled
    BeadContext sm as ss ds se rp fs ts cs tz dl ldap
#else
    BeadContext sm as ss ds rp fs ts cs tz dl ldap
#endif
#else
#ifdef EmailEnabled
    BeadContext sm as ss ds se rp fs ts cs un tz dl
#else
    BeadContext sm as ss ds rp fs ts cs un tz dl
#endif
#endif
  where
    description = "The BEAD website"

    dataDir = Just referenceDataDir

-- | Creating data context in the current directory,
--   copying reference files.