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

PortSystem          1.0

name                fpc
version             2.6.4
revision            1
categories          lang
platforms           darwin
license             GPL-2 LGPL-2
maintainers         takeshi openmaintainer
description         free pascal
long_description    Open source compiler for Pascal and Object Pascal
homepage            http://www.freepascal.org
master_sites        sourceforge:freepascal

set src             ${name}build-${version}.tar.gz
set pp              universal-macosx-10.5-ppcuniversal.tar.bz2
distfiles           ${src} ${pp}
checksums           ${src} \
                    rmd160  339b71d3c9983a720eed7c6bda1cfd330d89966f \
                    sha256  8d9c3bcfa469d9b37c05663e2775d179809e4b3443604fac7d21aa64c9a56daa \
                    ${pp} \
                    rmd160  0124c2f4447ba7f96e3db0a27e6bff459c5749f4 \
                    sha256  e7243e83e6a04de147ebab7530754ec92cd1fbabbc9b6b00a3f90a796312f3e9

extract.only        ${src}
                    
use_configure       no
universal_variant   no

post-extract {
    system -W ${workpath} "bzip2 -dc ${distpath}/${pp} | tar xf -"
}

worksrcdir          ${name}build-${version}/fpcsrc
set fpcbasepath     ${prefix}/lib/${name}
build.env           PP=${workpath}/ppcuniversal \
                    PREFIX=${destroot}${fpcbasepath}
build.target        all

destroot.env        ${build.env}

# Yosemite fix: fpc's build system checkes MACOSX_DEPLOYMENT_TARGET and halts if > 10.9
platform darwin {
    if {${os.major} > 13} {
        macosx_deployment_target    10.9
    }
}

post-destroot {
# create a symlink to the architecture dependent executable
    switch ${build_arch} {
        "x86_64" {
            set b "ppcx64"
        } 
        "i386" {
            set b "ppc386"
        }
        default {
            set b "ppcppc"
        }
    }
    ln -s ${fpcbasepath}/lib/${name}/${version}/${b} ${destroot}${fpcbasepath}/bin
# generate a configuration file
    xinstall -d ${destroot}${fpcbasepath}/etc
    system "
        ${destroot}${fpcbasepath}/bin/fpcmkcfg \
            -d basepath=${fpcbasepath}/lib/${name}/${version} \
            -o ${destroot}${fpcbasepath}/etc/fpc.cfg
    "
# install man
    xinstall -d ${destroot}${fpcbasepath}/man
    foreach d {1 5} {
        file copy ${workpath}/${name}build-${version}/install/man/man${d} ${destroot}${fpcbasepath}/man
        foreach f [glob ${destroot}${fpcbasepath}/man/man${d}/*.${d}] {
            system "/usr/bin/gzip ${f}"
        }
    }
    ln -s ${fpcbasepath}/man/man1/fpc.1.gz ${destroot}${prefix}/share/man/man1
    ln -s ${fpcbasepath}/man/man5/fpc.cfg.5.gz ${destroot}${prefix}/share/man/man5
}

notes "
    The compiler fpc looks for the fpc.cfg file in the following places: \n\
    - The current directory. \n\
    - Home directory, looks for .fpc.cfg \n\
    - The directory specified in the environment variable PPC_CONFIG_PATH, \n\
      and if it's not set under compilerdir/../etc. \n\
    - If it is not yet found: in /etc. \n\
    \n\
    All the files of fpc are installed in ${fpcbasepath} and \n\
    the executables are installed in ${fpcbasepath}/bin. \n\
    The fpc.cfg is installed in ${fpcbasepath}/etc to make fpc find fpc.cfg. \n\
    To customize, use ~/.fpc.cfg or /etc/fpc.cfg and call ${fpcbasepath}/bin/fpc directly.
"