{-# LANGUAGE LambdaCase #-}

module Language.EFLINT.Util where

import Control.Monad (forM)
import System.FilePath
import System.Directory

find_included_file :: [FilePath] -> FilePath -> IO [FilePath]
find_included_file :: [FilePath] -> FilePath -> IO [FilePath]
find_included_file [FilePath]
dirs FilePath
path = do 
  [[FilePath]] -> [FilePath]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[FilePath]] -> [FilePath]) -> IO [[FilePath]] -> IO [FilePath]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [FilePath] -> (FilePath -> IO [FilePath]) -> IO [[FilePath]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [FilePath]
dirs (\FilePath
dir -> do 
    let file :: FilePath
file = FilePath
dir FilePath -> FilePath -> FilePath
</> FilePath
path 
    FilePath -> IO Bool
doesFileExist FilePath
file IO Bool -> (Bool -> IO [FilePath]) -> IO [FilePath]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case Bool
True  -> [FilePath] -> IO [FilePath]
forall (m :: * -> *) a. Monad m => a -> m a
return [FilePath
file]
                                 Bool
False -> (FilePath -> IO Bool
doesFileExist (FilePath
file FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
".eflint") IO Bool -> (Bool -> IO [FilePath]) -> IO [FilePath]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case Bool
True  -> [FilePath] -> IO [FilePath]
forall (m :: * -> *) a. Monad m => a -> m a
return [FilePath
file FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
".eflint"]
                                                                                       Bool
False -> [FilePath] -> IO [FilePath]
forall (m :: * -> *) a. Monad m => a -> m a
return []))