# -*- 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           legacysupport 1.1
PortGroup           makefile 1.0
PortGroup           compiler_blacklist_versions 1.0

# utimensat
legacysupport.newest_darwin_requires_legacy 16

name                7zip
version             24.06
revision            0

categories          archivers
license             LGPL-2.1
maintainers         {gmail.com:srirangav @srirangav} openmaintainer

description         ${name} is a file archiver with a high compression ratio.
long_description    ${name} is a file archiver that has a high compression \
                    ratio in 7z format with LZMA and LZMA2 compression, \
                    supports packing and unpacking XZ, BZIP2, GZIP, TAR, \
                    ZIP and WIM archives, and unpacking AR, ARJ, CAB, CHM, \
                    CPIO, CramFS, DMG, EXT, FAT, GPT, HFS, IHEX, ISO, LZH, \
                    LZMA, MBR, MSI, NSIS, NTFS, QCOW2, RAR, RPM, SquashFS, \
                    UDF, UEFI, VDI, VHD, VHDX, VMDK, WIM, XAR and Z archives.

homepage            https://www.7-zip.org
master_sites        ${homepage}/a/

supported_archs     arm64 x86_64

use_xz              yes
distname            7z[string map {. {}} ${version}]-src

checksums           rmd160  05c297baef8c8b40df7977720ec184ff184dd86b \
                    sha256  2aa1660c773525b2ed84d6cd7ff0680c786ec0893b87e4db44654dcb7f5ac8b5 \
                    size    1487008

extract.mkdir       yes
patchfiles          patch-7zip_gcc_mak.diff

post-patch {
    reinplace "s|#include <sys/sysmacros.h>|//#include <sys/sysmacros.h>|" \
    ${worksrcpath}/CPP/7zip/Common/FileStreams.cpp \
    ${worksrcpath}/CPP/7zip/UI/Common/UpdateCallback.cpp
    # needed for legacysupport
    reinplace "s|LDFLAGS = $\(\LDFLAGS_STATIC\)\|LDFLAGS += $\(\LDFLAGS_STATIC\)\|" \
    ${worksrcpath}/C/7zip_gcc_c.mak \
    ${worksrcpath}/CPP/7zip/7zip_gcc.mak
    # ld: warning: option -s is obsolete and being ignored
    # atom not found in symbolIndex on 10.6
    reinplace "s|LFLAGS_ALL = $\(\LFLAGS_STRIP\)\|LFLAGS_ALL =|" \
    ${worksrcpath}/C/7zip_gcc_c.mak \
    ${worksrcpath}/CPP/7zip/7zip_gcc.mak
}

# uses newer assembly features on Intel
compiler.blacklist-append  {*gcc-[3-4].*} {clang < 1100} {macports-clang-3.*}

build.dir ${worksrcpath}/CPP/7zip/Bundles/Alone2

if {${build_arch} eq "x86_64"} {

    build.args-append -f ../../cmpl_mac_x64.mak

    destroot {
        xinstall -m 755 \
                 -s ${worksrcpath}/CPP/7zip/Bundles/Alone2/b/m_x64/7zz \
                 ${destroot}${prefix}/bin
    }

} elseif {${build_arch} eq "arm64"} {

    build.args-append -f ../../cmpl_mac_arm64.mak

    destroot {
        xinstall -m 755 \
                 -s ${worksrcpath}/CPP/7zip/Bundles/Alone2/b/m_arm64/7zz \
                 ${destroot}${prefix}/bin
    }
}

post-destroot {
    set docdir ${prefix}/share/doc/${subport}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath}/DOC/ \
        7zC.txt 7zFormat.txt 7zip.hhp 7zip.wxs copying.txt License.txt lzma.txt Methods.txt \
        readme.txt src-history.txt unRarLicense.txt \
        ${destroot}${docdir}
}

livecheck.type      regex
livecheck.url       https://www.7-zip.org/history.txt
livecheck.regex     "^(\\d+.\\d+)"
