# -*- 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-fors-fims
conflicts       eso-fors-fims-devel
version         107.0
revision        13

set instrument      fors
set fimsVersion     306603
set autoposVersion  250924
set pafchkVersion   303764

categories      science
license         GPL-2+
platforms       darwin
maintainers     eso.org:usd-help
description     ESO's FORS Observation Preparation software
long_description ESO's FORS Observation Preparation software. FIMS is used to generate configuration files that specify target acquisition and focal plane assembly setups for many FORS configurations, including MOS, MXU, PMOS and imaging observations with occulting bars. For the MXU mode, FIMS also produces the file which specifies how to manufacture the MXU mask on the laser punching machine.
homepage        http://www.eso.org/sci/observing/phase2/SMGuidelines/FIMS.FORS.html

set __eso_prefix                  ""
set __prefix                      ${prefix}/libexec/${__eso_prefix}eso/fims-${version}
set _skycat_dir                   ${prefix}/libexec/${__eso_prefix}eso/skycat
set _tcltk_dir                    ${__prefix}/libexec/${__eso_prefix}eso/tcltk-8.4
set eso_period                    107
set TCLTK_REQVER                  8.4
set SKYCAT_REQVER                 3.1.4
set INSUSER                       FIMS
set SKYCAT_CFG                    http://archive.eso.org/skycat/skycat.cfg

set uname_m         [exec uname -m]

distname        eso-${instrument}-fims-${version}
master_sites        https://ftp.eso.org/pub/usg/fims/P107
# master_sites    https://ftp.eso.org/pub/usg/fims/P${eso_period}
dist_subdir     eso-${instrument}-fims
distfiles       fimsLIN64_P${version}${extract.suffix}


checksums       fimsLIN64_P${version}${extract.suffix} \
                sha256  f96e8f16c837e55651b88d4f96b6097b075be681747d01c806c2c03d3325042e
					
compilers.choose    cc cxx
compilers.setup     -g95 -clang37
		  
worksrcdir          eso-${instrument}-fims-${version}

post-extract {
   system "/usr/bin/tar -C ${workpath} -zxvf ${workpath}/fimsLIN64.tar.gz \
     fimsLIN64/src/autopos-${autoposVersion}.tar.gz \
     fimsLIN64/src/fims-${fimsVersion}.tar.gz \
     fimsLIN64/src/fims.sh.tpl \
     fimsLIN64/src/pafchk-${pafchkVersion}.tar.gz \
     fimsLIN64/config/FORS1.isf \
     fimsLIN64/config/FORS2.isf \
     fimsLIN64/doc/\* \
     fimsLIN64/bitmap/\* \
     "

   system "/usr/bin/tar -C ${workpath} -zxvf ${workpath}/fimsLIN64/src/autopos-${autoposVersion}.tar.gz"
   system "/usr/bin/tar -C ${workpath} -zxvf ${workpath}/fimsLIN64/src/fims-${fimsVersion}.tar.gz"
   system "/usr/bin/tar -C ${workpath} -zxvf ${workpath}/fimsLIN64/src/pafchk-${pafchkVersion}.tar.gz"
}

post-patch {
  reinplace "s%CC =%CXX =%"                      ${workpath}/autopos/src/makefile.novlt
  reinplace "s%\$(CC)%\$(CXX)%"                  ${workpath}/autopos/src/makefile.novlt
  reinplace "s%^CCFLAG.*%\& -D_DARWIN_C_SOURCE%" ${workpath}/autopos/src/makefile.novlt
}

depends_run      port:xorg-server port:${__eso_prefix}eso-fsmosaic port:${__eso_prefix}eso-skycat

configure {
   reinplace "s%#!/vlt/JAN2006/gnu/bin/perl%#!${prefix}/bin/perl%" ${workpath}/fims/src/gerber2eps.perl
   reinplace "s%#/usr/contrib/bin/perl%#!${prefix}/bin/perl%"      ${workpath}/fims/src/gerber2ps.perl
}

build {
   system "cd ${workpath}/autopos/src && make -w CC=${configure.cc} CXX=${configure.cxx} -f makefile.novlt all"
   system "cd ${workpath}/pafchk/src && make -w CC=${configure.cc} CXX=${configure.cxx} -f makefile.novlt all"
   system "cd ${workpath}/fims/src && make -w CC=${configure.cc} CXX=${configure.cxx} -f makefile.novlt all"

   copy ${workpath}/fimsLIN64/src/fims.sh.tpl ${workpath}/fims.sh
   reinplace "s#@@FIMSBASEPATH@@#${__prefix}/${uname_m}#" ${workpath}/fims.sh
   reinplace "s#@@TCLPATH@@#${__prefix}/${uname_m}/lib:${_tcltk_dir}/lib#" ${workpath}/fims.sh
   reinplace "s#@@BINPATH@@#${__prefix}/${uname_m}/bin:${_skycat_dir}/bin:${_tcltk_dir}/bin#" ${workpath}/fims.sh
   reinplace "s#@@LIBPATH@@#${__prefix}/${uname_m}/lib:${_skycat_dir}/lib:${_tcltk_dir}/lib#" ${workpath}/fims.sh
   reinplace "s#@@USER@@#${INSUSER}#" ${workpath}/fims.sh
   reinplace "s#@@TCLTKREQVER@@#${TCLTK_REQVER}#" ${workpath}/fims.sh
   reinplace "s#@@SKYCATREQVER@@#${SKYCAT_REQVER}#" ${workpath}/fims.sh
   reinplace "s@^bashlib=.*$@#&\\nbashlib=${__prefix}/${uname_m}/bin/bash_library.sh@" ${workpath}/fims.sh
   reinplace "s@^FC_PLUGIN@#&@" ${workpath}/fims.sh

   delete ${workpath}/fimsLIN64/doc/skycat_version.txt
}

destroot {
   xinstall -d ${destroot}${__prefix}/${uname_m}/bin
   xinstall -d ${destroot}${__prefix}/${uname_m}/lib
   xinstall -d ${destroot}${__prefix}/bitmap
   xinstall -d ${destroot}${__prefix}/config
   xinstall -d ${destroot}${__prefix}/doc

   xinstall -p ${workpath}/autopos/bin/AP ${destroot}${__prefix}/${uname_m}/bin/

   xinstall -p {*}[glob ${workpath}/pafchk/bin/*] ${destroot}${__prefix}/${uname_m}/bin/
   xinstall -p -m 644 ${workpath}/pafchk/lib/libpafchk.a ${destroot}${__prefix}/${uname_m}/lib/
   xinstall -p ${workpath}/pafchk/lib/libpafchk.so       ${destroot}${__prefix}/${uname_m}/lib/

   
   xinstall -p ${workpath}/fims.sh                                  ${destroot}${__prefix}/${uname_m}/bin/
   xinstall -p ${workpath}/bash_library.sh                          ${destroot}${__prefix}/${uname_m}/bin/   
   xinstall -p -m 755 {*}[glob ${workpath}/fims/bin/*.{sh,perl}]    ${destroot}${__prefix}/${uname_m}/bin/
   xinstall -p -m 644 {*}[glob ${workpath}/fims/bin/*.prg]          ${destroot}${__prefix}/${uname_m}/bin/
   xinstall -d ${destroot}${__prefix}/${uname_m}/lib/libesoPlugins.tcl
   xinstall -d ${destroot}${__prefix}/${uname_m}/lib/libfimsPlugins.tcl
   xinstall -p -m 644 {*}[glob ${workpath}/fims/lib/libesoPlugins.tcl/*]  ${destroot}${__prefix}/${uname_m}/lib/libesoPlugins.tcl/
   xinstall -p -m 644 {*}[glob ${workpath}/fims/lib/libfimsPlugins.tcl/*] ${destroot}${__prefix}/${uname_m}/lib/libfimsPlugins.tcl/
   xinstall -p -m 644 {*}[glob ${workpath}/fims/config/*]                 ${destroot}${__prefix}/config
   xinstall -p -m 644 {*}[glob ${workpath}/fimsLIN64/doc/*]               ${destroot}${__prefix}/doc/
   xinstall -p -m 644 {*}[glob ${workpath}/fimsLIN64/bitmap/*]            ${destroot}${__prefix}/bitmap/

   xinstall -p -m 644 ${workpath}/fimsLIN64/config/FORS1.isf ${destroot}${__prefix}/config/
   xinstall -p -m 644 ${workpath}/fimsLIN64/config/FORS2.isf ${destroot}${__prefix}/config/


   ln -s ${__prefix}/${uname_m}/bin/fims.sh ${destroot}${prefix}/bin/${__eso_prefix}fims.sh
}

use_parallel_build  no
