findEmptyPackages :: VersionTest -> IO [(Bool, FilePath)]
findEmptyPackages rt = libVersions >>= fmap catMaybes . mapM willEmpty
  where
    libVersions = map snd `fmap` runFinds packageFind

    packageFind = case rt of
        VersionAll -> packagesToAlwaysCheck
        _          -> packagesToAlwaysCheck ++ packagesCoveredByAll
    packagesToAlwaysCheck =
        [ path "/usr/local/lib" >>> matches (not . ghcName) ]
    packagesCoveredByAll =
        [ path "/Library/Frameworks/HaskellPlatform.framework/lib" >>> star
        , path "/Users" >>> star >>> path ".cabal/lib" >>> star
        ]

    willEmpty :: FilePath -> IO (Maybe (Bool, FilePath))
    willEmpty fp = do
        names <- filter notDot `fmap` contents fp
        let ghcVersions = catMaybes $ map ghcVersion names
        let removingAll = all (versionTest rt) ghcVersions
        let namesLeft = filter (not . willRemove) names
        return $ if not (null ghcVersions) && removingAll
            then Just (null namesLeft, fp)
            else Nothing

    willRemove = maybe False (versionTest rt) . ghcVersion

--
-- Program Options
--
findEmptyPackages :: VersionTest -> IO [(Bool, FilePath)]
findEmptyPackages rt = libVersions >>= fmap catMaybes . mapM willEmpty
  where
    libVersions = map snd `fmap` runFinds packageFind

    packageFind = case rt of
        VersionAll -> packagesToAlwaysCheck
        _          -> packagesToAlwaysCheck ++ packagesCoveredByAll
    packagesToAlwaysCheck =
        [ path "/usr/local/lib" >>> matches (not . ghcName) ]
    packagesCoveredByAll =
        [ path "/Library/Frameworks/HaskellPlatform.framework/lib" >>> star
        , path "/Users" >>> star >>> path ".cabal/lib" >>> star
        ]

    willEmpty :: FilePath -> IO (Maybe (Bool, FilePath))
    willEmpty fp = do
        names <- filter notDot `fmap` contents fp
        let ghcVersions = catMaybes $ map ghcVersion names
        let removingAll = all (versionTest rt) ghcVersions
        let namesLeft = filter (not . willRemove) names
        return $ if not (null ghcVersions) && removingAll
            then Just (null namesLeft, fp)
            else Nothing

    willRemove = maybe False (versionTest rt) . ghcVersion

--
-- Program Options
--
findEmptyPackages rt = libVersions >>= fmap catMaybes . mapM willEmpty
  where
    libVersions = map snd `fmap` runFinds packageFind

    packageFind = case rt of
        VersionAll -> packagesToAlwaysCheck
        _          -> packagesToAlwaysCheck ++ packagesCoveredByAll
    packagesToAlwaysCheck =
        [ path "/usr/local/lib" >>> matches (not . ghcName) ]
    packagesCoveredByAll =
        [ path "/Library/Frameworks/HaskellPlatform.framework/lib" >>> star
        , path "/Users" >>> star >>> path ".cabal/lib" >>> star
        ]

    willEmpty :: FilePath -> IO (Maybe (Bool, FilePath))
    willEmpty fp = do
        names <- filter notDot `fmap` contents fp
        let ghcVersions = catMaybes $ map ghcVersion names
        let removingAll = all (versionTest rt) ghcVersions
        let namesLeft = filter (not . willRemove) names
        return $ if not (null ghcVersions) && removingAll
            then Just (null namesLeft, fp)
            else Nothing

    willRemove = maybe False (versionTest rt) . ghcVersion

--
-- Program Options
--