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

# strnlen
legacysupport.newest_darwin_requires_legacy 10

github.setup            hashcat hashcat 7.1.2 v
revision                1
categories              security
license                 MIT
maintainers             @gaming-hacker openmaintainer

description             World's fastest and most advanced password recovery utility
long_description        ${name} is the world's fastest and most advanced \
                        password recovery utility, supporting five unique modes \
                        of attack for over 200 highly-optimized hashing algorithms. \
                        hashcat currently supports CPUs, GPUs and other hardware \
                        accelerators on Linux, Windows and macOS, and has \
                        facilities to help enable distributed password cracking.

homepage                https://hashcat.net/${name}

build.target            {}

checksums               rmd160  0f547ff9218e81a4db88a8190d931c4bab487b00 \
                        sha256  9546a6326d747530b44fcc079babad40304a87f32d3c9080016d58b39cfc8b96 \
                        size    19125734
github.tarball_from     archive

set python_branch       3.13
set python_version      [string map {. ""} ${python_branch}]
set perl_version        5.34

compiler.c_standard     2011
compiler.cxx_standard   2017

depends_lib-append      port:libiconv \
                        port:minizip \
                        port:python${python_version} \
                        port:xxhash \
                        port:zlib

depends_run-append      port:p${perl_version}-dbd-sqlite \
                        port:perl${perl_version} \
                        port:py${python_version}-asn1 \
                        port:py${python_version}-cryptography \
                        port:py${python_version}-plyvel \
                        port:py${python_version}-protobuf3 \
                        port:py${python_version}-pycryptodome \
                        port:py${python_version}-python-snappy

patch.pre_args-replace  -p0 -p1

if {${os.platform} eq "darwin" && ${os.major} < 12} {
    patchfiles-append   patch-Makefile-compat.diff \
                        hashcat-7.1.2-no-metal.patch \
                        patch-fix-Apple-headers.diff
    # TODO: should be fixable, but the code is a mess.
    patchfiles-append   hashcat-7.1.2-no-iokit.patch
    # Could be used up to darwin 15, but w/e.
    patchfiles-append   patch-legacy-support.diff
} else {
    patchfiles-append   patch-remove-arch.patch
}

# Portability fixes:
patchfiles-append       hashcat-7.1.2-fix-cpu-features.patch \
                        patch-fix-bridges.diff

platform darwin {
    if {${os.major} < 10 || ${configure.build_arch} eq "ppc"} {
        # FIXME: https://github.com/hashcat/hashcat/issues/4638
        patchfiles-append   hashcat-bigendian-fix.patch

        # MacTypes.h:94:41: error: conflicting types for 'UInt32'; have 'long unsigned int'
        patchfiles-append   fix-lzma-types.diff

        post-patch {
            reinplace "s|/System/Library/Frameworks/OpenCL.framework/OpenCL|${prefix}/lib/libOpenCL.dylib|" \
                            ${worksrcpath}/src/ext_OpenCL.c
        }

        depends_build-append \
                            port:opencl-headers
        depends_lib-append  port:freeocl
    }
}

post-patch {
    reinplace "s|MACOSX_DEPLOYMENT_TARGET=.\*|MACOSX_DEPLOYMENT_TARGET=${macosx_deployment_target}|" ${worksrcpath}/src/Makefile
    reinplace "s|python3-config|${prefix}/bin/python${python_branch}-config|" {*}[glob ${worksrcpath}/src/bridges/bridge_python_*.mk]
    reinplace -E "s|#! */usr/bin/(env)* *python3*|#!${prefix}/bin/python${python_branch}\\\n|" {*}[glob ${worksrcpath}/tools/*hashcat.py]
    reinplace "s|#! */usr/bin/env *perl|#!${prefix}/bin/perl${perl_version}|" {*}[glob ${worksrcpath}/tools/*hashcat.pl]

    if {${os.platform} eq "darwin" && ${os.major} < 12} {
        reinplace "s|@PREFIX@|${prefix}|" ${worksrcpath}/src/Makefile
    }
}

set build_env {
                        SHARED=1
                        USE_SYSTEM_ZLIB=1
                        USE_SYSTEM_OPENCL=1
                        USE_SYSTEM_XXHASH=1
}

build.env-append        {*}$build_env
destroot.env-append     {*}$build_env

post-destroot {
    xinstall -d ${destroot}${prefix}/share/bash-completion/completions
    xinstall -m 0644 ${worksrcpath}/extra/tab_completion/${name}.sh ${destroot}${prefix}/share/bash-completion/completions/${name}
}
