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

name                            john
categories                      sysutils security
maintainers                     nomaintainer

homepage                        https://www.openwall.com/john/

patchfiles                      patch-src-params.h.diff

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/src/params.h
}

if {${subport} eq "john"} {
    version                     1.9.0
    revision                    2

    # john is quite old and doesn't support arm64, but jumbo is different story
    supported_archs             i386 ppc ppc64 x86_64

    license                     {GPL-2+ OpenSSLException}

    description                 Featureful Unix password cracker
    long_description            John the Ripper is a UNIX password cracker, currently available for \
                                UNIX (tested with Linux x86, FreeBSD x86, Solaris 2.x SPARC, OSF/1 \
                                Alpha), DOS, WinNT/Win95.

    master_sites                https://www.openwall.com/john/k/
    use_xz                      yes

    checksums                   rmd160  16114db2fe5004ce4044a2d56b46ef209b941dc4 \
                                sha256  0b266adcfef8c11eed690187e71494baea539efbd632fe221181063ba09508df \
                                size    8944932

    conflicts                   john-jumbo john-jumbo-devel

    use_configure               no

    use_parallel_build          no

    build.dir                   ${worksrcpath}/src
    build.args                  CC=${configure.cc}
    build.target                clean

    switch ${configure.build_arch} {
        i386 {
            build.target-append macosx-x86-sse2
        }
        x86_64 {
            # x86_64 is only supported on Leopard and up
            if {${os.major} >= 9} {
                build.target-append macosx-x86-64
            } else {
                configure.build_arch i386
                build.target-append macosx-x86-sse2
            }
        }
        ppc {
            # Use altivec if available
            if {![catch {sysctl hw.vectorunit} result] && $result == 1} {
                build.target-append macosx-ppc32-altivec
            } else {
                build.target-append macosx-ppc32
            }
        }
        ppc64 {
            build.target-append macosx-ppc64
        }
        default {
            build.target-append unknown-arch
        }
    }
}

subport john-jumbo {
    version                     1.9.0
    revision                    9
    set jumbo_patchlvl          1

    master_sites                https://www.openwall.com/john/k/
    distname                    ${name}-${version}-jumbo-${jumbo_patchlvl}

    use_xz                      yes

    checksums                   rmd160  ea75c464d211691f00db7aec66e0503c1177866b \
                                sha256  f5d123f82983c53d8cc598e174394b074be7a77756f5fb5ed8515918c81e7f3b \
                                size    33638768

    conflicts                   john john-jumbo-devel

    patchfiles-append           patch-apple-m1.diff \
                                patch-fix-ld-multiple-definition-errors.diff \
                                patch-fix-alignment-compile-error.diff
}

subport john-jumbo-devel {
    PortGroup                   github 1.0

    github.setup                openwall john 8d60fae463f9e5efa105964dcb1e66249840bb29
    github.tarball_from         archive
    version                     20250530
    revision                    0

    checksums                   rmd160  857d4d7bf7c0b9211e616b1ad5824101d9f2a2cc \
                                sha256  d8a29e387ff7d89ef248be92eadee234d094c49dbe765ebc868ad5558622170f \
                                size    56266890

    conflicts                   john john-jumbo

    github.livecheck.branch     bleeding-jumbo
}

if {${subport} in [list "john-jumbo" "john-jumbo-devel"]} {
    PortGroup                   openssl 1.0

    description                 This is the community-enhanced, "jumbo" version of John the Ripper.
    long_description            {*}${description}

    license                     GPL-2 GPL-3+ Apache-2 Restrictive

    depends_lib-append          port:bzip2 \
                                port:gmp \
                                port:libpcap \
                                port:zlib

    set perl_version            5.34

    set ruby_branch             3.4
    set ruby_version            [string map {. {}} ${ruby_branch}]

    # NOTE: not all scripts were migrated to python3
    # See:
    #  - https://github.com/openwall/john/issues/4883
    #  - https://github.com/openwall/john/pull/5309
    set python2_branch          2.7
    set python3_branch          3.13

    set python2_version         [string map {. {}} ${python2_branch}]
    set python3_version         [string map {. {}} ${python3_branch}]

    depends_run-append          port:p${perl_version}-compress-raw-lzma \
                                port:p${perl_version}-digest-md4 \
                                port:p${perl_version}-net-radius \
                                port:p${perl_version}-netpacket \
                                port:p${perl_version}-data-plist \
                                port:perl${perl_version} \
                                port:py${python2_version}-asn1 \
                                port:py${python2_version}-plyvel \
                                port:py${python2_version}-pysap \
                                port:py${python3_version}-asn1crypto \
                                port:py${python3_version}-bsddb3 \
                                port:py${python3_version}-ldap3 \
                                port:py${python3_version}-parsimonious \
                                port:py${python3_version}-protobuf3 \
                                port:python${python2_version} \
                                port:python${python3_version} \
                                port:ruby${ruby_version}

    if {${subport} eq "john-jumbo"} {
        depends_run-append      port:py${python2_version}-lxml \
                                port:py${python2_version}-pycryptodome

        depends_run-append      bin:scapy-${python2_branch}:py${python2_version}-scapy
    }

    if {${subport} eq "john-jumbo-devel"} {
        depends_run-append      port:py${python3_version}-asn1 \
                                port:py${python3_version}-cbor2 \
                                port:py${python3_version}-hanko \
                                port:py${python3_version}-lxml \
                                port:py${python3_version}-pycryptodome

        depends_run-append      bin:scapy-${python3_branch}:py${python3_version}-scapy
    }

    configure.dir               ${worksrcpath}/src

    # error We need minimum OpenCL 1.2 to build with OpenCL support.
    if {${os.platform} eq "darwin" && ${os.major} <= 11} {
        configure.args-append   --disable-opencl
    }

    build.dir                   ${worksrcpath}/src
    build.target                default

    variant native description {Enable CPU-specific optimizations} {
        configure.args-append   --enable-native-march
    }

    variant experimental description {Enable new, experimental or even incomplete features} {
        configure.args-append   --enable-experimental-code
    }

    variant threads description {Enable multithreaded support via OpenMP} {
        compiler.openmp_version 3.0

        depends_lib-append      port:libomp
    }

    # libomp aren't available on PowerPC
    if {${build_arch} ni [list ppc ppc64]} {
        default_variants-append +threads
    }

    if {![variant_isset native]} {
        notes "\
        This version is built based on a base architecture for convenience,\
        which may not be optimized for your system. To build a version\
        customized for your machine, use the +native variant"
    }

    post-destroot {
        # pse2john.py depends on pysap which is python2 only
        reinplace -W ${destroot}${prefix}/share/john \
            -locale C -E -q "1s|^#! ?/usr/bin/(env )?python|#!${prefix}/bin/python${python2_branch}|" \
            pse2john.py

        if {${subport} eq "john-jumbo"} {
            reinplace -W ${destroot}${prefix}/share/john \
                -locale C -E -q "1s|^#! ?/usr/bin/(env )?python|#!${prefix}/bin/python${python2_branch}|" \
                DPAPImk2john.py \
                aix2john.py \
                ibmiscanner2john.py \
                keyring2john.py \
                kirbi2john.py \
                pcap2john.py \
                ps_token2john.py

            system -W ${destroot}${prefix}/share/john "chmod +x diskcryptor2john.py"
        }

        if {${subport} eq "john-jumbo-devel"} {
            system -W ${destroot}${prefix}/share/john "chmod +x bestcryptve2john.py coinomi2john.py"
        }

        reinplace -locale C -E -q "1s|^#! ?/usr/bin/(env )?python2|#!${prefix}/bin/python${python2_branch}|" \
            {*}[glob ${destroot}${prefix}/share/john/*.py]
        reinplace -locale C -E -q "1s|^#! ?/usr/bin/(env )?python3?|#!${prefix}/bin/python${python3_branch}|" \
            {*}[glob ${destroot}${prefix}/share/john/*.py]
        reinplace -locale C -E -q "1s|^#! ?/usr/bin/(env )?perl|#!${prefix}/bin/perl${perl_version}|" \
            {*}[glob -directory ${destroot}${prefix}/share/john *.pl benchmark-unify relbench]
        reinplace -locale C -E -q "1s|^#! ?/usr/bin/(env )?ruby|#!${prefix}/bin/ruby${ruby_branch}|" \
            {*}[glob ${destroot}${prefix}/share/john/*.rb]

        xinstall -d ${destroot}${prefix}/etc/bash_completion.d
        move ${destroot}${prefix}/share/john/john.bash_completion \
            ${destroot}${prefix}/etc/bash_completion.d

        xinstall -d ${destroot}${prefix}/share/zsh/site-functions
        move ${destroot}${prefix}/share/john/john.zsh_completion \
            ${destroot}${prefix}/share/zsh/site-functions
    }

}

destroot {
    file copy ${worksrcpath}/run ${destroot}${prefix}/share/john
    system "chmod -R a+rX ${destroot}${prefix}/share/john"
    system "ln -s ${prefix}/share/john/john ${destroot}${prefix}/bin/john"
    xinstall -d ${destroot}${prefix}/share/doc
    file copy ${worksrcpath}/doc ${destroot}${prefix}/share/doc/john
    system "chmod -R a+rX ${destroot}${prefix}/share/doc/john"
}

notes-append "You'll find the john files under ${prefix}/share/john/"

if {${subport} in [list "john" "john-jumbo"]} {
    livecheck.type              regex
    livecheck.regex             /${name}-(\[0-9.\]+)${extract.suffix}
}
