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

categories          lang
license             Apache-2
maintainers         {easieste @easye} openmaintainer
supported_archs     x86_64 ppc ppc64
description         The Clozure Common Lisp Implementation
long_description    Clozure CL is an open source Common Lisp implementation. \
                    Clozure CL used to be called OpenMCL. It is also sometimes \
                    called CCL. You will see the three names being used interchangeably.
homepage            https://github.com/Clozure/ccl

if {${os.arch} ne "powerpc"} {
    PortGroup       github 1.0

    github.setup    Clozure ccl 1.12.2 v
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    revision        0

    checksums   ${name}-${version}${extract.suffix} \
        rmd160  e1042b06a11e914bc9047633d4b5a6c5bb9ff727 \
        sha256  7f0b764b5f9d049cfb4382b8395091b0c81d754183924e4611f5590eb25b18c2 \
        size    5017262
    dist_subdir     ${name}/${version}
    worksrcdir      ccl-${version}

    patchfiles  patch-macports-xdg-data-dir.diff

    post-patch {
        reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/tools/asdf.lisp
    }
}

# Pegged, do not update:
platform darwin powerpc {
    if {${os.major} > 8} {
        version     1.6

    checksums   ${name}-${version}-darwinppc${extract.suffix} \
        rmd160  8ec32a06899c7a0687221257561dabf4cff23626 \
        sha256  563ed539c588df6137612f957d66554c6e453b1b17adc14d2c1773b395732e43 \
        size    124226791
    } else {
        version     1.4

    checksums   ${name}-${version}-darwinppc${extract.suffix} \
        rmd160  be383bbbaa002cae14ad8c2faffa407f687a6a4e \
        sha256  6033422accffe8ab2d4d176f3673da48afd35408e9675c6594d775037e5c26bf \
        size    71167804
    }
    master_sites    https://ccl.clozure.com/ftp/pub/release/${version}/
    distname        ${name}-${version}-darwinppc
    worksrcdir      ccl
}

set shortversion    ${version}

use_configure       no

platform darwin i386 {
    compiler.whitelist    macports-clang-9.0
}

platform darwin  {
    global bootimg
    global ccl_script
    
    if { ${configure.build_arch} eq "x86_64" } {
        master_sites-append  https://github.com/Clozure/ccl/releases/download/v${version}/:ccl_x86_64
        distfiles-append     darwinx86${extract.suffix}:ccl_x86_64
        checksums-append     darwinx86${extract.suffix} \
            rmd160  6d3e739caa2427801abdfbe859da8e658e74422e \
            sha256  428406380e64e42b1a5c202b7da807bfe8a5de507a466ad873f6292e389b1b2b \
            size    18906289
        # This tar has no top-level directory, so we have to extract it manually
        extract.only-delete  darwinx86${extract.suffix}
        set bootimg          dx86cl64
        set ccl_script       ccl64
    } elseif { ${configure.build_arch} == "ppc64" } {
        set bootimg          dppccl64
        set ccl_script       ccl64
    } elseif { ${configure.build_arch} == "ppc" } {
        set bootimg          dppccl
        set ccl_script       ccl
    } else {
        pre-fetch {
            return -code error "architecture ${configure.build_arch} has not been ported to ${name}"
        }
    }
}

post-extract {
    if { ${configure.build_arch} eq "x86_64" } {
        system "cd ${worksrcpath} && tar -x -f ${distpath}/darwinx86${extract.suffix}"
    }
}

build {
    if { ${configure.build_arch} eq "x86_64" } {
        system "cd ${worksrcpath}/lisp-kernel/darwinx8664 && make clean && make"
        system "cd ${worksrcpath} && echo '(ccl::rebuild-ccl :clean t)' | ./${bootimg} --batch"
        # FIXME: doc target no longer exists with ccl-1.11
        #
        # Generating the manual would involve using ccl with Quicklisp to run.
        # https://github.com/Clozure/ccldoc
    } else {
        # PPC:
        system "cd ${worksrcpath} && echo '(ccl::rebuild-ccl :full t)' | ./${bootimg} --batch"
    }
}

set prefixpath  \\\"[regsub -all / [strsed ${prefix}/share/asdf-install {g/^\///}] "\\\" \\\""]\\\"

destroot {
    if { ${configure.build_arch} eq "x86_64" } {
        set install_root ${prefix}/share/${name}/${shortversion}
        set stage_root ${destroot}${install_root}
        file mkdir ${stage_root}
        system "cd ${worksrcpath} && tar -c -f - . | (cd ${stage_root} && tar -x -f -)"
        set ccl_wrapper ${worksrcpath}/scripts/${ccl_script}
        reinplace "s|/usr/local/src/ccl|${install_root}|" ${ccl_wrapper}
        system "chmod +x ${ccl_wrapper}"
        file copy ${ccl_wrapper} ${destroot}${prefix}/bin

        xinstall -d ${destroot}${prefix}/share/doc/${name}
        xinstall -d ${destroot}${prefix}/share/examples
        xinstall -m 644 -W ${worksrcpath}/doc \
            release-notes-1.1.txt release-notes-1.2.txt release-notes.txt \
            compiler-changes-in-CCL-1.12-trunk.pdf \
            ${destroot}${prefix}/share/doc/${name}
        file copy ${worksrcpath}/examples ${destroot}${prefix}/share/examples/${name}
    } else {
        # PPC:
        file mkdir ${destroot}${prefix}/share/${name}/${shortversion}
        system "cd ${worksrcpath} && tar cf - . | (cd ${destroot}${prefix}/share/${name}/${shortversion} && tar xf -)"
        reinplace "s|/usr/local/src/ccl|${prefix}/share/${name}/${shortversion}|" ${workpath}/ccl/scripts/${ccl_script}
        system "chmod +x ${workpath}/ccl/scripts/${ccl_script}"
        file copy ${workpath}/ccl/scripts/${ccl_script} ${destroot}${prefix}/bin
        reinplace "s|\"usr\" \"local\" \"asdf-install\")))))|${prefixpath})))))|" \
            ${destroot}${prefix}/share/${name}/${shortversion}/tools/asdf-install/installer.lisp
    }
}

post-activate {
    if { ${configure.build_arch} in [list ppc ppc64] } {
        # Prepare asdf install folders and so forth.
        file mkdir ${prefix}/share/asdf-install
        system "chgrp admin ${prefix}/share/asdf-install"
        system "chmod g+w ${prefix}/share/asdf-install"
    }
}
