#  Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
#  This Source Code Form is subject to the terms of the Mozilla Public
#  License, v. 2.0. If a copy of the MPL was not distributed with this
#  file, You can obtain one at https://mozilla.org/MPL/2.0/.

import Distributed

if get(ENV, "GITHUB_EVENT_NAME", "") == "pull_request"
    # To speed up the Documentation build during pull requests, over-write
    # {minted} blocks by {verbatim}. In pushes to `master` and on tags, we don't
    # do this.
    write(
        joinpath(@__DIR__, "src", "assets", "custom.sty"),
        """
        % Neutralize the minted environment so LaTeX doesn't invoke Pygments
        \\let\\minted\\verbatim
        \\let\\endminted\\endverbatim
        \\let\\inputminted\\verbatiminput
        """,
    )
end

Distributed.@everywhere include(joinpath(@__DIR__, "make_utilities.jl"))

# Needed to make Documenter think that there is a PDF in the right place when
# link checking. In production we replace this by running the LaTeX build.
write(joinpath(@__DIR__, "src", "JuMP.pdf"), "")

@time begin
    literate_tutorials()
    add_tutorial_overview()
    add_solver_readmes()
    add_changelog()
    add_moi_pages()
end

@time if _PDF || _IS_GITHUB_ACTIONS
    # Copy the src into a new directory for the latex build
    cp(joinpath(@__DIR__, "src"), joinpath(@__DIR__, "latex_src"); force = true)
    # w = Distributed.workers()
    # f_latex = Distributed.remotecall(make_latex, w[1])
    # f_html = Distributed.remotecall(make_html, w[2])
    # fetch(f_latex)
    # fetch(f_html)
    make_html()
    make_latex()
    # Hack for deploying: copy the pdf (and only the PDF) into the HTML build
    # directory! We don't want to copy everything in `latex_build` because it
    # includes lots of extraneous LaTeX files.
    cp(
        joinpath(@__DIR__, "latex_build", "JuMP.pdf"),
        joinpath(@__DIR__, "build", "JuMP.pdf");
        force = true,
    )
    rm(joinpath(@__DIR__, "latex_src"); force = true, recursive = true)
else
    @time make_html()
end

if _IS_GITHUB_ACTIONS
    Documenter.deploydocs(;
        repo = "github.com/jump-dev/JuMP.jl.git",
        push_preview = true,
    )
end
