-- | A module for documenting Scala source files using @scaladoc@.
module System.Build.Scala.Scaladoc(
                                    Scaladoc,
                                    -- * @Scaladoc@ members
                                    debug,
                                    nowarn,
                                    verbose,
                                    deprecation,
                                    unchecked,
                                    classpath,
                                    sourcepath,
                                    bootclasspath,
                                    extdirs,
                                    directory,
                                    encoding,
                                    target,
                                    print,
                                    optimise,
                                    explaintypes,
                                    uniqid,
                                    version,
                                    help,
                                    (?),
                                    access,
                                    bottom,
                                    charset,
                                    doctitle,
                                    footer,
                                    header,
                                    linksource,
                                    nocomment,
                                    stylesheetfile,
                                    top,
                                    windowtitle,
                                    etc,
                                    -- * @Scaladoc@ values
                                    scaladoc,
                                    scaladoc'
                                  ) where

import Data.Maybe
import Prelude hiding (print)
import System.FilePath
import System.Build.Args
import System.Build.CompilePaths
import System.Build.Extensions
import System.Build.OutputDirectory
import System.Build.OutputReferenceSet
import System.Build.OutputReferenceGet
import System.Build.Command
import System.Build.Scala.Target
import System.Build.Scala.Debug
import System.Build.Scala.Access
import System.Build.Scala.Scalac (kscalac, scalac')

-- | Javadoc is the compiler for Scala API documentation.
data Scaladoc = Scaladoc {
  debug :: Maybe Debug,           -- ^ @-g@
  nowarn :: Bool,                 -- ^ @-nowarn@
  verbose :: Bool,                -- ^ @-verbose@
  deprecation :: Bool,            -- ^ @-deprecation@
  unchecked :: Bool,              -- ^ @-unchecked@
  classpath :: [FilePath],        -- ^ @-classpath@
  sourcepath :: [FilePath],       -- ^ @-sourcepath@
  bootclasspath :: [FilePath],    -- ^ @-bootclasspath@
  extdirs :: [FilePath],          -- ^ @-extdirs@
  directory :: Maybe FilePath,    -- ^ @-d@
  encoding :: Maybe String,       -- ^ @-encoding@
  target :: Maybe Target,         -- ^ @-target@
  print :: Bool,                  -- ^ @-print@
  optimise :: Bool,               -- ^ @-optimise@
  explaintypes :: Bool,           -- ^ @-explaintypes@
  uniqid :: Bool,                 -- ^ @-uniqid@
  version :: Bool,                -- ^ @-version@
  help :: Bool,                   -- ^ @-help@
  (?) :: Maybe FilePath,          -- ^ @\@@
  access :: Maybe Access,         -- ^ @-access@
  bottom :: Maybe String,         -- ^ @-bottom@
  charset :: Maybe String,        -- ^ @-charset@
  doctitle :: Maybe String,       -- ^ @-doctitle@
  footer :: Maybe String,         -- ^ @-footer@
  header :: Maybe String,         -- ^ @-header@
  linksource :: Bool,             -- ^ @-linksource@
  nocomment :: Bool,              -- ^ @-nocomment@
  stylesheetfile :: Maybe String, -- ^ @-stylesheetfile@
  top :: Maybe String,            -- ^ @-top@
  windowtitle :: Maybe String,    -- ^ @-windowtitle@
  etc :: Maybe String
}

-- | A @Scaladoc@ with nothing set.
scaladoc :: Scaladoc
scaladoc = Scaladoc Nothing False False False False [] [] [] [] Nothing Nothing Nothing False False False False False False Nothing Nothing Nothing Nothing Nothing Nothing Nothing False False Nothing Nothing Nothing Nothing

-- | Construct a @Scaladoc@.
scaladoc' :: Maybe Debug
             -> Bool
             -> Bool
              -> Bool
             -> Bool
             -> [FilePath]
             -> [FilePath]
             -> [FilePath]
             -> [FilePath]
             -> Maybe FilePath
             -> Maybe String
             -> Maybe Target
             -> Bool
             -> Bool
             -> Bool
             -> Bool
             -> Bool
             -> Bool
             -> Maybe FilePath
             -> Maybe Access
             -> Maybe String
             -> Maybe String
             -> Maybe String
             -> Maybe String
             -> Maybe String
             -> Bool
             -> Bool
             -> Maybe String
             -> Maybe String
             -> Maybe String
             -> Maybe String
             -> Scaladoc
scaladoc' = Scaladoc

instance Show Scaladoc where
  show (Scaladoc debug'
                 nowarn'
                 verbose'
                 deprecation'
                 unchecked'
                 classpath'
                 sourcepath'
                 bootclasspath'
                 extdirs'
                 directory'
                 encoding'
                 target'
                 print'
                 optimise'
                 explaintypes'
                 uniqid'
                 version'
                 help'
                 script'
                 access'
                 bottom'
                 charset'
                 doctitle'
                 footer'
                 header'
                 linksource'
                 nocomment'
                 stylesheetfile'
                 top'
                 windowtitle'
                 etc') = (kscalac (scalac' debug' nowarn' verbose' deprecation' unchecked' classpath' sourcepath' bootclasspath' extdirs' directory' encoding' target' print' optimise' explaintypes' uniqid' version' help' script' etc') ++ ["access" -~> access',
                                      "bottom" ~~> bottom',
                                      "charset" ~~> charset',
                                      "doctitle" ~~> doctitle',
                                      "footer" ~~> footer',
                                      "header" ~~> header',
                                      "linksource" ~~ linksource',
                                      "nocomment" ~~ nocomment',
                                      "stylesheetfile" ~~> stylesheetfile',
                                      "top" ~~> top',
                                      "windowtitle" ~~> windowtitle']) ^^^ " "

instance CompilePaths Scaladoc where
  j =>> ps = show j ++ ' ' : space ps

instance Extensions Scaladoc where
  exts _ = ["scala"]

instance OutputDirectory Scaladoc where
  outdir = directory

instance OutputReferenceSet Scaladoc where
  setReference p j = j { classpath = p }

instance OutputReferenceGet Scaladoc where
  getReference = classpath

instance Command Scaladoc where
  command _ = let envs = [
                           ("SCALA_HOME", (</> "bin" </> "scaladoc")),
                           ("SCALADOC", id)
                         ]
              in fromMaybe "scaladoc" `fmap` tryEnvs envs