-- | Find all the per-version installation directories.
findVersions :: IO (Map.Map Version [FilePath])
findVersions = makeMap `fmap` runFinds
    [ path "/Library/Frameworks/GHC.framework/Versions"                 >>> extracts partVersion
    , path "/Library/Frameworks/HaskellPlatform.framework/lib" >>> star >>> extracts ghcVersion
    , path "/Library/Haskell"                                           >>> extracts ghcVersion
    , path "/Users" >>> star >>> path ".cabal/lib" >>> star             >>> extracts ghcVersion
    , path "/Users" >>> star >>> path ".ghc"                            >>> extracts partVersion
    , path "/Users" >>> star >>> path "Library/Haskell"                 >>> extracts ghcVersion
    , path "/usr/local/lib"                                             >>> extracts ghcVersion
    , path "/usr/local/lib" >>> matches (not . ghcName)                 >>> extracts ghcVersion
    ]
  where
    makeMap :: Ord a => [(a, b)] -> Map.Map a [b]
    makeMap = Map.fromListWith (++) . map (\(a, b) -> (a, [b]))

-- | Find all the top level installation directories. Includes some per-version
-- directories where things were stored in common system lib directories.
findVersions :: IO (Map.Map Version [FilePath])
findVersions = makeMap `fmap` runFinds
    [ path "/Library/Frameworks/GHC.framework/Versions"                 >>> extracts partVersion
    , path "/Library/Frameworks/HaskellPlatform.framework/lib" >>> star >>> extracts ghcVersion
    , path "/Library/Haskell"                                           >>> extracts ghcVersion
    , path "/Users" >>> star >>> path ".cabal/lib" >>> star             >>> extracts ghcVersion
    , path "/Users" >>> star >>> path ".ghc"                            >>> extracts partVersion
    , path "/Users" >>> star >>> path "Library/Haskell"                 >>> extracts ghcVersion
    , path "/usr/local/lib"                                             >>> extracts ghcVersion
    , path "/usr/local/lib" >>> matches (not . ghcName)                 >>> extracts ghcVersion
    ]
  where
    makeMap :: Ord a => [(a, b)] -> Map.Map a [b]
    makeMap = Map.fromListWith (++) . map (\(a, b) -> (a, [b]))

-- | Find all the top level installation directories. Includes some per-version
-- directories where things were stored in common system lib directories.
findVersions = makeMap `fmap` runFinds
    [ path "/Library/Frameworks/GHC.framework/Versions"                 >>> extracts partVersion
    , path "/Library/Frameworks/HaskellPlatform.framework/lib" >>> star >>> extracts ghcVersion
    , path "/Library/Haskell"                                           >>> extracts ghcVersion
    , path "/Users" >>> star >>> path ".cabal/lib" >>> star             >>> extracts ghcVersion
    , path "/Users" >>> star >>> path ".ghc"                            >>> extracts partVersion
    , path "/Users" >>> star >>> path "Library/Haskell"                 >>> extracts ghcVersion
    , path "/usr/local/lib"                                             >>> extracts ghcVersion
    , path "/usr/local/lib" >>> matches (not . ghcName)                 >>> extracts ghcVersion
    ]
  where
    makeMap :: Ord a => [(a, b)] -> Map.Map a [b]
    makeMap = Map.fromListWith (++) . map (\(a, b) -> (a, [b]))

-- | Find all the top level installation directories. Includes some per-version
-- directories where things were stored in common system lib directories.