# -*- 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               makefile 1.0
PortGroup               conflicts_build 1.0
PortGroup               codeberg 1.0

name                    smake
codeberg.setup          schilytools schilytools 2024-03-21
version                 1.7-${codeberg.version}
revision                0
categories              devel
maintainers             {hotmail.com:amtor @RobK88} \
                        openmaintainer
license                 CDDL
description             A highly portable make program with automake features
long_description        Smake is a highly portable make program with automake \
                        features. It is currently mainly targeted to be used \
                        with the Schily SING makefile system.

homepage                https://codeberg.org/schilytools/schilytools
master_sites            ${homepage}/archive/
checksums               rmd160  e937719c3a68efac9f6aacfaaaed4cb1775805e5 \
                        sha256  4d66bf35a5bc2927248fac82266b56514fde07c1acda66f25b9c42ccff560a02 \
                        size    5892015

post-extract {
    copy ${filespath}/Gmake.smake ${worksrcpath}
}

patchfiles-append       001-Makefile-Patch.diff

post-patch {
    reinplace -locale C "s|/opt/schily|${prefix}|g" \
    ${worksrcpath}/DEFAULTS/Defaults.darwin \
    ${worksrcpath}/DEFAULTS/Defaults.mac-os10 \
    ${worksrcpath}/DEFAULTS_ENG/Defaults.darwin \
    ${worksrcpath}/DEFAULTS_ENG/Defaults.mac-os10 \
    ${worksrcpath}/libfind/find.c \
    ${worksrcpath}/libfind/find_main.c \
    ${worksrcpath}/librscg/scsi-remote.c \
    ${worksrcpath}/patch/tests/random/cmptest.sh \
    ${worksrcpath}/patch/tests/random/gentest.sh \
    ${worksrcpath}/smake/job.c \
    ${worksrcpath}/smake/make.c \
    ${worksrcpath}/smake/smake.1 \
    ${worksrcpath}/TEMPLATES/Defaults.gcc \
    ${worksrcpath}/TEMPLATES/Defaults.clang \
    ${worksrcpath}/TEMPLATES/Defaults.xcc
}

proc port_conflict_check {p_port_name p_conflict_ver_min p_conflict_ver_max} {
    ui_info "Checking for conflict against port: ${p_port_name}"
    if { ![catch {set port_conflict_ver_info [lindex [registry_active ${p_port_name}] 0]}] } {
        set port_conflict_ver [lindex ${port_conflict_ver_info} 1]_[lindex ${port_conflict_ver_info} 2]
        ui_info "${p_port_name} active version: ${port_conflict_ver}"

        if { [vercmp ${port_conflict_ver} ${p_conflict_ver_min}] >= 0
            && [vercmp ${port_conflict_ver} ${p_conflict_ver_max}] <= 0 } {

            ui_info "${p_port_name} conflicts; declare build conflict"
            conflicts_build-append \
                ${p_port_name}
        }
    }
}

port_conflict_check     cdrtools 0.0 3.01_1

# https://trac.macports.org/ticket/31616
compiler.blacklist-append\
                        llvm-gcc-4.2 macports-llvm-gcc-4.2 {clang < 300}

configure.ldflags-append -lintl

pre-build {
    if { $universal_possible && [variant_isset universal] } {
#       Big Sur and Later
        if { ${os.platform} eq "darwin" && ${os.major} >= 20 } {
            platform i386 {
                build.args-append   COPTX="-arch x86_64 -arch arm64" \
                                    C++OPTX="-arch x86_64 -arch arm64" \
                                    LDOPTX="-arch x86_64 -arch arm64"
            }
            platform arm {
                build.args-append   COPTX="-arch x86_64 -arch arm64" \
                                    C++OPTX="-arch x86_64 -arch arm64" \
                                    LDOPTX="-arch x86_64 -arch arm64"
            }
#       Lion to Catalina
        } elseif { ${os.platform} eq "darwin" && ${os.major} >=11 } {
            platform i386 {
                build.args-append   COPTX="-arch x86_64 -arch i386" \
                                    C++OPTX="-arch x86_64 -arch i386" \
                                    LDOPTX="-arch x86_64 -arch i386"
            }
#       Snow Leopard
        } elseif { ${os.platform} eq "darwin" && ${os.major} ==10 } {
            platform i386 {
                build.args-append   COPTX="-arch x86_64 -arch i386" \
                                    C++OPTX="-arch x86_64 -arch i386" \
                                    LDOPTX="-arch x86_64 -arch i386"
            }
#           Note: Snow Leopard can only run 32bit ppc apps via Rosetta
            platform powerpc {
                build.args-append   COPTX="-arch ppc -arch i386" \
                                    C++OPTX="-arch ppc -arch i386" \
                                    LDOPTX="-arch ppc -arch i386"
            }
#       Tiger and Leopard
        } elseif { ${os.platform} eq "darwin" && ${os.major} >= 8 } {
#           Note: One could change ppc to ppc64 if so desired
            platform i386 {
                build.args-append   COPTX="-arch ppc -arch i386" \
                                    C++OPTX="-arch ppc -arch i386" \
                                    LDOPTX="-arch ppc -arch i386"
            }
#           Note: One could change ppc to ppc64 if so desired
            platform powerpc {
                build.args-append   COPTX="-arch ppc -arch i386" \
                                    C++OPTX="-arch ppc -arch i386" \
                                    LDOPTX="-arch ppc -arch i386"
            }
#       Panther and earlier
        } else {
            platform powerpc {
                build.args-append   COPTX="-arch ppc -arch ppc64" \
                                    C++OPTX="-arch ppc -arch ppc64" \
                                    LDOPTX="-arch ppc -arch ppc64"
            }
        }
    }
}

depends_build-append    port:gmake \
                        port:gettext
                        
depends_lib-append      port:gettext-runtime

build.type              gnu
                        
build.args-append       INS_BASE="${destroot}${prefix}" \
                        INS_RBASE="${destroot}${prefix}" \
                        DEFOSINCDIRS="${prefix}/include" \
                        CC_OPT=${configure.optflags}

destroot.destdir        INS_BASE="${destroot}${prefix}" \
                        INS_RBASE="${destroot}${prefix}" \
                        MANDIR=man \
                        DEFINSUSR=${install.user} \
                        DEFINSGRP=${install.group}

# NOTE:  This port first builds a bootstrapped version of smake using gmake.
#        The final version of smake (and its static associated libraries) are built using
#        the boostrapped version of smake which does NOT support parallel building.
#        i.e.  the j flag is ignored by the boostrapped version of smake and
#              the final version of smake.
use_parallel_build      no

destroot {
    xinstall -m 0755    {*}[glob ${worksrcpath}/smake/OBJ/*/smake] \
                        ${destroot}${prefix}/bin

    xinstall -m 0644    {*}[glob ${worksrcpath}/smake/OBJ/*/*/smake.1] \
                        ${destroot}${prefix}/share/man/man1

    xinstall -m 0644    {*}[glob ${worksrcpath}/man/man5/OBJ/*/*/makefiles.5] \
                        {*}[glob ${worksrcpath}/man/man5/OBJ/*/*/makerules.5] \
                        ${destroot}${prefix}/share/man/man5
}
