-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Simple.Install
-- Copyright   :  Isaac Jones 2003-2004
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- This is the entry point into installing a built package. Performs the
-- \"@.\/setup install@\" and \"@.\/setup copy@\" actions. It moves files into
-- place based on the prefix argument. It does the generic bits and then calls
-- compiler-specific functions to do the rest.

{- All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}

module Distribution.Simple.Install (
        install,
  ) where

import Distribution.PackageDescription (
        PackageDescription(..), BuildInfo(..), Library(..),
        hasLibs, withLib, hasExes, withExe )
import Distribution.Package (Package(..))
import Distribution.Simple.LocalBuildInfo (
        LocalBuildInfo(..), InstallDirs(..), absoluteInstallDirs,
        substPathTemplate)
import Distribution.Simple.BuildPaths (haddockName, haddockPref)
import Distribution.Simple.Utils
         ( createDirectoryIfMissingVerbose, installDirectoryContents
         , installOrdinaryFile, die, info, notice, matchDirFileGlob )
import Distribution.Simple.Compiler
         ( CompilerFlavor(..), compilerFlavor )
import Distribution.Simple.Setup (CopyFlags(..), CopyDest(..), fromFlag)

import qualified Distribution.Simple.GHC  as GHC
import qualified Distribution.Simple.NHC  as NHC
import qualified Distribution.Simple.JHC  as JHC
import qualified Distribution.Simple.LHC  as LHC
import qualified Distribution.Simple.Hugs as Hugs
import qualified Distribution.Simple.UHC  as UHC

import Control.Monad (when, unless)
import System.Directory
         ( doesDirectoryExist, doesFileExist )
import System.FilePath
         ( takeFileName, takeDirectory, (</>), isAbsolute )

import Distribution.Verbosity
import Distribution.Text
         ( display )

-- |Perform the \"@.\/setup install@\" and \"@.\/setup copy@\"
-- actions.  Move files into place based on the prefix argument.  FIX:
-- nhc isn't implemented yet.

install :: PackageDescription -- ^information from the .cabal file
        -> LocalBuildInfo -- ^information from the configure step
        -> CopyFlags -- ^flags sent to copy or install
        -> IO ()
install pkg_descr lbi flags = do
  let distPref  = fromFlag :: Flag a -> afromFlag (copyDistPref :: CopyFlags -> Flag FilePathcopyDistPref flags :: CopyFlagsflags)
      verbosity = fromFlag :: Flag a -> afromFlag (copyVerbosity :: CopyFlags -> Flag VerbositycopyVerbosity flags :: CopyFlagsflags)
      copydest  = fromFlag :: Flag a -> afromFlag (copyDest :: CopyFlags -> Flag CopyDestcopyDest flags :: CopyFlagsflags)
      installDirs@(InstallDirs {
         bindir     = binPref,
         libdir     = libPref,
--         dynlibdir  = dynlibPref, --see TODO below
         datadir    = dataPref,
         progdir    = progPref,
         docdir     = docPref,
         htmldir    = htmlPref,
         haddockdir = interfacePref,
         includedir = incPref})
             = absoluteInstallDirs ::
  PackageDescription
  -> LocalBuildInfo
  -> CopyDest
  -> InstallDirs FilePathabsoluteInstallDirs pkg_descr :: PackageDescriptionpkg_descr lbi :: LocalBuildInfolbi copydest :: CopyDestcopydest

      --TODO: decide if we need the user to be able to control the libdir
      -- for shared libs independently of the one for static libs. If so
      -- it should also have a flag in the command line UI
      -- For the moment use dynlibdir = libdir
      dynlibPref = libPref :: FilePathlibPref
      progPrefixPref = substPathTemplate ::
  PackageId -> LocalBuildInfo -> PathTemplate -> FilePathsubstPathTemplate (packageId :: Package pkg => pkg -> PackageIdentifierpackageId pkg_descr :: PackageDescriptionpkg_descr) lbi :: LocalBuildInfolbi (progPrefix :: LocalBuildInfo -> PathTemplateprogPrefix lbi :: LocalBuildInfolbi)
      progSuffixPref = substPathTemplate ::
  PackageId -> LocalBuildInfo -> PathTemplate -> FilePathsubstPathTemplate (packageId :: Package pkg => pkg -> PackageIdentifierpackageId pkg_descr :: PackageDescriptionpkg_descr) lbi :: LocalBuildInfolbi (progSuffix :: LocalBuildInfo -> PathTemplateprogSuffix lbi :: LocalBuildInfolbi)

  docExists <- doesDirectoryExist :: FilePath -> IO BooldoesDirectoryExist ($) :: (a -> b) -> a -> b$ haddockPref :: FilePath -> PackageDescription -> FilePathhaddockPref distPref :: FilePathdistPref pkg_descr :: PackageDescriptionpkg_descr
  info :: Verbosity -> String -> IO ()info verbosity :: Verbosityverbosity ("directory " (++) :: [a] -> [a] -> [a]++ haddockPref :: FilePath -> PackageDescription -> FilePathhaddockPref distPref :: FilePathdistPref pkg_descr :: PackageDescriptionpkg_descr (++) :: [a] -> [a] -> [a]++
                  " does exist: " (++) :: [a] -> [a] -> [a]++ show :: Show a => a -> Stringshow docExists :: BooldocExists)

  installDataFiles ::
  Verbosity -> PackageDescription -> FilePath -> IO ()installDataFiles verbosity :: Verbosityverbosity pkg_descr :: PackageDescriptionpkg_descr dataPref :: FilePathdataPref

  when :: Monad m => Bool -> m () -> m ()when docExists :: BooldocExists ($) :: (a -> b) -> a -> b$ do
      createDirectoryIfMissingVerbose ::
  Verbosity -> Bool -> FilePath -> IO ()createDirectoryIfMissingVerbose verbosity :: Verbosityverbosity True :: BoolTrue htmlPref :: FilePathhtmlPref
      installDirectoryContents ::
  Verbosity -> FilePath -> FilePath -> IO ()installDirectoryContents verbosity :: Verbosityverbosity
          (haddockPref :: FilePath -> PackageDescription -> FilePathhaddockPref distPref :: FilePathdistPref pkg_descr :: PackageDescriptionpkg_descr) htmlPref :: FilePathhtmlPref
      -- setPermissionsRecursive [Read] htmlPref
      -- The haddock interface file actually already got installed
      -- in the recursive copy, but now we install it where we actually
      -- want it to be (normally the same place). We could remove the
      -- copy in htmlPref first.
      let haddockInterfaceFileSrc  = haddockPref :: FilePath -> PackageDescription -> FilePathhaddockPref distPref :: FilePathdistPref pkg_descr :: PackageDescriptionpkg_descr
                                                   (</>) :: FilePath -> FilePath -> FilePath</> haddockName :: PackageDescription -> FilePathhaddockName pkg_descr :: PackageDescriptionpkg_descr
          haddockInterfaceFileDest = interfacePref :: FilePathinterfacePref (</>) :: FilePath -> FilePath -> FilePath</> haddockName :: PackageDescription -> FilePathhaddockName pkg_descr :: PackageDescriptionpkg_descr
      -- We only generate the haddock interface file for libs, So if the
      -- package consists only of executables there will not be one:
      exists <- doesFileExist :: FilePath -> IO BooldoesFileExist haddockInterfaceFileSrc :: FilePathhaddockInterfaceFileSrc
      when :: Monad m => Bool -> m () -> m ()when exists :: Boolexists ($) :: (a -> b) -> a -> b$ do
        createDirectoryIfMissingVerbose ::
  Verbosity -> Bool -> FilePath -> IO ()createDirectoryIfMissingVerbose verbosity :: Verbosityverbosity True :: BoolTrue interfacePref :: FilePathinterfacePref
        installOrdinaryFile :: Verbosity -> FilePath -> FilePath -> IO ()installOrdinaryFile verbosity :: Verbosityverbosity haddockInterfaceFileSrc :: FilePathhaddockInterfaceFileSrc
                                      haddockInterfaceFileDest :: FilePathhaddockInterfaceFileDest

  let lfile = licenseFile :: PackageDescription -> FilePathlicenseFile pkg_descr :: PackageDescriptionpkg_descr
  unless :: Monad m => Bool -> m () -> m ()unless (null :: [a] -> Boolnull lfile :: FilePathlfile) ($) :: (a -> b) -> a -> b$ do
    createDirectoryIfMissingVerbose ::
  Verbosity -> Bool -> FilePath -> IO ()createDirectoryIfMissingVerbose verbosity :: Verbosityverbosity True :: BoolTrue docPref :: FilePathdocPref
    installOrdinaryFile :: Verbosity -> FilePath -> FilePath -> IO ()installOrdinaryFile verbosity :: Verbosityverbosity lfile :: FilePathlfile (docPref :: FilePathdocPref (</>) :: FilePath -> FilePath -> FilePath</> takeFileName :: FilePath -> FilePathtakeFileName lfile :: FilePathlfile)

  let buildPref = buildDir :: LocalBuildInfo -> FilePathbuildDir lbi :: LocalBuildInfolbi
  when :: Monad m => Bool -> m () -> m ()when (hasLibs :: PackageDescription -> BoolhasLibs pkg_descr :: PackageDescriptionpkg_descr) ($) :: (a -> b) -> a -> b$
    notice :: Verbosity -> String -> IO ()notice verbosity :: Verbosityverbosity ("Installing library in " (++) :: [a] -> [a] -> [a]++ libPref :: FilePathlibPref)
  when :: Monad m => Bool -> m () -> m ()when (hasExes :: PackageDescription -> BoolhasExes pkg_descr :: PackageDescriptionpkg_descr) ($) :: (a -> b) -> a -> b$
    notice :: Verbosity -> String -> IO ()notice verbosity :: Verbosityverbosity ("Installing executable(s) in " (++) :: [a] -> [a] -> [a]++ binPref :: FilePathbinPref)

  -- install include files for all compilers - they may be needed to compile
  -- haskell files (using the CPP extension)
  when :: Monad m => Bool -> m () -> m ()when (hasLibs :: PackageDescription -> BoolhasLibs pkg_descr :: PackageDescriptionpkg_descr) ($) :: (a -> b) -> a -> b$ installIncludeFiles ::
  Verbosity -> PackageDescription -> FilePath -> IO ()installIncludeFiles verbosity :: Verbosityverbosity pkg_descr :: PackageDescriptionpkg_descr incPref :: FilePathincPref

  case compilerFlavor :: Compiler -> CompilerFlavorcompilerFlavor (compiler :: LocalBuildInfo -> Compilercompiler lbi :: LocalBuildInfolbi) of
     GHC  -> do withLib :: PackageDescription -> (Library -> IO ()) -> IO ()withLib pkg_descr :: PackageDescriptionpkg_descr ($) :: (a -> b) -> a -> b$
                  installLib ::
  Verbosity
  -> LocalBuildInfo
  -> FilePath
  -> FilePath
  -> FilePath
  -> PackageDescription
  -> Library
  -> IO ()GHC.installLib verbosity :: Verbosityverbosity lbi :: LocalBuildInfolbi libPref :: FilePathlibPref dynlibPref :: FilePathdynlibPref buildPref :: FilePathbuildPref pkg_descr :: PackageDescriptionpkg_descr
                withExe :: PackageDescription -> (Executable -> IO ()) -> IO ()withExe pkg_descr :: PackageDescriptionpkg_descr ($) :: (a -> b) -> a -> b$
                  installExe ::
  Verbosity
  -> LocalBuildInfo
  -> InstallDirs FilePath
  -> FilePath
  -> (FilePath, FilePath)
  -> PackageDescription
  -> Executable
  -> IO ()GHC.installExe verbosity :: Verbosityverbosity lbi :: LocalBuildInfolbi installDirs :: InstallDirs FilePathinstallDirs buildPref :: FilePathbuildPref (progPrefixPref :: FilePathprogPrefixPref, progSuffixPref :: FilePathprogSuffixPref) pkg_descr :: PackageDescriptionpkg_descr
     LHC  -> do withLib :: PackageDescription -> (Library -> IO ()) -> IO ()withLib pkg_descr :: PackageDescriptionpkg_descr ($) :: (a -> b) -> a -> b$
                  installLib ::
  Verbosity
  -> LocalBuildInfo
  -> FilePath
  -> FilePath
  -> FilePath
  -> PackageDescription
  -> Library
  -> IO ()LHC.installLib verbosity :: Verbosityverbosity lbi :: LocalBuildInfolbi libPref :: FilePathlibPref dynlibPref :: FilePathdynlibPref buildPref :: FilePathbuildPref pkg_descr :: PackageDescriptionpkg_descr
                withExe :: PackageDescription -> (Executable -> IO ()) -> IO ()withExe pkg_descr :: PackageDescriptionpkg_descr ($) :: (a -> b) -> a -> b$
                  installExe ::
  Verbosity
  -> LocalBuildInfo
  -> InstallDirs FilePath
  -> FilePath
  -> (FilePath, FilePath)
  -> PackageDescription
  -> Executable
  -> IO ()LHC.installExe verbosity :: Verbosityverbosity lbi :: LocalBuildInfolbi installDirs :: InstallDirs FilePathinstallDirs buildPref :: FilePathbuildPref (progPrefixPref :: FilePathprogPrefixPref, progSuffixPref :: FilePathprogSuffixPref) pkg_descr :: PackageDescriptionpkg_descr
     JHC  -> do withLib :: PackageDescription -> (Library -> IO ()) -> IO ()withLib pkg_descr :: PackageDescriptionpkg_descr ($) :: (a -> b) -> a -> b$
                  installLib ::
  Verbosity
  -> FilePath
  -> FilePath
  -> PackageDescription
  -> Library
  -> IO ()JHC.installLib verbosity :: Verbosityverbosity libPref :: FilePathlibPref buildPref :: FilePathbuildPref pkg_descr :: PackageDescriptionpkg_descr
                withExe :: PackageDescription -> (Executable -> IO ()) -> IO ()withExe pkg_descr :: PackageDescriptionpkg_descr ($) :: (a -> b) -> a -> b$
                  installExe ::
  Verbosity
  -> FilePath
  -> FilePath
  -> (FilePath, FilePath)
  -> PackageDescription
  -> Executable
  -> IO ()JHC.installExe verbosity :: Verbosityverbosity binPref :: FilePathbinPref buildPref :: FilePathbuildPref (progPrefixPref :: FilePathprogPrefixPref, progSuffixPref :: FilePathprogSuffixPref) pkg_descr :: PackageDescriptionpkg_descr
     Hugs -> do
       let targetProgPref = progdir :: InstallDirs dir -> dirprogdir (absoluteInstallDirs ::
  PackageDescription
  -> LocalBuildInfo
  -> CopyDest
  -> InstallDirs FilePathabsoluteInstallDirs pkg_descr :: PackageDescriptionpkg_descr lbi :: LocalBuildInfolbi NoCopyDest :: CopyDestNoCopyDest)
       let scratchPref = scratchDir :: LocalBuildInfo -> FilePathscratchDir lbi :: LocalBuildInfolbi
       install ::
  Verbosity
  -> LocalBuildInfo
  -> FilePath
  -> FilePath
  -> FilePath
  -> FilePath
  -> FilePath
  -> (FilePath, FilePath)
  -> PackageDescription
  -> IO ()Hugs.install verbosity :: Verbosityverbosity lbi :: LocalBuildInfolbi libPref :: FilePathlibPref progPref :: FilePathprogPref binPref :: FilePathbinPref targetProgPref :: FilePathtargetProgPref scratchPref :: FilePathscratchPref (progPrefixPref :: FilePathprogPrefixPref, progSuffixPref :: FilePathprogSuffixPref) pkg_descr :: PackageDescriptionpkg_descr
     NHC  -> do withLib :: PackageDescription -> (Library -> IO ()) -> IO ()withLib pkg_descr :: PackageDescriptionpkg_descr ($) :: (a -> b) -> a -> b$ installLib ::
  Verbosity
  -> FilePath
  -> FilePath
  -> PackageIdentifier
  -> Library
  -> IO ()NHC.installLib verbosity :: Verbosityverbosity libPref :: FilePathlibPref buildPref :: FilePathbuildPref (packageId :: Package pkg => pkg -> PackageIdentifierpackageId pkg_descr :: PackageDescriptionpkg_descr)
                withExe :: PackageDescription -> (Executable -> IO ()) -> IO ()withExe pkg_descr :: PackageDescriptionpkg_descr ($) :: (a -> b) -> a -> b$ installExe ::
  Verbosity
  -> FilePath
  -> FilePath
  -> (FilePath, FilePath)
  -> Executable
  -> IO ()NHC.installExe verbosity :: Verbosityverbosity binPref :: FilePathbinPref buildPref :: FilePathbuildPref (progPrefixPref :: FilePathprogPrefixPref, progSuffixPref :: FilePathprogSuffixPref)
     UHC  -> do withLib :: PackageDescription -> (Library -> IO ()) -> IO ()withLib pkg_descr :: PackageDescriptionpkg_descr ($) :: (a -> b) -> a -> b$ installLib ::
  Verbosity
  -> LocalBuildInfo
  -> FilePath
  -> FilePath
  -> FilePath
  -> PackageDescription
  -> Library
  -> IO ()UHC.installLib verbosity :: Verbosityverbosity lbi :: LocalBuildInfolbi libPref :: FilePathlibPref dynlibPref :: FilePathdynlibPref buildPref :: FilePathbuildPref pkg_descr :: PackageDescriptionpkg_descr
     _    -> die :: String -> IO adie ($) :: (a -> b) -> a -> b$ "installing with "
                (++) :: [a] -> [a] -> [a]++ display :: Text a => a -> Stringdisplay (compilerFlavor :: Compiler -> CompilerFlavorcompilerFlavor (compiler :: LocalBuildInfo -> Compilercompiler lbi :: LocalBuildInfolbi))
                (++) :: [a] -> [a] -> [a]++ " is not implemented"
  return :: Monad m => forall a. a -> m areturn ()
  -- register step should be performed by caller.

-- | Install the files listed in data-files
--
installDataFiles :: Verbosity -> PackageDescription -> FilePath -> IO ()
installDataFiles verbosity pkg_descr destDataDir =
  flip :: (a -> b -> c) -> b -> a -> cflip mapM_ :: Monad m => (a -> m b) -> [a] -> m ()mapM_ (dataFiles :: PackageDescription -> [FilePath]dataFiles pkg_descr :: PackageDescriptionpkg_descr) ($) :: (a -> b) -> a -> b$ \ file -> do
    let srcDataDir = dataDir :: PackageDescription -> FilePathdataDir pkg_descr :: PackageDescriptionpkg_descr
    files <- matchDirFileGlob :: FilePath -> FilePath -> IO [FilePath]matchDirFileGlob srcDataDir :: FilePathsrcDataDir file :: FilePathfile
    let dir = takeDirectory :: FilePath -> FilePathtakeDirectory file :: FilePathfile
    createDirectoryIfMissingVerbose ::
  Verbosity -> Bool -> FilePath -> IO ()createDirectoryIfMissingVerbose verbosity :: Verbosityverbosity True :: BoolTrue (destDataDir :: FilePathdestDataDir (</>) :: FilePath -> FilePath -> FilePath</> dir :: FilePathdir)
    sequence_ :: Monad m => [m a] -> m ()sequence_ [ installOrdinaryFile :: Verbosity -> FilePath -> FilePath -> IO ()installOrdinaryFile verbosity :: Verbosityverbosity (srcDataDir :: FilePathsrcDataDir  (</>) :: FilePath -> FilePath -> FilePath</> file' :: FilePathfile')
                                              (destDataDir :: FilePathdestDataDir (</>) :: FilePath -> FilePath -> FilePath</> file' :: FilePathfile')
              | file' <- files :: [FilePath]files ]

-- | Install the files listed in install-includes
--
installIncludeFiles :: Verbosity -> PackageDescription -> FilePath -> IO ()
installIncludeFiles verbosity
  PackageDescription { library = Just lib } destIncludeDir = do

  incs <- mapM :: Monad m => (a -> m b) -> [a] -> m [b]mapM (findInc :: [FilePath] -> [Char] -> IO ([Char], FilePath)findInc relincdirs :: [[Char]]relincdirs) (installIncludes :: BuildInfo -> [FilePath]installIncludes lbi :: LocalBuildInfolbi)
  sequence_ :: Monad m => [m a] -> m ()sequence_
    [ do createDirectoryIfMissingVerbose ::
  Verbosity -> Bool -> FilePath -> IO ()createDirectoryIfMissingVerbose verbosity :: Verbosityverbosity True :: BoolTrue destDir :: FilePathdestDir
         installOrdinaryFile :: Verbosity -> FilePath -> FilePath -> IO ()installOrdinaryFile verbosity :: Verbosityverbosity srcFile :: FilePathsrcFile destFile :: FilePathdestFile
    | (relFile, srcFile) <- incs :: [([Char], FilePath)]incs
    , let destFile = destIncludeDir :: FilePathdestIncludeDir (</>) :: FilePath -> FilePath -> FilePath</> relFile :: [Char]relFile
          destDir  = takeDirectory :: FilePath -> FilePathtakeDirectory destFile :: FilePathdestFile ]
  where
   relincdirs = "." (:) :: a -> [a] -> [a]: filter :: (a -> Bool) -> [a] -> [a]filter (not :: Bool -> Boolnot(.) :: (b -> c) -> (a -> b) -> a -> c.isAbsolute :: FilePath -> BoolisAbsolute) (includeDirs :: BuildInfo -> [FilePath]includeDirs lbi :: LocalBuildInfolbi)
   lbi = libBuildInfo :: Library -> BuildInfolibBuildInfo lib :: Librarylib

   findInc []         file = die :: String -> IO adie ("can't find include file " (++) :: [a] -> [a] -> [a]++ file :: FilePathfile)
   findInc (dir:dirs) file = do
     let path = dir :: FilePathdir (</>) :: FilePath -> FilePath -> FilePath</> file :: FilePathfile
     exists <- doesFileExist :: FilePath -> IO BooldoesFileExist path :: FilePathpath
     if exists :: Boolexists then return :: Monad m => forall a. a -> m areturn (file :: FilePathfile, path :: FilePathpath) else findInc :: [FilePath] -> [Char] -> IO ([Char], FilePath)findInc dirs :: [FilePath]dirs file :: FilePathfile
installIncludeFiles _ _ _ = die :: String -> IO adie "installIncludeFiles: Can't happen?"