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

# Please keep the ksh93 and ksh93-devel ports as similar as possible.

epoch               2
github.setup        att ast 2012-08-01
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            2

name                ksh93
version             93u+[string map {- {}} ${github.version}]
conflicts           ksh ksh-devel ksh93-devel pdksh
categories          shells
platforms           darwin freebsd
maintainers         {ryandesign @ryandesign} openmaintainer
license             EPL-1

description         the AT&T KornShell

long_description    This is ksh 93u+, the last official version of \
                    KornShell by David Korn and AT&T Research.

master_sites        macports_distfiles
distfiles           ast-ksh.${github.version}.tgz \
                    INIT.${github.version}.tgz
checksums           ast-ksh.${github.version}.tgz \
                    rmd160  74bf4735182a6b527e3eb5c5ae6d7854ea72d78b \
                    sha256  e6192cfa52a6a9fd20618cbaf3fa81f0cc9fd83525500757e83017275e962851 \
                    size    2053532 \
                    INIT.${github.version}.tgz \
                    rmd160  48f1830bc85a26125dd986256d38be435668d445 \
                    sha256  c40cf57e9b2186271a9c362a560aa4a6e25ba911a8258ab931d2bbdbce44cfe5 \
                    size    377769

extract.mkdir       yes

# Most of these patchfiles are from Apple's ksh 93u+ build.
patchfiles-append   ksh-20120801-cve-2019-14868.patch \
                    ksh-20120801-subshell-leak.patch \
                    memccpy.patch \
                    src__cmd__ksh93__jobs.c.diff \
                    src__cmd__ksh93__parse.c.diff \
                    src__cmd__ksh93__sh__array.c.diff \
                    src__lib__libast__comp__omitted.c.diff \
                    src__lib__libast__features__common.diff \
                    src__lib__libast__features__lib.diff \
                    src__lib__libast__sfio__sfvprintf.c.diff

use_configure       no

# Apple uses this flag in their ksh 93u+ build. The problem with
# enabling this feature has been fixed in the ksh 93u+m fork.
configure.cflags-append \
                    -DSHOPT_SPAWN=0

# Apple uses this flag in their ksh 93u+ build. The ksh 93u+m fork
# believes it to be a no-op, but keeping it here in case it wasn't a
# no-op back in 93u+.
configure.cflags-append \
                    -D_ast_int8_t=int64_t

# This may be easy to fix with a patch; I haven't investigated. It's
# already fixed in the ksh 93u+m fork.
# error: implicit declaration of function 'ioctl' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
supported_archs     i386 ppc ppc64 x86_64
configure.cflags-append \
                    -Wno-error=implicit-function-declaration

build.cmd           bin/package
build.target        make

# Build output is misleading: You will see "CC=cc" and builds being done
# by "cc", but that "cc" is a wrapper script created by the build system
# which in turn calls the compiler we specify here.
build.env           "CC=${configure.cc}" \
                    "CCFLAGS=${configure.cflags} [get_canonical_archflags cc]" \
                    "LDFLAGS=${configure.ldflags} [get_canonical_archflags ld]" \
                    "NPROC=${build.jobs}"
build.args          SHELL="/bin/sh"

post-build {
    # The build system does not exit with an error if the build fails.
    # The following is adapted from Apple's ksh build Makefile.
    if {[catch {system -W ${build.dir} "fgrep '*** exit code' arch/${ksharch}/lib/package/gen/make.out && exit 1 || exit 0"}]} {
        return -code error "build failed"
    }
}

destroot {
    xinstall -m 0755 ${build.dir}/arch/${ksharch}/bin/ksh \
        ${destroot}${prefix}/bin
    xinstall -m 0444 ${build.dir}/arch/${ksharch}/man/man1/sh.1 \
        ${destroot}${prefix}/share/man/man1/ksh.1
}

options ksharch
default ksharch {[ksharch]}

proc ksharch {} {
    global build.cmd build.dir build.env
    return [exec env {*}${build.env} ${build.dir}/${build.cmd} host type]
}

livecheck.type      none