# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           github 1.0
PortGroup           legacysupport 1.0

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

name                hamlib
categories          science
license             LGPL GPL
maintainers         {ra1nb0w @ra1nb0w} openmaintainer

description         Ham Radio Control Libraries

long_description    Flexible and portable shared libraries that offer a \
                    standardized API to control any radio oriented equipment \
                    through a computer interface.

homepage            https://hamlib.github.io

github.setup        Hamlib Hamlib 4.7.1
github.tarball_from releases
checksums           rmd160  a9d3d56a077fb049696e3df64a6a403fd43bffe1 \
                    sha256  d197a08a3d5d936d7571ae573f745bbba619e88998742c8267e3fcb0fb3d5974 \
                    size    3112433
revision            0

distname            hamlib-${version}

depends_build-append \
    port:pkgconfig

depends_lib-append \
    port:libtool \
    port:libusb-compat \
    port:libxml2 \
    port:readline

compiler.cxx_standard 2011

set python_versions { 3.10 3.11 3.12 3.13 3.14}
# python binding is not enabled by default
set default_python_variant ""

# Define the available variants
foreach py_ver ${python_versions} {
    set py_ver_no_dot [join [split ${py_ver} "."] ""]
    set variant_line {variant python${py_ver_no_dot} description "Build with python ${py_ver} support"}
    foreach py_over ${python_versions} {
        if { ${py_ver} == ${py_over} } { continue }
        set py_over_no_dot [join [split ${py_over} "."] ""]
        append variant_line " conflicts python${py_over_no_dot}"
    }
    append variant_line { { } }
    eval $variant_line
    if {[variant_isset python${py_ver_no_dot}]} {
        if {${default_python_variant} != "+python${py_ver_no_dot}"} {
            set default_python_variant ""
        }
    }
}

# set default python variant if not selected
if {${default_python_variant} != ""} {
    default_variants-append "${default_python_variant}"
}

# If a python variant is enabled, activate it
set active_python_version ""
set active_python_version_no_dot ""
foreach py_ver ${python_versions} {
    set py_ver_no_dot [join [split ${py_ver} "."] ""]
    if {[variant_isset python${py_ver_no_dot}]} {
        set active_python_version        ${py_ver}
        set active_python_version_no_dot ${py_ver_no_dot}

        depends_build-append \
            port:swig-python

        depends_lib-append \
            port:python${active_python_version_no_dot}

        configure.args-append \
            --with-python-binding

        # specify the Python version to use
        set python_framework_dir ${frameworks_dir}/Python.framework/Versions/${active_python_version}
        configure.args-append \
            PYTHON=${python_framework_dir}/bin/python${active_python_version} \
            PYTHON_EXECUTABLE=${python_framework_dir}/bin/python${active_python_version} \
            PYTHON_INCLUDE_DIR=${python_framework_dir}/Headers \
            PYTHON_LIBRARY=${python_framework_dir}/Python \
            PYTHON_VERSION=${py_ver}

        # install the binding in the right directory
        set python_site_dir ${python_framework_dir}/lib/python${active_python_version}/site-packages
        destroot.args-append \
            pythondir=${python_site_dir} \
            pyexecdir=${python_site_dir} \
            pkgpythondir=${python_site_dir}/${name} \
            pkgpyexecdir=${python_site_dir}/${name}
    }
}
