import Data.Maybe ( fromMaybe )
import Data.Monoid ( Monoid(..) )
import Data.Typeable ( Typeable )
+import Paths_htsn ( getSysconfDir )
import System.Directory ( getHomeDirectory )
import System.FilePath ( (</>) )
import System.IO.Error ( catchIOError )
import System.Log ( Priority(..) )
-import Logging ( log_error )
+import Logging ( log_error ) -- Can't import report_error from Main
+import Terminal ( display_error ) -- 'cause of circular imports.
import TSN.FeedHosts ( FeedHosts(..) )
convert _ = Nothing
--- | Obtain an OptionalConfiguration from the file ".htsnrc" in the
--- user's home directory.
+-- | Obtain an OptionalConfiguration from htsnrc in either the global
+-- configuration directory or the user's home directory. The one in
+-- $HOME is prefixed by a dot so that it is hidden.
--
-- We make an attempt at cross-platform compatibility; we will try
--- to find the correct directory even on Windows. But if the call
--- to getHomeDirectory fails for whatever reason, we fall back to
--- using the environment variable $HOME.
+-- to find the correct directory even on Windows. But if the calls
+-- to getHomeDirectory/getSysconfDir fail for whatever reason, we
+-- fall back to using the Unix-specific /etc and $HOME.
--
from_rc :: IO OptionalConfiguration
from_rc = do
- -- After some thought, the "global" /etc/htsnrc configuration file
- -- was left out. Since each config file needs a password, and this
- -- should be run by a dedicated user anyway, the global file does
- -- not serve much purpose. It could also be a security risk (visible
- -- password) if the admin screws up.
+ etc <- catchIOError getSysconfDir (\e -> do
+ display_error (show e)
+ log_error (show e)
+ return "/etc")
home <- catchIOError getHomeDirectory (\e -> do
+ display_error (show e)
log_error (show e)
return "$(HOME)")
+ let global_config_path = etc </> "htsnrc"
let user_config_path = home </> ".htsnrc"
- cfg <- DC.load [ DC.Optional user_config_path ]
+ cfg <- DC.load [ DC.Optional global_config_path,
+ DC.Optional user_config_path ]
cfg_daemonize <- DC.lookup cfg "daemonize"
cfg_feed_hosts <- DC.lookup cfg "feed_hosts"
cfg_log_file <- DC.lookup cfg "log_file"