# -*- 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.1
PortGroup           makefile 1.0
PortGroup           openssl 1.0

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

github.setup        tsduck tsduck 3.41-4299 v
revision            0
categories          multimedia
license             BSD
maintainers         {mit.edu:quentin @quentinmit} \
                    openmaintainer
description         MPEG Transport Stream Toolkit
long_description    {*}${description}

homepage            https://tsduck.io

checksums           rmd160  8c6300529c2d87063b8fe1106e44758a35e53825 \
                    sha256  1940946f5d15b9c1fea941e91d4685eb60cf4857a77f55eb3ad71d4e7e79ce65 \
                    size    7889207
github.tarball_from archive

# https://github.com/tsduck/tsduck/commit/1f3a5b6999eca914c1f241ca2e7634f84b1e0996
patchfiles          0000-tsPlatform.h-fix-powerpc-macros.patch

# Current patches assume a build with gcc. If you need to build with clang, ask MacPorts.
patchfiles-append   0001-tsSysUtils.cpp-fix-for-macOS-when-MACH_TASK_BASIC_IN.patch \
                    0002-tsUDPSocket.h-TS_NO_SSM-on-macOS.patch \
                    0003-tsEnvironment.cpp-use-correct-environ-for-macOS.patch \
                    0004-Fix-flags-and-linking.patch \
                    0005-Drop-html-docs-target-broken.patch \
                    0006-tsconfig-accommodate-MacPorts-prefix-case.patch \
                    0007-tsconfig-extra-fixes.patch

if {${os.platform} eq "darwin" && ${os.major} < 16} {
    patchfiles-append \
                    0008-Link-to-LegacySupport.patch
}

# The build uses a wrong option here:
post-patch {
    reinplace "s|uname -m|uname -p|" ${worksrcpath}/scripts/make-config.sh
}

set py_ver          3.13
set py_ver_nodot    [string map {. {}} ${py_ver}]

depends_build-append \
                    port:gsed \
                    port:python${py_ver_nodot}

depends_lib-append  port:curl \
                    port:libedit \
                    port:zlib

# We need to use the new C++ ABI
# https://github.com/tsduck/tsduck/issues/1641
# compiler.cxx_standard   2020
compiler.blacklist-append \
                    {clang} *gcc-4.*

build.env-append    PYTHON=${prefix}/bin/python${py_ver}

# PCSC requires pcsc-lite
# SRT requires libsrt
# Neither of these are currently available in macports
# Makefile.tsduck sets -install_name @rpath/$(notdir $@) and
# -rpath @executable_path, which are not compatible with the installed
# layout in $prefix.
set args            "NOJAVA=1 NOPCSC=1 NORIST=1 NOSRT=1 CFLAGS_EXTRA='-Wno-unknown-warning-option -Wno-unknown-pragmas' CXXFLAGS_EXTRA='-Wno-unknown-warning-option -Wno-unknown-pragmas' LDFLAGS_EXTRA=-Wl,-rpath,@executable_path/../lib"

build.target        default
build.args          NOTEST=true ${args} V=1

# tsversion(56671) malloc: *** error for object 0xa04da754: pointer being freed was not allocated
# P. S. tsconfig is a script, not a binary.
legacysupport.redirect_bins \
    tsanalyze tsbitrate tscharset tscmp tscrc32 tsdate tsdump tsecmg tseit tsemmg tsfclean tsfixcc \
    tsftrunc tsfuzz tsgenecm tslatencymonitor tslsdvb tsp tspacketize tspcap tspcontrol tspsi tsresync \
    tsscan tsstuff tsswitch tstabcomp tstabdump tstables tsterinfo tstestecmg tsvatek tsversion tsxml

destroot.env-append PYTHON=${prefix}/bin/python${py_ver}
destroot.target     install install-devel
destroot.args       NOTEST=true ${args} "SYSROOT=${destroot}" "SYSPREFIX=${prefix}"

test.run            yes
test.args           ${args} LDFLAGS_EXTRA=-Wl,-rpath,@executable_path
