using Pkg;
using Logging;
@info "Initiating build"
## We want the Conda local Python env, anything else is out of control
install_p = false
if !haskey(ENV, "PYTHON")
    @info "PYTHON ENV not set --> settng to empty"
    install_p = true
    ENV["PYTHON"] = ""
end
if haskey(ENV, "R_HOME")
    @info "R set, using existing install"
else
    @info "R not set, using existing install"
    ENV["R_HOME"] = "*"
end
# ENV["R_HOME"] = "*"
# ENV["LD_PRELOAD"] = joinpath(ENV["HOME"], ".julia/conda/3/lib/libstdc++.so.6.0.30")
# Conda and PyCall are dependencies, but we need to make sure they get prebuilt first.
# We're in our own env, so explicitly adding them now does not harm.
Pkg.add("Conda")
Pkg.add("PyCall")
Pkg.add("HDF5")
Pkg.add("RCall")
Pkg.build("HDF5")
## --> Initiates an PyConda env local to us
Pkg.build("PyCall")
Pkg.build("RCall")
# Precompile
using PyCall
using Logging
using Conda
using RCall
## Add the two packages we need
# Conda.pip_interop(true)
# Conda.add("gcc=12.1.0"; channel="conda-forge")
# Pin this version, to avoid clashes with libgcc.34
# Conda.add("scipy=1.8.0"))
try
    PyCall.pyimport("smlmvis");
    PyCall.pyimport("meshio");
catch e
    @warn "Failed import $e -- installing"
    println("Failed import $e -- installing")
    Conda.pip_interop(true)
    Conda.add("gcc=12.1.0"; channel="conda-forge")
    #Pin this version, to avoid clashes with libgcc.34
    Conda.add("scipy=1.8.0")
    Conda.pip("install", "smlmvis")
    Conda.add("meshio"; channel="conda-forge")
end
# if install_p
#     Conda.add("smlmvis", channel="bcardoen")
#     Conda.add("meshio"; channel="conda-forge")
# end
PyCall.pyimport("smlmvis");
@info "Success!"
