import MKL # Github Actions uses Intel CPUs, where MKL is faster than OpenBLAS
import Documenter
import Literate
import WhitenedRBMs

ENV["JULIA_DEBUG"] = "Documenter,Literate,WhitenedRBMs"

#=
We place Literate.jl source .jl files and the generated .md files inside docs/src/literate.
=#
const literate_dir = joinpath(@__DIR__, "src/literate")

#=
Helper function to remove all "*.md" files from a directory.
=#
function clear_md_files(dir::String)
    if isdir(dir)
        for (root, dirs, files) in walkdir(dir), file in files
            if endswith(file, ".md")
                rm(joinpath(root, file))
            end
        end
    end
end

#=
Remove previously Literate.jl generated files. This removes all "*.md" files inside
`literate_dir`. This is a precaution: if we build docs locally and something fails,
and then change the name of a source file (".jl"), we will be left with a lingering
".md" file which will be included in the current docs build. The following line makes
sure this does not happen.
=#
clear_md_files(literate_dir)

#=
Run Literate.jl on the .jl source files within docs/src/literate (recursively).
For each .jl file, this creates a markdown .md file at the same location as
and with the same name as the corresponding .jl file, but with the extension
changed (.jl -> .md).
=#
if isdir(literate_dir)
    for (root, dirs, files) in walkdir(literate_dir), file in files
        if endswith(file, ".jl")
            Literate.markdown(joinpath(root, file), root)
        end
    end
else
    @warn "There was nothing at" literate_dir
end

#=
Build docs.
=#
Documenter.makedocs(
    modules = [WhitenedRBMs],
    sitename = "WhitenedRBMs.jl",
    pages = [
        "Home" => "index.md",
        "Examples" => [
            "MNIST identity" => "literate/MNIST_identity.md",
            "MNIST centered" => "literate/MNIST_center.md",
            "MNIST whitened" => "literate/MNIST_white.md",
        ],
        "Reference" => "reference.md"
    ],
    strict = true
)

#=
After the docs have been compiled, we can remove the *.md files generated by Literate.
=#
clear_md_files(literate_dir)

#=
Deploy docs to Github pages.
=#
Documenter.deploydocs(
    repo = "github.com/cossio/WhitenedRBMs.jl.git",
    devbranch = "master"
)
