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

name                7zip
version             25.01
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/
conflicts           p7zip

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

# utimensat
legacysupport.newest_darwin_requires_legacy \
                    16

checksums           rmd160  d2e43ce6fa941a84ea85d1597fa2cabe160938a2 \
                    sha256  ed087f83ee789c1ea5f39c464c55a5c9d4008deb0efe900814f2df262b82c36e \
                    size    1533240

extract.mkdir       yes
patchfiles          patch-7zip_gcc_mak.diff

# ppc support: https://github.com/ip7z/7zip/pull/141
patchfiles-append   patch-powerpc.patch

# i386 support
patchfiles-append   patch-i386.patch

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 +=|g" \
    ${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.args-append   -j${build.jobs}

build {
    if {${muniversal.build_arch} eq "x86_64"} {
        # file name is "x64" for x86_64
        set arch_filename x64
    } elseif {${muniversal.build_arch} eq "i386"} {
        # file name is "x86" for i386
        set arch_filename x86
    } else {
        # arm64, ppc, ppc64
        set arch_filename ${muniversal.build_arch}
    }

    set envvars [lmap e ${build.env} {shellescape $e}]
    foreach dir {Alone Alone2 Alone7z Format7zF SFXCon} {
        system -W ${worksrcpath}/CPP/7zip/Bundles/${dir} \
            "env [join $envvars] ${build.cmd} ${build.pre_args} ${build.args} -f ../../cmpl_mac_${arch_filename}.mak"
    }
}

destroot {
    if {${muniversal.build_arch} eq "x86_64"} {
        set arch_filename x64
    } elseif {${muniversal.build_arch} eq "i386"} {
        set arch_filename x86
    } else {
        set arch_filename ${muniversal.build_arch}
    }

    xinstall -m 0755 \
        ${worksrcpath}/CPP/7zip/Bundles/Alone/b/m_${arch_filename}/7za \
        ${worksrcpath}/CPP/7zip/Bundles/Alone2/b/m_${arch_filename}/7zz \
        ${worksrcpath}/CPP/7zip/Bundles/Alone7z/b/m_${arch_filename}/7zr \
        ${destroot}${prefix}/bin
    set libdir ${prefix}/lib/${name}
    file mkdir ${destroot}${libdir}
    xinstall -m 0755 \
        ${worksrcpath}/CPP/7zip/Bundles/Format7zF/b/m_${arch_filename}/7z.so \
        ${destroot}${libdir}
    xinstall -m 0755 \
        ${worksrcpath}/CPP/7zip/Bundles/SFXCon/b/m_${arch_filename}/7zCon \
        ${destroot}${libdir}/7zCon.sfx
    ln -s 7zz ${destroot}${prefix}/bin/7z
}

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+)"
