findOrphanSymlinks :: [FilePath] -> IO [FilePath]
findOrphanSymlinks removed = do
    pathDirs <- (maybe [] (parts ':') . lookup "PATH") `fmap` getEnvironment
    let placesToLook =
            map path (pathDirs ++ [ "/usr/bin", "/usr/local/bin" ])
            ++ [ path "/Users" >>> star >>> path "Library/Haskell/bin" ]
    (nub . map snd) `fmap` runFinds
        (map (\p -> p >>> star >>> sym >>> test orphan) placesToLook)
  where
    sym :: Find a FilePath
    sym = fileExtract $ const $ \fp -> do
        st <- getSymbolicLinkStatus fp
        if isSymbolicLink st
            then (Just . simplifyPath . (takeDirectory fp </>))
                    `fmap` readSymbolicLink fp
            else return Nothing
    orphan fp = any (`isPrefixOf` fp) removed

-- | Find all package directories where removing the per-version directory
-- might indicate that the whole package can be removed.
findOrphanSymlinks :: [FilePath] -> IO [FilePath]
findOrphanSymlinks removed = do
    pathDirs <- (maybe [] (parts ':') . lookup "PATH") `fmap` getEnvironment
    let placesToLook =
            map path (pathDirs ++ [ "/usr/bin", "/usr/local/bin" ])
            ++ [ path "/Users" >>> star >>> path "Library/Haskell/bin" ]
    (nub . map snd) `fmap` runFinds
        (map (\p -> p >>> star >>> sym >>> test orphan) placesToLook)
  where
    sym :: Find a FilePath
    sym = fileExtract $ const $ \fp -> do
        st <- getSymbolicLinkStatus fp
        if isSymbolicLink st
            then (Just . simplifyPath . (takeDirectory fp </>))
                    `fmap` readSymbolicLink fp
            else return Nothing
    orphan fp = any (`isPrefixOf` fp) removed

-- | Find all package directories where removing the per-version directory
-- might indicate that the whole package can be removed.
findOrphanSymlinks removed = do
    pathDirs <- (maybe [] (parts ':') . lookup "PATH") `fmap` getEnvironment
    let placesToLook =
            map path (pathDirs ++ [ "/usr/bin", "/usr/local/bin" ])
            ++ [ path "/Users" >>> star >>> path "Library/Haskell/bin" ]
    (nub . map snd) `fmap` runFinds
        (map (\p -> p >>> star >>> sym >>> test orphan) placesToLook)
  where
    sym :: Find a FilePath
    sym = fileExtract $ const $ \fp -> do
        st <- getSymbolicLinkStatus fp
        if isSymbolicLink st
            then (Just . simplifyPath . (takeDirectory fp </>))
                    `fmap` readSymbolicLink fp
            else return Nothing
    orphan fp = any (`isPrefixOf` fp) removed

-- | Find all package directories where removing the per-version directory
-- might indicate that the whole package can be removed.