# Autogenerated wrapper script for tectonic_jll for x86_64-w64-mingw32-cxx11
export tectonic

using Fontconfig_jll
using FreeType2_jll
using Graphite2_jll
using HarfBuzz_jll
using ICU_jll
using OpenSSL_jll
using Zlib_jll
using libpng_jll
## Global variables
PATH = ""
LIBPATH = ""
LIBPATH_env = "PATH"
LIBPATH_default = ""

# Relative path to `tectonic`
const tectonic_splitpath = ["bin", "tectonic.exe"]

# This will be filled out by __init__() for all products, as it must be done at runtime
tectonic_path = ""

# tectonic-specific global declaration
function tectonic(f::Function; adjust_PATH::Bool = true, adjust_LIBPATH::Bool = true)
    global PATH, LIBPATH
    env_mapping = Dict{String,String}()
    if adjust_PATH
        if !isempty(get(ENV, "PATH", ""))
            env_mapping["PATH"] = string(PATH, ';', ENV["PATH"])
        else
            env_mapping["PATH"] = PATH
        end
    end
    if adjust_LIBPATH
        LIBPATH_base = get(ENV, LIBPATH_env, expanduser(LIBPATH_default))
        if !isempty(LIBPATH_base)
            env_mapping[LIBPATH_env] = string(LIBPATH, ';', LIBPATH_base)
        else
            env_mapping[LIBPATH_env] = LIBPATH
        end
    end
    withenv(env_mapping...) do
        f(tectonic_path)
    end
end


# Inform that the wrapper is available for this platform
wrapper_available = true

"""
Open all libraries
"""
function __init__()
    # This either calls `@artifact_str()`, or returns a constant string if we're overridden.
    global artifact_dir = find_artifact_dir()

    global PATH_list, LIBPATH_list
    # Initialize PATH and LIBPATH environment variable listings
    # From the list of our dependencies, generate a tuple of all the PATH and LIBPATH lists,
    # then append them to our own.
    foreach(p -> append!(PATH_list, p), (Fontconfig_jll.PATH_list, FreeType2_jll.PATH_list, Graphite2_jll.PATH_list, HarfBuzz_jll.PATH_list, ICU_jll.PATH_list, OpenSSL_jll.PATH_list, Zlib_jll.PATH_list, libpng_jll.PATH_list,))
    foreach(p -> append!(LIBPATH_list, p), (Fontconfig_jll.LIBPATH_list, FreeType2_jll.LIBPATH_list, Graphite2_jll.LIBPATH_list, HarfBuzz_jll.LIBPATH_list, ICU_jll.LIBPATH_list, OpenSSL_jll.LIBPATH_list, Zlib_jll.LIBPATH_list, libpng_jll.LIBPATH_list,))

    global tectonic_path = normpath(joinpath(artifact_dir, tectonic_splitpath...))

    push!(PATH_list, dirname(tectonic_path))
    # Filter out duplicate and empty entries in our PATH and LIBPATH entries
    filter!(!isempty, unique!(PATH_list))
    filter!(!isempty, unique!(LIBPATH_list))
    global PATH = join(PATH_list, ';')
    global LIBPATH = join(vcat(LIBPATH_list, [Sys.BINDIR, joinpath(Sys.BINDIR, Base.LIBDIR, "julia"), joinpath(Sys.BINDIR, Base.LIBDIR)]), ';')

    
end  # __init__()
