1import Distribution.PackageDescription ( PackageDescription )
2import Distribution.Simple ( defaultMainWithHooks
3                           , simpleUserHooks
4                           , postBuild
5                           , postCopy
6                           , postInst
7                           )
8import Distribution.Simple.InstallDirs ( mandir
9                                       , CopyDest (NoCopyDest)
10                                       )
11import Distribution.Simple.LocalBuildInfo ( LocalBuildInfo(..)
12                                          , absoluteInstallDirs
13                                          )
14import Distribution.Simple.Utils ( installOrdinaryFiles
15                                 , notice )
16import Distribution.Simple.Setup ( buildVerbosity
17                                 , copyDest
18                                 , copyVerbosity
19                                 , fromFlag
20                                 , installVerbosity
21                                 )
22import Distribution.Verbosity ( Verbosity )
23
24import System.IO ( openFile
25                 , IOMode (WriteMode)
26                 )
27import System.Process ( runProcess )
28import System.FilePath ( (</>) )
29
30-- WARNING to editors of this file:
31-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32-- At this moment (Cabal 1.23), whatever you write here must be
33-- compatible with ALL Cabal libraries which we support bootstrapping
34-- with.  This is because pre-setup-depends versions of cabal-install will
35-- build Setup.hs against the version of Cabal which MATCHES the library
36-- that cabal-install was built against.  There is no way of overriding
37-- this behavior without bumping the required 'cabal-version' in our
38-- Cabal file.  Travis will let you know if we fail to install from
39-- tarball!
40
41main :: IO ()
42main = defaultMainWithHooks $ simpleUserHooks
43  { postBuild = \ _ flags _ lbi ->
44      buildManpage lbi (fromFlag $ buildVerbosity flags)
45  , postCopy = \ _ flags pkg lbi ->
46      installManpage pkg lbi (fromFlag $ copyVerbosity flags) (fromFlag $ copyDest flags)
47  , postInst = \ _ flags pkg lbi ->
48      installManpage pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
49  }
50
51buildManpage :: LocalBuildInfo -> Verbosity -> IO ()
52buildManpage lbi verbosity = do
53  let cabal = buildDir lbi </> "cabal/cabal"
54      manpage = buildDir lbi </> "cabal/cabal.1"
55  manpageHandle <- openFile manpage WriteMode
56  notice verbosity ("Generating manual page " ++ manpage ++ " ...")
57  _ <- runProcess cabal ["manpage"] Nothing Nothing Nothing (Just manpageHandle) Nothing
58  return ()
59
60installManpage :: PackageDescription -> LocalBuildInfo -> Verbosity -> CopyDest -> IO ()
61installManpage pkg lbi verbosity copy = do
62  let destDir = mandir (absoluteInstallDirs pkg lbi copy) </> "man1"
63  installOrdinaryFiles verbosity destDir [(buildDir lbi </> "cabal", "cabal.1")]
64