#!/bin/sh
#
# $Id: mkfcphysall.sh 15 2008-03-17 15:35:20Z pletzer $
#
# For building all external packages that facets depends on

# Store top directory
TOPDIR=$PWD

# The following variables should be set by a configure system
if test -z "$FCPREFIX"; then
  FCPREFIX=$HOME/software
fi

# Flags on os
case `uname` in

  Darwin)
    RPATH_FLAG=-L
    LTRPATH_FLAG=-L
    FCINSTALLOPT="INSTALL=/usr/bin/install"
    ;;

  Linux)
    RPATH_FLAG=-Wl,--rpath,
    LTRPATH_FLAG=-Wl,-rpath,
    ;;

esac

# Exit on failure
exitonfailure () {
  if test $1 != 0; then
    if test -n "$2"; then
      echo $2
      exit
    fi
    BUILD_FAILURES="$BUILD_FAILURES $3"
  fi
}

#
# We do not need the -built files (timestamps) because
# "make depend" of each project will take care of this
#

# Build bhssolver
cd $TOPDIR/bhssolver
BHSSOLVER_VERSION=`grep ^AM_INIT_AUTOMAKE configure.ac | sed -e 's/^.*, //' -e 's/)//'`
echo BHSSOLVER_VERSION = $BHSSOLVER_VERSION
if test ! -f configure -o configure -ot configure.ac; then
  config/cleanconf.sh
fi
# Versions
mkdir -p build
cd build
# if test ! -f Makefile; then
  ../configure --prefix=$FCPREFIX/bhssolver-$BHSSOLVER_VERSION
  exitonfailure $? "bhssolver failed to configure.  Examine logs in $TOPDIR/bhssolver/build."
# fi
make depend all
exitonfailure $? "bhssolver failed to build.  Examine logs in $TOPDIR/bhssolver/build."
make install
rm -rf $FCPREFIX/bhssolver
ln -s $FCPREFIX/bhssolver-$BHSSOLVER_VERSION $FCPREFIX/bhssolver

# Build fmcfm
cd $TOPDIR/fmcfm/transport
FMCFM_VERSION=`grep ^AM_INIT_AUTOMAKE configure.ac | sed -e 's/^.*, //' -e 's/)//'`
echo FMCFM_VERSION = $FMCFM_VERSION
# if test ! -f configure; then
if test ! -f configure -o configure -ot configure.ac; then
  config/cleanconf.sh
fi
mkdir -p build
cd build
# if test ! -f Makefile; then
  ../configure --prefix=$FCPREFIX/fmcfm-$FMCFM_VERSION
  exitonfailure $? "fmcfm failed to configure.  Examine logs in $TOPDIR/fmcfm/build."
# fi
make all
exitonfailure $? "fmcfm failed to build.  Examine logs in $TOPDIR/fmcfm/build."
make install
rm -rf $FCPREFIX/fmcfm
ln -s $FCPREFIX/fmcfm-$FMCFM_VERSION $FCPREFIX/fmcfm

# Build serial uedge.
cd $TOPDIR/uedge
UEDGE_VERSION=`grep ^AM_INIT_AUTOMAKE configure.ac | sed -e 's/^.*, //' -e 's/)//'`
echo UEDGE_VERSION = $UEDGE_VERSION
# if test ! -f configure; then
if test ! -f configure -o configure -ot configure.ac; then
  config/cleanconf.sh
fi
mkdir -p build
cd build
# if test ! -f Makefile; then
  ../configure --prefix=$FCPREFIX/uedge-$UEDGE_VERSION --with-hdf5-dir=$FCPREFIX/hdf5
  exitonfailure $?  "uedge failed to configure.  Examine logs in $TOPDIR/uedge."
# fi
make
# make install
# rm -rf $FCPREFIX/uedge
# ln -s $FCPREFIX/uedge-4.9.1 $FCPREFIX/uedge

# Build nubeam.
cd $TOPDIR/nubeam
NUBEAM_VERSION=`grep ^AC_INIT configure.ac | awk -F',' '{print $2}'`
echo NUBEAM_VERSION = $NUBEAM_VERSION
unset redoautotools
if test ! -f configure -o configure -ot configure.ac; then
  redoautotools=yes
else
  for i in Makefile.am */Makefile.am; do
    infile=`echo $i | sed 's/am$/in/'`
    if test ! -f $infile -o $infile -ot $i; then
      redoautotools=yes
      break
    fi
  done
fi
if test -n "$redoautotools"; then
  config/cleanconf.sh
fi
### # serial build
### mkdir -p serial
### cd serial
### # if test ! -f Makefile; then
### ../configure --prefix=$FCPREFIX/nubeam-$NUBEAM_VERSION-serial \
###   
### exitonfailure $?  "nubeam failed to configure.  Examine logs in $TOPDIR/nubeam/serial."
### make depend all install
### # fi
# parallel build
mkdir -p parallel
cd parallel
MPIF90=$FCPREFIX/mpi/bin/mpif90
MPICC=$FCPREFIX/mpi/bin/mpicc
MPICXX=$FCPREFIX/mpi/bin/mpicxx
# if test ! -f Makefile; then
## 
## Note: as of 14 Math 2008 nubeam requires -fno-range-check with gcc 4.3
## because openmpi was built with disable-shared, must also disable-shared here
../configure --prefix=$FCPREFIX/nubeam-$NUBEAM_VERSION-parallel \
 --enable-parallel FC=$MPIF90 F77=$MPIF90 CC=$MPICC CXX=$MPICXX \
 --with-FFLAGS="-O -fno-range-check" --disable-shared
exitonfailure $?  "nubeam failed to configure.  Examine logs in $TOPDIR/nubeam/parallel."
make install
ln -s $FCPREFIX/nubeam-$NUBEAM_VERSION-parallel $FCPREFIX/nubeam
# fi

# Build facets.
cd $TOPDIR/facets
FACETS_VERSION=`grep ^AM_INIT_AUTOMAKE configure.ac | sed -e 's/^.*, //' -e 's/)//'`
echo FACETS_VERSION = $FACETS_VERSION
unset redoautotools
if test ! -f configure -o configure -ot configure.ac; then
  redoautotools=yes
else
  for i in Makefile.am */Makefile.am; do
    infile=`echo $i | sed 's/am$/in/'`
    if test ! -f $infile -o $infile -ot $i; then
      redoautotools=yes
      break
    fi
  done
fi
if test -n "$redoautotools"; then
  config/cleanconf.sh
fi
# serial build
mkdir -p serial
cd serial
# if test ! -f Makefile; then
../configure --prefix=$FCPREFIX/facets-$FACETS_VERSION --disable-parallel --with-txbase-dir=$FCPREFIX/txbase --with-hdf5-dir=$FCPREFIX/hdf5 --with-petsc-dir=$FCPREFIX/petsc --with-bhssolver-dir=$FCPREFIX/bhssolver --with-fmcfm-dir=$FCPREFIX/fmcfm
make depend all install
# fi
# parallel build
mkdir -p ../parallel
cd ../parallel
# if test ! -f Makefile; then
../configure --prefix=$FCPREFIX/facets-$FACETS_VERSION --with-txbase-dir=$FCPREFIX/txbase --enable-parallel --with-hdf5-dir=$FCPREFIX/hdf5mpi --with-petsc-dir=$FCPREFIX/petscmpi --with-bhssolver-dir=$FCPREFIX/bhssolver --with-fmcfm-dir=$FCPREFIX/fmcfm
make depend all install
# fi


# Fix perms
chmod -R a+r $FCPREFIX

# Summary

echo --------------------------------------
echo
echo Don\'t forget to add $PYADDPATH to your PYTHONPATH and
echo $FCPREFIX/bin and $FCPREFIX/mpi/bin to your PATH!
echo
echo --------------------------------------

