## htslib_config.R.in -- substituted by configure / configure.win
##
## This installed file is intentionally source-able from another package's
## configure script. Prefer Rduckhts::rduckhts_htslib_config(), which also
## compares this receipt with duckhts_htslib_version() from the loaded extension.

.rduckhts_htslib_root <- function() {
    pkg_dir <- system.file(package = "Rduckhts")
    if (!nzchar(pkg_dir)) {
        stop("Rduckhts is not installed")
    }
    file.path(pkg_dir, "duckhts_extension", "htslib")
}

.rduckhts_htslib_shared_library <- function(lib_dir) {
    candidates <- if (.Platform$OS.type == "windows") {
        c(file.path(lib_dir, "hts-3.dll"), file.path(lib_dir, "libhts.dll"))
    } else if (identical(Sys.info()[["sysname"]], "Darwin")) {
        c(file.path(lib_dir, "libhts.dylib"), file.path(lib_dir, "libhts.3.dylib"))
    } else {
        c(file.path(lib_dir, "libhts.so"), file.path(lib_dir, "libhts.so.3"))
    }
    candidates[file.exists(candidates)][1L]
}

.rduckhts_htslib_header_number <- function(include_dir) {
    header <- file.path(include_dir, "htslib", "hts.h")
    if (!file.exists(header)) {
        return(NA_integer_)
    }
    line <- grep("^#define[[:space:]]+HTS_VERSION[[:space:]]+[0-9]+",
                 readLines(header, warn = FALSE), value = TRUE)
    if (!length(line)) {
        return(NA_integer_)
    }
    as.integer(sub(".*HTS_VERSION[[:space:]]+([0-9]+).*", "\\1", line[[1L]]))
}

htslib_default_link <- function() {
    if (isTRUE(TRUE)) "static" else "shared"
}

htslib_config <- function(link = htslib_default_link(), validate = TRUE,
                          root = .rduckhts_htslib_root()) {
    link <- match.arg(link, c("shared", "static"))
    include_dir <- file.path(root, "include")
    lib_dir <- file.path(root, "lib")
    static_library <- file.path(lib_dir, "libhts.a")
    shared_library <- .rduckhts_htslib_shared_library(lib_dir)
    library_file <- if (identical(link, "static")) static_library else shared_library
    plugin_dir <- file.path(root, "libexec", "htslib")
    if (!dir.exists(plugin_dir)) {
        plugin_dir <- NA_character_
    }

    rpath <- ""
    if (identical(link, "shared") && .Platform$OS.type == "unix") {
        rpath <- if (identical(Sys.info()[["sysname"]], "Darwin")) {
            paste0("-Wl,-rpath,", shQuote(lib_dir))
        } else {
            paste0("-Wl,-rpath,", shQuote(lib_dir), " -Wl,--disable-new-dtags")
        }
    }

    static_dependencies <- "-ldeflate -llzma -lbz2 -lz  -lz -lbz2 -llzma -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -lcurl -lsecur32 -lnghttp2 -lwldap32 -lssh2 -lgcrypt -lgpg-error -lbcrypt -ladvapi32 -lssl -lcrypto -lcrypt32 -lgdi32 -lzstd -lz -lpsl -lws2_32 -lidn2 -lunistring -liconv -lcharset -lbrotlidec -lbrotlicommon -lm -lpthread -LD:/rtools45/x86_64-w64-mingw32.static.posix -LD:/rtools45/x86_64-w64-mingw32.static.posix -lsecur32 -lidn2 -lunistring -liconv -lcharset -lwldap32 -lssh2 -lgcrypt -lgpg-error -lws2_32 -lz -lgcrypt -lgpg-error -lws2_32 -lgcrypt -lgpg-error -lws2_32 -lws2_32 -lz -lpsl -lunistring -lws2_32 -lidn2 -lunistring -liconv -lcharset -lbcrypt -ladvapi32 -lcrypt32 -lssl -lcrypto -lz -lws2_32 -lgdi32 -lcrypt32 -lcrypto -lz -lws2_32 -lgdi32 -lcrypt32 -lgdi32 -lbrotlidec -lbrotlidec -lbrotlicommon -lm -lbrotlicommon -lm -lunistring -lws2_32 -lunistring -liconv -lcharset -lbrotlicommon -lm -lpthread -liconv -lcharset -lz -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -lbrotlicommon -lm -LD:/rtools45/x86_64-w64-mingw32.static.posix -lz -lws2_32 -lgdi32 -lcrypt32 -lcrypto -lgdi32 -lcrypt32 -lunistring -lidn2 -lunistring -liconv -lcharset -lgcrypt -lgpg-error -lws2_32 -lz -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -lgcrypt -lgpg-error -lws2_32 -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -lgcrypt -lgpg-error -lws2_32 -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -lws2_32 -liconv -lcharset -lz -lbrotlidec -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -lbrotlicommon -lm -lzstd -lssl -LD:/rtools45/x86_64-w64-mingw32.static.posix -lz -lws2_32 -lgdi32 -lcrypt32 -lcrypto -lgdi32 -lcrypt32 -lunistring -liconv -lcharset -lssh2 -lgcrypt -lgpg-error -lws2_32 -lz -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -lgcrypt -lgpg-error -lws2_32 -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -lgcrypt -lgpg-error -lws2_32 -LD:/rtools45/x86_64-w64-mingw32.static.posix/lib -lgcrypt -lgpg-error -lz -lnghttp2 -lpsl -lunistring -lws2_32 -lidn2 -lunistring -liconv -lcharset  -lssl -lcrypto -ldeflate -ldl -lgnurx -lpthread -lws2_32"
    libs <- if (identical(link, "static")) {
        paste(shQuote(static_library), static_dependencies)
    } else {
        paste(shQuote(shared_library), rpath)
    }

    config <- list(
        contract_version = 1L,
        htslib_version = "1.24",
        htslib_header_version = 102400L,
        runtime_version = NA_character_,
        source_id = "htslib-release-1.24",
        build_id = "Rduckhts-1.5.1-0.1.3-windows_amd64_mingw",
        duckdb_platform = "windows_amd64_mingw",
        link = link,
        include_dir = include_dir,
        lib_dir = lib_dir,
        library_file = library_file,
        plugin_dir = plugin_dir,
        cppflags = paste0("-I", shQuote(include_dir)),
        ldflags = libs,
        rpath = rpath,
        static_dependencies = static_dependencies,
        shared_available = !is.na(shared_library),
        static_available = file.exists(static_library),
        features = list(
            cram = TRUE,
            zlib = TRUE,
            bzip2 = TRUE,
            lzma = TRUE,
            libdeflate = TRUE,
            curl = TRUE,
            openssl = TRUE,
            plugins = FALSE,
            s3 = TRUE,
            gcs = TRUE
        ),
        static_license_note = paste(
            "Rduckhts is GPL-3; bundled htslib is distributed under its",
            "permissive MIT/Expat-style license. Downstream packages remain",
            "responsible for the licenses of their complete static link."
        )
    )
    class(config) <- c("rduckhts_htslib_config", "list")

    if (isTRUE(validate)) {
        missing <- c(include_dir, lib_dir, library_file)
        missing <- missing[is.na(missing) | !file.exists(missing)]
        if (length(missing)) {
            stop("Rduckhts htslib contract is incomplete: missing ",
                 paste(missing, collapse = ", "))
        }
        header_version <- .rduckhts_htslib_header_number(include_dir)
        if (is.na(header_version) ||
            !identical(header_version, config$htslib_header_version)) {
            stop("Rduckhts htslib header/receipt version mismatch: header=",
                 header_version, ", receipt=", config$htslib_header_version)
        }
        if (!nzchar(config$htslib_version)) {
            stop("Rduckhts htslib receipt has an empty semantic version")
        }
    }

    config
}

## Compatibility helpers retained for downstream configure scripts.
htslib_cflags <- function() htslib_config(validate = TRUE)$cppflags
htslib_libs <- function(static = TRUE) {
    htslib_config(if (isTRUE(static)) "static" else "shared", validate = TRUE)$ldflags
}
htslib_rpath <- function() htslib_config(validate = TRUE)$rpath
htslib_version <- function() "1.24"
