getInstalledPackages :: Verbosity -> PackageDBStack -> ProgramConfiguration
                     -> IO PackageIndex
getInstalledPackages verbosity packagedbs conf = do
  homedir       <- getHomeDirectory
  (hugsProg, _) <- requireProgram verbosity hugsProgram conf
  let hugsbindir = takeDirectory (programPath hugsProg)
      hugslibdir = takeDirectory hugsbindir </> "lib" </> "hugs"
      dbdirs = nub (concatMap (packageDbPaths homedir hugslibdir) packagedbs)
  indexes  <- mapM getIndividualDBPackages dbdirs
  return $! mconcat indexes

  where
    getIndividualDBPackages :: FilePath -> IO PackageIndex
    getIndividualDBPackages dbdir = do
      pkgdirs <- getPackageDbDirs dbdir
      pkgs    <- sequence [ getInstalledPackage pkgname pkgdir
                          | (pkgname, pkgdir) <- pkgdirs ]
      let pkgs' = map setInstalledPackageId (catMaybes pkgs)
      return (PackageIndex.fromList pkgs')
getInstalledPackages :: Verbosity -> PackageDBStack -> ProgramConfiguration
                     -> IO PackageIndex
getInstalledPackages verbosity packagedbs conf = do
  homedir       <- getHomeDirectory
  (hugsProg, _) <- requireProgram verbosity hugsProgram conf
  let hugsbindir = takeDirectory (programPath hugsProg)
      hugslibdir = takeDirectory hugsbindir </> "lib" </> "hugs"
      dbdirs = nub (concatMap (packageDbPaths homedir hugslibdir) packagedbs)
  indexes  <- mapM getIndividualDBPackages dbdirs
  return $! mconcat indexes

  where
    getIndividualDBPackages :: FilePath -> IO PackageIndex
    getIndividualDBPackages dbdir = do
      pkgdirs <- getPackageDbDirs dbdir
      pkgs    <- sequence [ getInstalledPackage pkgname pkgdir
                          | (pkgname, pkgdir) <- pkgdirs ]
      let pkgs' = map setInstalledPackageId (catMaybes pkgs)
      return (PackageIndex.fromList pkgs')
getInstalledPackages verbosity packagedbs conf = do
  homedir       <- getHomeDirectory
  (hugsProg, _) <- requireProgram verbosity hugsProgram conf
  let hugsbindir = takeDirectory (programPath hugsProg)
      hugslibdir = takeDirectory hugsbindir </> "lib" </> "hugs"
      dbdirs = nub (concatMap (packageDbPaths homedir hugslibdir) packagedbs)
  indexes  <- mapM getIndividualDBPackages dbdirs
  return $! mconcat indexes

  where
    getIndividualDBPackages :: FilePath -> IO PackageIndex
    getIndividualDBPackages dbdir = do
      pkgdirs <- getPackageDbDirs dbdir
      pkgs    <- sequence [ getInstalledPackage pkgname pkgdir
                          | (pkgname, pkgdir) <- pkgdirs ]
      let pkgs' = map setInstalledPackageId (catMaybes pkgs)
      return (PackageIndex.fromList pkgs')