## htslib_config.R.in -- template substituted by configure / configure.win
##
## Downstream R packages that need to compile against the htslib bundled in
## Rduckhts can source this file (after installation) to obtain the correct
## compiler and linker flags.
##
## Usage from another package's configure or Makevars:
##   Rscript -e "source(system.file('htslib_config.R', package='Rduckhts')); cat(htslib_cflags())"
##   Rscript -e "source(system.file('htslib_config.R', package='Rduckhts')); cat(htslib_libs())"

htslib_cflags <- function() {
    pkg_dir <- system.file(package = "Rduckhts")
    include_dir <- file.path(pkg_dir, "duckhts_extension", "htslib", "include")
    if (!dir.exists(include_dir)) {
        stop("Rduckhts htslib headers not found at ", include_dir,
             ". Was the package installed correctly?")
    }
    paste0("-I", shQuote(include_dir))
}

htslib_libs <- function(static = TRUE) {
    pkg_dir <- system.file(package = "Rduckhts")
    lib_dir <- file.path(pkg_dir, "duckhts_extension", "htslib", "lib")
    if (!dir.exists(lib_dir)) {
        stop("Rduckhts htslib libraries not found at ", lib_dir,
             ". Was the package installed correctly?")
    }
    if (static) {
        libhts_a <- file.path(lib_dir, "libhts.a")
        if (!file.exists(libhts_a)) {
            stop("Static libhts.a not found at ", libhts_a)
        }
        dep_libs <- " -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"
        paste(shQuote(libhts_a), dep_libs)
    } else {
        paste0("-L", shQuote(lib_dir), " -lhts  -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")
    }
}

htslib_rpath <- function() {
    pkg_dir <- system.file(package = "Rduckhts")
    lib_dir <- file.path(pkg_dir, "duckhts_extension", "htslib", "lib")
    if (.Platform$OS.type == "unix") {
        if (Sys.info()[["sysname"]] == "Darwin") {
            paste0("-Wl,-rpath,", shQuote(lib_dir))
        } else {
            paste0("-Wl,-rpath,", shQuote(lib_dir),
                   " -Wl,--disable-new-dtags")
        }
    } else {
        ""
    }
}

htslib_version <- function() {
    ""
}
