    module Propellor.Debug where

    import Control.Monad.IfElse
    import System.IO
    import System.Log.Logger
    import System.Log.Formatter
    import System.Log.Handler (setFormatter)
    import System.Log.Handler.Simple
    import Control.Applicative
    import Prelude

    import Utility.Monad
    import Utility.Env
    import Utility.Exception
    import Utility.Process
    import Utility.Directory

    debug :: [String] -> IO ()
    debug :: [String] -> IO ()
    debug = String -> String -> IO ()
    debugM String
    "propellor" (String -> IO ()) -> ([String] -> String) -> [String] -> IO ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [String] -> String
    unwords

    checkDebugMode :: IO ()
    checkDebugMode :: IO ()
    checkDebugMode = Maybe String -> IO ()
    go (Maybe String -> IO ()) -> IO (Maybe String) -> IO ()
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
    =<< String -> IO (Maybe String)
    getEnv String
    "PROPELLOR_DEBUG"
      where
    	go :: Maybe String -> IO ()
    go (Just String
    "1") = IO ()
    enableDebugMode
    	go (Just String
    _) = IO ()
    forall (m :: * -> *). Monad m => m ()
    noop
    	go Maybe String
    Nothing = IO Bool -> IO () -> IO ()
    forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
    whenM (String -> IO Bool
    doesDirectoryExist String
    ".git") (IO () -> IO ()) -> IO () -> IO ()
    forall a b. (a -> b) -> a -> b
    $
    		IO Bool -> IO () -> IO ()
    forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
    whenM (String -> [String] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    elem String
    "1" ([String] -> Bool) -> (String -> [String]) -> String -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> [String]
    lines (String -> Bool) -> IO String -> IO Bool
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> IO String
    getgitconfig) IO ()
    enableDebugMode
    	getgitconfig :: IO String
    getgitconfig = String -> IO String -> IO String
    forall (m :: * -> *) a. MonadCatch m => a -> m a -> m a
    catchDefaultIO String
    "" (IO String -> IO String) -> IO String -> IO String
    forall a b. (a -> b) -> a -> b
    $
    		String -> [String] -> IO String
    readProcess String
    "git" [String
    "config", String
    "propellor.debug"]

    enableDebugMode :: IO ()
    enableDebugMode :: IO ()
    enableDebugMode = do
    	f <- GenericHandler Handle
    -> LogFormatter (GenericHandler Handle) -> GenericHandler Handle
    forall a. LogHandler a => a -> LogFormatter a -> a
    setFormatter
    		(GenericHandler Handle
     -> LogFormatter (GenericHandler Handle) -> GenericHandler Handle)
    -> IO (GenericHandler Handle)
    -> IO
         (LogFormatter (GenericHandler Handle) -> GenericHandler Handle)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Handle -> Priority -> IO (GenericHandler Handle)
    streamHandler Handle
    stderr Priority
    DEBUG
    		IO (LogFormatter (GenericHandler Handle) -> GenericHandler Handle)
    -> IO (LogFormatter (GenericHandler Handle))
    -> IO (GenericHandler Handle)
    forall a b. IO (a -> b) -> IO a -> IO b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> LogFormatter (GenericHandler Handle)
    -> IO (LogFormatter (GenericHandler Handle))
    forall a. a -> IO a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (String -> LogFormatter (GenericHandler Handle)
    forall a. String -> LogFormatter a
    simpleLogFormatter String
    "[$time] $msg")
    	updateGlobalLogger rootLoggerName $
    		setLevel DEBUG .  setHandlers [f]