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

PortSystem          1.0
PortGroup           compiler_blacklist_versions 1.0

name                unrar
set my_name         ${name}src
version             7.1.2
categories          archivers
maintainers         nomaintainer
license             Restrictive/Distributable

description         Extract, view & test RAR archives

long_description    The unRAR utility is a minor part of the RAR archiver \
                    and contains RAR uncompression algorithm. unRAR \
                    requires very small volume of memory to operate.

homepage            https://www.rarlab.com/
master_sites        ${homepage}rar/
distname            ${my_name}-${version}

checksums           rmd160  410c958c0465c9cfe6ccdcff5ce86b44bed2cc4e \
                    sha256  143dcacb3a9d9ca0af695adecabe1350b0a3e93d927a065ef8f7cc605ab2d322 \
                    size    264081

patchfiles          patch-makefile.unix.diff

worksrcdir          ${name}

use_configure       no

variant universal   {}

# error: unknown type name 'constexpr'
compiler.cxx_standard \
                    2011
configure.cxxflags-append \
                    -std=c++11

# error: invalid cpu feature string for builtin
# uses newer intrinsic functions
compiler.blacklist  {clang < 900}

# Should go after blacklists.
if {[tbool configure.ccache]} {
    configure.cxx "ccache ${configure.cxx}"
}

set cxx_stdlibflags {}
if {[string match *clang* ${configure.cxx}] && ${configure.cxx_stdlib} ne ""} {
    set cxx_stdlibflags -stdlib=${configure.cxx_stdlib}
}

build.args              -f makefile \
                        CXX="[join ${configure.cxx}]"

# The makefile misuses the DESTDIR variable as if it were PREFIX. We need it at
# build time so the library gets the correct install_name.
build.args-append       DESTDIR=${prefix}

pre-build {
    build.args-append   CXXFLAGS="${configure.cxxflags} [get_canonical_archflags cxx] ${cxx_stdlibflags}" \
                        LDFLAGS="${configure.ldflags} [get_canonical_archflags ld] ${cxx_stdlibflags}"
}

post-destroot {
    set docdir ${prefix}/share/doc/${subport}
    xinstall -d ${destroot}${docdir}
    xinstall -m 644 -W ${worksrcpath} license.txt readme.txt ${destroot}${docdir}
}

platform darwin {
    patchfiles-append   patch-dylib.diff
}

if {${name} == ${subport}} {
    installs_libs       no
    
    destroot {
        xinstall ${worksrcpath}/${name} ${destroot}${prefix}/bin
    }
    
    livecheck.type      regex
    livecheck.url       ${homepage}rar_add.htm
    livecheck.regex     ${my_name}-(\\d+(?:\\.\\d+)*)
} else {
    livecheck.type      none
}

subport libunrar {
    description         unrar library
    long_description    ${description}
    
    # Copied from standalone libunrar port. Why do we need these?
    configure.cxxflags-append -DSILENT
    
    # The makefile adds -fPIC to the CXXFLAGS when building the library, but we
    # override the CXXFLAGS with our own, so we have to manually add this again.
    configure.cxxflags-append -fPIC
    
    build.target        lib
    
    destroot {
        xinstall -d ${destroot}${prefix}/include/unrar
        xinstall -m 644 ${worksrcpath}/dll.hpp ${destroot}${prefix}/include/unrar
        if {${os.platform} eq "darwin"} {
            xinstall ${worksrcpath}/libunrar.dylib ${destroot}${prefix}/lib
            xinstall ${worksrcpath}/libunrar.a ${destroot}${prefix}/lib
        } else {
            xinstall ${worksrcpath}/libunrar.so ${destroot}${prefix}/lib
        }
    }
}
