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

name                7zip
version             25.00
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

# utimensat
legacysupport.newest_darwin_requires_legacy \
                    16

checksums           rmd160  dcdffbdbb6fc0a624c4d2d3c601c26dc38f2007a \
                    sha256  bff9e69b6ca73a5b8715d7623870a39dc90ad6ce1f4d1070685843987af1af9b \
                    size    1531036

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_STRIP = -s|LFLAGS_STRIP =|" \
    ${worksrcpath}/C/7zip_gcc_c.mak \
    ${worksrcpath}/CPP/7zip/7zip_gcc.mak
    # do not force -O2 flag during compilation
    reinplace "s|CFLAGS_BASE = $\(MY_ARCH_2\) -O2|CFLAGS_BASE = $\(MY_ARCH_2\) -O2 ${configure.cflags}|" \
    ${worksrcpath}/C/7zip_gcc_c.mak
    reinplace "s|CFLAGS_BASE = -O2|CFLAGS_BASE = -O2 ${configure.cxxflags}|" \
    ${worksrcpath}/CPP/7zip/7zip_gcc.mak
}

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

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

if {${build_arch} eq "x86_64"} {
    set arch_filename \
                    x64
} elseif {${build_arch} eq "arm64"} {
    set arch_filename \
                    arm64
}

build.args-append -f ../../cmpl_mac_${arch_filename}.mak
destroot {
    xinstall -m 755 \
             -s ${worksrcpath}/CPP/7zip/Bundles/Alone2/b/m_${arch_filename}/7zz \
             ${destroot}${prefix}/bin
}

post-destroot {
    set docdir ${prefix}/share/doc/${subport}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 {*}[glob ${worksrcpath}/DOC/*] ${destroot}${docdir}
}

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