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

# utimensat
legacysupport.newest_darwin_requires_legacy 16

name            lzma
version         25.00
revision        0
checksums       rmd160  03b846ffcd7374b97b70cddc74a32405c2f730b7 \
                sha256  e986e563fb51fa7f6c6bae6b1a00b4b429469e17751ea2c29f2783128ca5233c \
                size    1767433

categories      archivers
license         public-domain
maintainers     {ryandesign @ryandesign} openmaintainer

description     high compression ratio archiver

long_description \
    LZMA is default and general compression method of 7z \
    format in 7-Zip program. LZMA provides high compression \
    ratio and very fast decompression.

homepage        https://www.7-zip.org/sdk.html
master_sites    https://www.7-zip.org/a/

use_7z          yes
distname        lzma[strsed ${version} {/\.//}]

extract.mkdir   yes

extract {
    # Exclude precompiled binaries.
    system -W ${worksrcpath} "${prefix}/bin/7za x [shellescape ${distpath}/${distfiles}] -x'!bin/*'"
}

post-extract {
    fs-traverse item [list ${worksrcpath}] {
        if {[file isdirectory ${item}]} {
            file attributes ${item} -permissions ag+rx
        }
    }

   # DOS to UNIX line endings so we can patch.
   reinplace "s|\r||g" ${worksrcpath}/CPP/7zip/7zip_gcc.mak
}

patchfiles      patch-7zip_gcc_mak.diff \
                patch-lzma.1

post-patch {
    # needed for legacysupport
    reinplace -W ${worksrcpath} -E {s|LDFLAGS = \$\(LDFLAGS_STATIC\)|LDFLAGS += $(LDFLAGS_STATIC)|} \
        C/7zip_gcc_c.mak \
        CPP/7zip/7zip_gcc.mak

    reinplace "s|@CFLAGS@|${configure.cflags} [get_canonical_archflags cc]|g" \
        ${worksrcpath}/CPP/7zip/7zip_gcc.mak
    reinplace "s|@CXXFLAGS@|${configure.cxxflags} [get_canonical_archflags cxx]|g" \
        ${worksrcpath}/CPP/7zip/7zip_gcc.mak
}

# error: "SSE4.1 instruction set not enabled"
compiler.blacklist {*gcc-[3-4].*} {clang < 800}

# this build sets the optflags it wants internally
configure.optflags

build.dir       ${worksrcpath}/CPP/7zip/Bundles/LzmaCon
build.type      gnu
build.args      -f makefile.gcc

# ld: warning: option -s is obsolete and being ignored
# atom not found in symbolIndex on 10.6
build.args-append \
                LFLAGS_STRIP=

installs_libs   no

destroot {
    xinstall ${worksrcpath}/CPP/7zip/Bundles/LzmaCon/_o/lzma \
        ${destroot}${prefix}/bin/lzma_alone
    xinstall -d ${destroot}${prefix}/include/${name}-sdk
    copy ${worksrcpath}/C ${worksrcpath}/CPP \
        ${destroot}${prefix}/include/${name}-sdk
    system -W ${destroot}${prefix}/include/${name}-sdk \
        "find . -type f ! \\\( -name \"*.h\" -o -name \"*.hpp\" \\\) -delete"
    xinstall ${worksrcpath}/lzma.1 \
        ${destroot}${prefix}/share/man/man1/lzma_alone.1

    set docdir ${prefix}/share/doc/${name}/
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath}/DOC \
        lzma.txt lzma-history.txt lzma-specification.txt \
        ${destroot}${docdir}
}

notes "
The LZMA SDK program is installed as \"lzma_alone\" to avoid conflict with LZMA Utils.
"

livecheck.type  regex
livecheck.regex {>(\d+(?:\.\d+)*)</td>}
