installLib    :: CopyFlags -- ^verbosity
              -> LocalBuildInfo
              -> FilePath  -- ^install location
              -> FilePath  -- ^install location for dynamic librarys
              -> FilePath  -- ^Build location
              -> PackageDescription -> IO ()
installLib flags lbi targetDir dynlibTargetDir builtDir
              pkg@PackageDescription{library=Just lib} =
    unless (fromFlag $ copyInPlace flags) $ do
        -- copy .hi files over:
        let verbosity = fromFlag (copyVerbosity flags)
            copy src dst n = do
              createDirectoryIfMissingVerbose verbosity True dst
              installOrdinaryFile verbosity (src </> n) (dst </> n)
            copyModuleFiles ext =
              findModuleFiles [builtDir] [ext] (libModules pkg)
                >>= installOrdinaryFiles verbosity targetDir
        ifVanilla $ copyModuleFiles "hi"
        ifProf    $ copyModuleFiles "p_hi"

        -- copy the built library files over:
        ifVanilla $ copy builtDir targetDir vanillaLibName
        ifProf    $ copy builtDir targetDir profileLibName
        ifGHCi    $ copy builtDir targetDir ghciLibName
        ifShared  $ copy builtDir dynlibTargetDir sharedLibName

        -- run ranlib if necessary:
        ifVanilla $ updateLibArchive verbosity lbi
                                     (targetDir </> vanillaLibName)
        ifProf    $ updateLibArchive verbosity lbi
                                     (targetDir </> profileLibName)

  where
    vanillaLibName = mkLibName pkgid
    profileLibName = mkProfLibName pkgid
    ghciLibName    = mkGHCiLibName pkgid
    sharedLibName  = mkSharedLibName pkgid (compilerId (compiler lbi))

    pkgid          = packageId pkg

    hasLib    = not $ null (libModules pkg)
                   && null (cSources (libBuildInfo lib))
    ifVanilla = when (hasLib && withVanillaLib lbi)
    ifProf    = when (hasLib && withProfLib    lbi)
    ifGHCi    = when (hasLib && withGHCiLib    lbi)
    ifShared  = when (hasLib && withSharedLib  lbi)
installLib    :: CopyFlags -- ^verbosity
              -> LocalBuildInfo
              -> FilePath  -- ^install location
              -> FilePath  -- ^install location for dynamic librarys
              -> FilePath  -- ^Build location
              -> PackageDescription -> IO ()
installLib flags lbi targetDir dynlibTargetDir builtDir
              pkg@PackageDescription{library=Just lib} =
    unless (fromFlag $ copyInPlace flags) $ do
        -- copy .hi files over:
        let verbosity = fromFlag (copyVerbosity flags)
            copy src dst n = do
              createDirectoryIfMissingVerbose verbosity True dst
              installOrdinaryFile verbosity (src </> n) (dst </> n)
            copyModuleFiles ext =
              findModuleFiles [builtDir] [ext] (libModules pkg)
                >>= installOrdinaryFiles verbosity targetDir
        ifVanilla $ copyModuleFiles "hi"
        ifProf    $ copyModuleFiles "p_hi"

        -- copy the built library files over:
        ifVanilla $ copy builtDir targetDir vanillaLibName
        ifProf    $ copy builtDir targetDir profileLibName
        ifGHCi    $ copy builtDir targetDir ghciLibName
        ifShared  $ copy builtDir dynlibTargetDir sharedLibName

        -- run ranlib if necessary:
        ifVanilla $ updateLibArchive verbosity lbi
                                     (targetDir </> vanillaLibName)
        ifProf    $ updateLibArchive verbosity lbi
                                     (targetDir </> profileLibName)

  where
    vanillaLibName = mkLibName pkgid
    profileLibName = mkProfLibName pkgid
    ghciLibName    = mkGHCiLibName pkgid
    sharedLibName  = mkSharedLibName pkgid (compilerId (compiler lbi))

    pkgid          = packageId pkg

    hasLib    = not $ null (libModules pkg)
                   && null (cSources (libBuildInfo lib))
    ifVanilla = when (hasLib && withVanillaLib lbi)
    ifProf    = when (hasLib && withProfLib    lbi)
    ifGHCi    = when (hasLib && withGHCiLib    lbi)
    ifShared  = when (hasLib && withSharedLib  lbi)
installLib flags lbi targetDir dynlibTargetDir builtDir
              pkg@PackageDescription{library=Just lib} =
    unless (fromFlag $ copyInPlace flags) $ do
        -- copy .hi files over:
        let verbosity = fromFlag (copyVerbosity flags)
            copy src dst n = do
              createDirectoryIfMissingVerbose verbosity True dst
              installOrdinaryFile verbosity (src </> n) (dst </> n)
            copyModuleFiles ext =
              findModuleFiles [builtDir] [ext] (libModules pkg)
                >>= installOrdinaryFiles verbosity targetDir
        ifVanilla $ copyModuleFiles "hi"
        ifProf    $ copyModuleFiles "p_hi"

        -- copy the built library files over:
        ifVanilla $ copy builtDir targetDir vanillaLibName
        ifProf    $ copy builtDir targetDir profileLibName
        ifGHCi    $ copy builtDir targetDir ghciLibName
        ifShared  $ copy builtDir dynlibTargetDir sharedLibName

        -- run ranlib if necessary:
        ifVanilla $ updateLibArchive verbosity lbi
                                     (targetDir </> vanillaLibName)
        ifProf    $ updateLibArchive verbosity lbi
                                     (targetDir </> profileLibName)

  where
    vanillaLibName = mkLibName pkgid
    profileLibName = mkProfLibName pkgid
    ghciLibName    = mkGHCiLibName pkgid
    sharedLibName  = mkSharedLibName pkgid (compilerId (compiler lbi))

    pkgid          = packageId pkg

    hasLib    = not $ null (libModules pkg)
                   && null (cSources (libBuildInfo lib))
    ifVanilla = when (hasLib && withVanillaLib lbi)
    ifProf    = when (hasLib && withProfLib    lbi)
    ifGHCi    = when (hasLib && withGHCiLib    lbi)
    ifShared  = when (hasLib && withSharedLib  lbi)