# Autogenerated wrapper script for iODBC_debug_jll for x86_64-apple-darwin14
export iodbctestw, libiodbcdSYM, libiodbcinstdSYM, libiodbcinst, libiodbc, iodbctest

## Global variables
PATH = ""
LIBPATH = ""
LIBPATH_env = "DYLD_FALLBACK_LIBRARY_PATH"
LIBPATH_default = "~/lib:/usr/local/lib:/lib:/usr/lib"

# Relative path to `iodbctestw`
const iodbctestw_splitpath = ["bin", "iodbctestw"]

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

# iodbctestw-specific global declaration
function iodbctestw(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(iodbctestw_path)
    end
end


# Relative path to `libiodbcdSYM`
const libiodbcdSYM_splitpath = ["lib", "libiodbc.2.dylib.dSYM", "Contents", "Resources", "DWARF", "libiodbc.2.dylib"]

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

# libiodbcdSYM-specific global declaration
# This will be filled out by __init__()
libiodbcdSYM = ""


# Relative path to `libiodbcinstdSYM`
const libiodbcinstdSYM_splitpath = ["lib", "libiodbcinst.2.dylib.dSYM", "Contents", "Resources", "DWARF", "libiodbcinst.2.dylib"]

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

# libiodbcinstdSYM-specific global declaration
# This will be filled out by __init__()
libiodbcinstdSYM = ""


# Relative path to `libiodbcinst`
const libiodbcinst_splitpath = ["lib", "libiodbcinst.2.dylib"]

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

# libiodbcinst-specific global declaration
# This will be filled out by __init__()
libiodbcinst_handle = C_NULL

# This must be `const` so that we can use it with `ccall()`
const libiodbcinst = "@rpath/libiodbcinst.2.dylib"


# Relative path to `libiodbc`
const libiodbc_splitpath = ["lib", "libiodbc.2.dylib"]

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

# libiodbc-specific global declaration
# This will be filled out by __init__()
libiodbc_handle = C_NULL

# This must be `const` so that we can use it with `ccall()`
const libiodbc = "@rpath/libiodbc.2.dylib"


# Relative path to `iodbctest`
const iodbctest_splitpath = ["bin", "iodbctest"]

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

# iodbctest-specific global declaration
function iodbctest(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(iodbctest_path)
    end
end


"""
Open all libraries
"""
function __init__()
    global artifact_dir = abspath(artifact"iODBC_debug")

    # Initialize PATH and LIBPATH environment variable listings
    global PATH_list, LIBPATH_list
    # We first need to add to LIBPATH_list the libraries provided by Julia
    append!(LIBPATH_list, [joinpath(Sys.BINDIR, Base.LIBDIR, "julia"), joinpath(Sys.BINDIR, Base.LIBDIR)])
    global iodbctestw_path = normpath(joinpath(artifact_dir, iodbctestw_splitpath...))

    push!(PATH_list, dirname(iodbctestw_path))
    global libiodbcdSYM_path = normpath(joinpath(artifact_dir, libiodbcdSYM_splitpath...))

    global libiodbcdSYM = libiodbcdSYM_path
    global libiodbcinstdSYM_path = normpath(joinpath(artifact_dir, libiodbcinstdSYM_splitpath...))

    global libiodbcinstdSYM = libiodbcinstdSYM_path
    global libiodbcinst_path = normpath(joinpath(artifact_dir, libiodbcinst_splitpath...))

    # Manually `dlopen()` this right now so that future invocations
    # of `ccall` with its `SONAME` will find this path immediately.
    global libiodbcinst_handle = dlopen(libiodbcinst_path)
    push!(LIBPATH_list, dirname(libiodbcinst_path))

    global libiodbc_path = normpath(joinpath(artifact_dir, libiodbc_splitpath...))

    # Manually `dlopen()` this right now so that future invocations
    # of `ccall` with its `SONAME` will find this path immediately.
    global libiodbc_handle = dlopen(libiodbc_path)
    push!(LIBPATH_list, dirname(libiodbc_path))

    global iodbctest_path = normpath(joinpath(artifact_dir, iodbctest_splitpath...))

    push!(PATH_list, dirname(iodbctest_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(LIBPATH_list, ':')

    # Add each element of LIBPATH to our DL_LOAD_PATH (necessary on platforms
    # that don't honor our "already opened" trick)
    #for lp in LIBPATH_list
    #    push!(DL_LOAD_PATH, lp)
    #end
end  # __init__()

