# -*- 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
PortGroup       compilers 1.0

name            eso-flames-fposs
conflicts       eso-flames-fposs-devel
version         115.0.3
# -*- 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: eso-flames-fposs.portfile 294148 2025-01-16 20:42:45Z jpritcha $

set instrument      flames

set _name       ${instrument}-fposs
categories      science
license         GPL-2+
maintainers     eso.org:usd-help
homepage        http://www.eso.org/sci/observing/phase2/SMGuidelines/FPOSS.FLAMES.html
description     ESO's FLAMES Observation Preparation software
long_description ESO's FLAMES Observation Preparation software.

master_sites        https://ftp.eso.org/pub/usg/fposs
distname            ${instrument}-fposs-${version}
distfiles           fposs-${version}.tar.gz

checksums       sha256  9d16463dacfaa493a2221c352b2aa323bd0edd2f86c3d1fcafe6588a11f0989b

set worksrcpath          ${workpath}/fposs-${version}

compilers.choose    cc cxx
compilers.setup     gcc

configure.cflags-append    -std=c11
configure.cxxflags-append  -std=c11

# As at 2023-11-15:
# Currently seg' faults when built with gcc-mp-12/13
# Apple gcc compiler seems to work fine (at least on Sonoma + Xcode-15)
# default_variants  +${compilers.gcc_default}
use_parallel_build      no

depends_run       port:tk port:tcl \
                  port:xorg-server \
                  port:coreutils

variant xquartz   description {Use this variant if you have XQuartz installed.} {
  depends_run-remove port:xorg-server
  depends_run-append port:xorg-libX11
}


configure {
  reinplace "s@\$(CC) \$(CFLAGS) -c \$(PAFCHK_MODULE)@\$(CC) -DFLAMES_ONLY -DDRAMA_BUILD \$(CWARNINGS) \$(COPT) -I\$(PAFCHK_MODULE)/include -c \$(PAFCHK_MODULE)@" \
    ${worksrcpath}/fposs/src/Makefile_Darwin
  reinplace "s@^CC\[\[:space:\]\].*=.*@CC = ${configure.cc}@" \
    ${worksrcpath}/fposs/src/Makefile_Darwin
  reinplace "s@gcc-mp-\[0-9\]*@${configure.cc}@g" \
    ${worksrcpath}/pafchk/src/makefile.novlt
}

build {
  reinplace "s?@@FPOSSBASEPATH@@?${prefix}/libexec/${name}/lib/fposs?"              ${worksrcpath}/fposs/scripts/fposs.sh.tpl
  reinplace "s?@@BINPATH@@?${prefix}/libexec/${name}/lib/fposs/bin?"                ${worksrcpath}/fposs/scripts/fposs.sh.tpl
  reinplace "s?@@LIBPATH@@?${prefix}/libexec/${name}/lib/fposs/lib?"                ${worksrcpath}/fposs/scripts/fposs.sh.tpl
  reinplace "s?@@USER@@?.fposs?"                                                    ${worksrcpath}/fposs/scripts/fposs.sh.tpl
  reinplace "s?../doc?doc?"                                                         ${worksrcpath}/fposs/scripts/fposs.sh.tpl
  reinplace "s@^bashlib=.*$@bashlib=${prefix}/libexec/${name}/bin/bash_library.sh@" ${worksrcpath}/fposs/scripts/fposs.sh.tpl
  system "cd ${worksrcpath}/pafchk/src && \
      CFLAGS='${configure.cflags}' CXXFLAGS='${configure.cxxflags}' \
      make -f makefile.novlt"
  system "cd ${worksrcpath}/fposs/src && \
      CFLAGS='${configure.cflags}' CXXFLAGS='${configure.cxxflags}' \
      make"
}

destroot {
  system "cd ${worksrcpath}/fposs/src && make INTROOT='${destroot}${prefix}/libexec/${name}' install"
  system "/usr/bin/install                                                   -d ${destroot}${prefix}/bin"
  system "/usr/bin/install                                                   -d ${destroot}${prefix}/libexec/${name}/bin"
  system "/usr/bin/install ${worksrcpath}/fposs/scripts/fposs.sh.tpl            ${destroot}${prefix}/libexec/${name}/bin/fposs.sh"
  system "/usr/bin/install ${worksrcpath}/fposs/scripts/bash_library.sh         ${destroot}${prefix}/libexec/${name}/bin"
  system "/usr/bin/install ${worksrcpath}/fposs/scripts/fposs_fixINSfilename.sh ${destroot}${prefix}/libexec/${name}/bin"
  ln -s ${prefix}/libexec/${name}/bin/fposs.sh                                  ${destroot}${prefix}/bin
  ln -s ${prefix}/libexec/${name}/bin/fposs_fixINSfilename.sh                   ${destroot}${prefix}/bin
  ln -s ${prefix}/libexec/${name}/share/fposs                                   ${destroot}${prefix}/share/fposs
}
