module Files ( File(..) , absolute , absoluteLink , filePath , filePathMaybe , find ) where import System.Exit (die) import System.Directory (doesDirectoryExist, doesFileExist, listDirectory, makeAbsolute) import System.FilePath (()) data File = File FilePath | Dir FilePath absolute :: File -> IO (FilePath) absolute file = filePath file >>= makeAbsolute absoluteLink :: FilePath -> FilePath absoluteLink ('.':path) = path absoluteLink path = "/" path filePathMaybe :: File -> IO (Maybe FilePath) filePathMaybe = filePathAux where filePathAux (File path) = ifToMaybe path <$> doesFileExist path filePathAux (Dir path) = ifToMaybe path <$> doesDirectoryExist path ifToMaybe path bool = if bool then return path else Nothing filePath :: File -> IO FilePath filePath file = filePathMaybe file >>= maybe (die $ notExist file) return where notExist (File path) = path ++ ": no such file" notExist (Dir path) = path ++ ": no such directory" find :: FilePath -> IO [FilePath] find path = filePathMaybe (Dir path) >>= maybe (return []) (fmap ((path ) <$>) . listDirectory)