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

categories              www
maintainers             {@barracuda156 gmail.com:vital.had} {@BlackBirdLC netcourrier.com:thewireless} openmaintainer
license                 GPL-2
name                    tenfourfox
subport                 tenfourfox-devel {}
subport                 aquafox {}

# Re ppc64: https://github.com/classilla/tenfourfox/issues/655
supported_archs         ppc
universal_variant       no

installs_libs           no

if {${subport} eq "${name}"} {
    conflicts           ${name}-devel
    description         Clone of Firefox, compatible with macOS 10.4+. This version corresponds \
                        to the latest released version of ${name}.
    long_description    {*}${description}

    github.setup        classilla tenfourfox FPR32SPR5
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    checksums           rmd160  e9a499d3acfe69c622eeafc7ea68ef957b7bdf3c \
                        sha256  b5aef01b6d031e4b582460955e3a3b95f7b3bfb994cdfa0171f0cd6279ca21ce \
                        size    325688845
    set appname 	TenFourFox

} elseif {${subport} eq "${name}-devel"} {
    conflicts           ${name}
    description         Clone of Firefox, compatible with macOS 10.4+. Tracks latest commit on GitHub.
    long_description    {*}${description}

    github.setup        classilla tenfourfox cdc8b5a989ce0b8ced48edbf8166d460c67a6a0d
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    version             20240505-FPR32
    checksums           rmd160  3ad5aa53b5d127c8d46da03cb9e94e5573193db8 \
                        sha256  2a9bf00d8b535cc78111f1f9ece6c185aeac3635b08dad09c9efb082356d3631 \
                        size    326144707
    set appname 	TenFourFox

} elseif {${subport} eq "aquafox"} {
    description         TenFourFox fork with extra optimizations.
    long_description    {*}${description}

    github.setup        BlackBirdLC Aquafox 1.0.0 v
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    checksums           rmd160  631a80fea5601ac7cb6aecb620ce146347774f3a \
                        sha256  c79d49237249573dd59eeb45fb09600ce043ef70d498b17bddba64a3cd1febea \
                        size    325246887
    patchfiles-append 	0785667d630acf269af554f538c19da181e5253f.patch
    set appname 	Aquafox
}

# Verify we have the required SDK:
platform darwin {
    set tiger_sdkpath ${developer_dir}/SDKs/MacOSX10.4u.sdk
    if {![file exists $tiger_sdkpath]} {
        pre-fetch {ui_error "Building ${name} @${version} requires the MacOSX10.4u.sdk to be present in ${developer_dir}/SDKs/"}
    }
}

# https://github.com/classilla/tenfourfox/wiki/HowToBuildFPR
depends_build-append    port:autoconf213 \
                        port:cctools \
                        port:freetype \
                        port:gcc48 \
                        port:gmake \
                        port:ld64 \
                        port:libidl \
                        port:python27

if {${os.major} < 10} {
    # Strip disabled below for 10.6 Rosetta/ppc.
    depends_build-append \
                        port:strip7
}

variant G4 conflicts G5 description "Optimize for G4 CPU" {
    post-extract {
        if {${os.major} == 10} {
            move ${worksrcpath}/G4-7450-nostrip.mozcfg ${worksrcpath}/.mozconfig
        } else {
            move ${worksrcpath}/G4-7450.mozcfg ${worksrcpath}/.mozconfig
        }
    }
    post-patch {
        reinplace "s|Debugging|macOS ${macos_version} on G4|g" ${worksrcpath}/netwerk/protocol/http/OptimizedFor.h
    }
}

variant G5 conflicts G4 description "Optimize for G5 CPU" {
    post-extract {
        if {${os.major} == 10} {
            move ${worksrcpath}/G5-nostrip.mozcfg ${worksrcpath}/.mozconfig
        } else {
            move ${worksrcpath}/G5.mozcfg ${worksrcpath}/.mozconfig
        }
    }
    post-patch {
        reinplace "s|Debugging|macOS ${macos_version} on G5|g" ${worksrcpath}/netwerk/protocol/http/OptimizedFor.h
    }
}

if {![variant_isset G5]} {
    default_variants    +G4
}

patch.pre_args-replace  -p0 -p1
patchfiles-append       patch-ots-stdlog-fix.diff

post-patch {
    reinplace "s|-j2|-j${buildmakejobs}|" ${worksrcpath}/.mozconfig
    reinplace "s|/opt/local|${prefix}|g" ${worksrcpath}/.mozconfig
    reinplace "s|/opt/local|${prefix}|g" ${worksrcpath}/media/libvpx/Makefile.in
    reinplace "s|/opt/local|${prefix}|g" ${worksrcpath}/config/external/nss/Makefile.in
    reinplace "s|/opt/local|${prefix}|g" ${worksrcpath}/104fx_copy.sh
}

# This is hardcoded into the mozconfig build script, but here anyway as a reminder:
compiler.whitelist      macports-gcc-4.8

configure.cxx_stdlib    libstdc++

use_autoconf            yes
autoconf.cmd            autoconf213

use_configure           no

# We don't want to confuse the build with any macports-passed-in values.
configure.cc
configure.cxx
configure.env

# This is needed:
build.env               SHELL=/bin/bash
build.cmd               gmake -f client.mk build

# Make it self-contained. This could be removed if we want to keep it lean,
# but then we'd have to copy the app from a different spot.
pre-destroot {
    system -W ${worksrcpath} "./104fx_copy.sh ${appname}.app"
}

destroot {
    move ${worksrcpath}/${appname}.app ${destroot}${applications_dir}
}
