getDefaultDir :: [String] -> IO String
getDefaultDir args = do vals <- sequence $ sup:def:env:xdg:[]
                        return $ firstNotNothing vals "."
    where env = getVar "XDG_DATA_HOME" "newsbeuter"
          xdg = getVar "HOME" ".local/share/newsbeuter" >>= exists
          def = getVar "HOME" ".newsbeuter" >>= exists
          sup = return $ getArg args "dir"
          exists :: Maybe String -> IO (Maybe String)
          exists Nothing  = return Nothing
          exists (Just p) = do b <- doesDirectoryExist p
                               if b then return $ Just p 
                               else return Nothing

-- Main process ------------------------------------------------------
getDefaultDir :: [String] -> IO String
getDefaultDir args = do vals <- sequence $ sup:def:env:xdg:[]
                        return $ firstNotNothing vals "."
    where env = getVar "XDG_DATA_HOME" "newsbeuter"
          xdg = getVar "HOME" ".local/share/newsbeuter" >>= exists
          def = getVar "HOME" ".newsbeuter" >>= exists
          sup = return $ getArg args "dir"
          exists :: Maybe String -> IO (Maybe String)
          exists Nothing  = return Nothing
          exists (Just p) = do b <- doesDirectoryExist p
                               if b then return $ Just p 
                               else return Nothing

-- Main process ------------------------------------------------------
getDefaultDir args = do vals <- sequence $ sup:def:env:xdg:[]
                        return $ firstNotNothing vals "."
    where env = getVar "XDG_DATA_HOME" "newsbeuter"
          xdg = getVar "HOME" ".local/share/newsbeuter" >>= exists
          def = getVar "HOME" ".newsbeuter" >>= exists
          sup = return $ getArg args "dir"
          exists :: Maybe String -> IO (Maybe String)
          exists Nothing  = return Nothing
          exists (Just p) = do b <- doesDirectoryExist p
                               if b then return $ Just p 
                               else return Nothing

-- Main process ------------------------------------------------------