# -*- 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

name            p7zip
version         16.02
revision        5
categories      archivers
# contains unrar code which has a restrictive license
license         LGPL-2.1+ Restrictive/Distributable
maintainers     nomaintainer
description     7-Zip implementation

long_description \
    p7zip is a port of 7-Zip, a file archiver with \
    highest compression ratio.

homepage        http://p7zip.sourceforge.net/

master_sites    sourceforge:project/${name}/${name}/${version}
distname        [strsed ${distname} {g/-/_/}]
use_bzip2       yes
distfiles       ${distname}_src_all${extract.suffix}
conflicts       7zip

checksums       rmd160  03550898e45b3eabe4ea0df5ee3787bd8f179fd0 \
                sha256  5eb20ac0e2944f6cb9c2d51dd6c4518941c185347d4089ea89087ffdd6e2341f \
                size    4239909

post-extract {
    # Most directories in the source distfile are lacking all
    # permissions for "group" and "other".
    fs-traverse item [list ${worksrcpath}] {
        if {[file isdir ${item}]} {
            file attributes ${item} -permissions a+rx
        }
    }
}

variant universal {}

patchfiles      patch-CVE-2016-9296.diff \
                patch-CVE-2017-17969.diff \
                patch-CVE-2018-5996.diff \
                patch-CVE-2018-10115.diff \
                patch-ErrorMsg-illegal-narrowing.diff \
                man.patch

# add the selected -stdlib to clang builds
# see https://trac.macports.org/ticket/56811
if {[string match *clang* ${configure.cxx}] && ${configure.cxx_stdlib} ne ""} {
    configure.cxx ${configure.cxx} -stdlib=${configure.cxx_stdlib}
}

configure {
    copy -force ${worksrcpath}/makefile.macosx_llvm_64bits ${worksrcpath}/makefile.machine
    # this also corrects a typo in the makefile ('XX' rather than 'CXX')
    reinplace "s|^XX=/usr/bin/llvm-g++|CXX=${configure.cxx}|" \
            ${worksrcpath}/makefile.machine
    reinplace "s|^CC=/usr/bin/llvm-gcc|CC=${configure.cc}|" \
            ${worksrcpath}/makefile.machine
    reinplace "s|-m64||g" \
            ${worksrcpath}/makefile.machine
    reinplace "s|\$(LOCAL_FLAGS)|[get_canonical_archflags] \$(LOCAL_FLAGS)|" \
            ${worksrcpath}/makefile.machine
}

# build 7za, 7z and 7zr
build.target all3

destroot.destdir    DEST_HOME=${prefix} \
                    DEST_SHARE_DOC=${prefix}/share/doc/${subport} \
                    DEST_MAN=${prefix}/share/man \
                    DEST_DIR=${destroot}

test.run            yes
test.target         all_test

livecheck.regex     /${name}_(\\d+\\.\\d+)_src_all\\.tar\\.
