# -*- 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                unzip
version             6.0
revision            4
maintainers         {gregfiumara.com:greg @gfiumara} openmaintainer
categories          archivers sysutils
platforms           darwin freebsd
license             BSD
installs_libs       no
description         Decompression compatible with pkunzip

long_description \
    UnZip is an extraction utility for archives compressed in .zip \
    format (also called \"zipfiles\"). Although highly compatible both \
    with PKWARE's PKZIP and PKUNZIP utilities for MS-DOS and with Info-ZIP's \
    own Zip program, our primary objectives have been portability and \
    non-MSDOS functionality.

homepage            http://infozip.sourceforge.net/UnZip.html
master_sites        sourceforge:infozip
distname            ${name}[strsed $version g/\\.//]

checksums           rmd160  48af66606e9472e45fbb94bc4e285da23d1b89ba \
                    sha256  036d96991646d0449ed0aa952e4fbe21b476ce994abc276e49d30e686708bd37 \
                    size    1376845

# Add Debian patches
patch.pre_args-replace  -p0 -p1
patchfiles \
    01-manpages-in-section-1-not-in-section-1l.patch \
    04-handle-pkware-verification-bit.patch \
    05-fix-uid-gid-handling.patch \
    06-initialize-the-symlink-flag.patch \
    07-increase-size-of-cfactorstr.patch \
    08-allow-greater-hostver-values.patch \
    09-cve-2014-8139-crc-overflow.patch \
    10-cve-2014-8140-test-compr-eb.patch \
    11-cve-2014-8141-getzip64data.patch \
    12-cve-2014-9636-test-compr-eb.patch \
    13-remove-build-date.patch \
    14-cve-2015-7696.patch \
    15-cve-2015-7697.patch \
    16-fix-integer-underflow-csiz-decrypted.patch \
    17-restore-unix-timestamps-accurately.patch \
    18-cve-2014-9913-unzip-buffer-overflow.patch \
    19-cve-2016-9844-zipinfo-buffer-overflow.patch \
    20-cve-2018-1000035-unzip-buffer-overflow.patch \
    21-fix-warning-messages-on-big-files.patch \
    22-cve-2019-13232-fix-bug-in-undefer-input.patch \
    23-cve-2019-13232-zip-bomb-with-overlapped-entries.patch \
    24-cve-2019-13232-do-not-raise-alert-for-misplaced-central-directory.patch


post-patch {
    reinplace -E "/-O3/s|(LF2=\")|\\1[get_canonical_archflags ld]|" \
        ${worksrcpath}/unix/Makefile
}

# C only, avoid macports-clang dep
configure.cxx_stdlib

use_configure       no

variant universal {}

set localflags      [get_canonical_archflags cc]

build.target        macosx
build.args          -f unix/Makefile \
                    CC="${configure.cc}"

pre-build {
    # have to add this here so variants can modify it
    build.args-append LOCAL_UNZIP="-DLARGE_FILE_SUPPORT ${localflags}"
}

destroot.args       {*}${build.args} \
                    BINDIR=${destroot}${prefix}/bin \
                    MANDIR=${destroot}${prefix}/share/man/man1

post-destroot {
    set docdir ${prefix}/share/doc/${name}-${version}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} BUGS LICENSE README ToDo \
        ${destroot}${docdir}
}

platform darwin 8 {
    append localflags " -DNO_LCHMOD"
}

variant iconv description {Add iconv support} {
    depends_lib-append  port:libiconv
    patchfiles-append   99-unzip60-alt-iconv-utf8.patch

    set unicode_support_args    "-DUNICODE_SUPPORT -DUTF8_MAYBE_NATIVE -D_MBCS"
    append localflags           " -I${prefix}/include -DNATIVE ${unicode_support_args}"
    build.post_args-append      LFLAGS1="-L${prefix}/lib -liconv"
}

livecheck.type      regex
livecheck.url       ${homepage}
livecheck.regex     "UnZip (\\d+(?:\\.\\d+)*)"
