#!/bin/bash

#Get number of cpus
NUMCPUS=$1;

#Some cleanup
rm -rf Dakota
rm -rf src
rm -rf install
mkdir src install

#Download from ISSM server
$ISSM_DIR/scripts/DownloadExternalPackage.py 'http://issm.jpl.nasa.gov/files/externalpackages/Dakota_4_3.src.tar.gz' 'Dakota_4_3.src.tar.gz'

#Untar 
tar -zxvf  Dakota_4_3.src.tar.gz

#Move Dakota to src directory
mv Dakota/* src
rm -rf Dakota

#Apply patches
patch src/src/ParallelLibrary.C ./ParallelLibrary.C.patch
patch src/src/ParallelLibrary.H ./ParallelLibrary.H.patch
patch src/src/NIDRProblemDescDB.C ./NIDRProblemDescDB.C.patch
patch src/src/NonDSampling.C ./NonDSampling.C.patch
patch src/src/NonDLocalReliability.C ./NonDLocalReliability.C.patch
patch src/src/NonDUnilevelRBDO.C ./NonDUnilevelRBDO.C.patch    #  source not even used?
#patch -R src/packages/Pecos/src/LHSDriver.cpp ./LHSDriver.cpp.patch

#Configure dakota
cd src
./configure \
--prefix="$ISSM_DIR/externalpackages/dakota/install/" \
--without-graphics  \
--with-pic \
--disable-mpi \
--with-blas="-L/usr/local/intel/mkl/10.1.2.024/lib/64 -lmkl_lapack -lmkl -lguide -lpthread" \
--with-lapack="-L/usr/local/intel/mkl/10.1.2.024/lib/64 -lmkl_lapack -lmkl -lguide -lpthread"
cd ..

#Before compiling, if running on 64 bits, we need to active fPIC compilation. Some packages 
#do not register -fPIC in Dakota, which is a problem. Edit the faulty Makefiles and add the -fPIC 
#flag to the compilation.
cat ./src/methods/NCSUOpt/Makefile | sed 's/FFLAGS = -g -O2/FFLAGS = -g -O2 -fPIC/g' >  temp
mv temp ./src/methods/NCSUOpt/Makefile

cat ./src/methods/acro/packages/pebbl/src/Makefile | sed 's/CXXFLAGS = -O2 -fpermissive/CXXFLAGS = -O2 -fpermissive -fPIC/g' > temp
mv temp ./src/methods/acro/packages/pebbl/src/Makefile

cat ./src/methods/hopspack/src-nappspack/Makefile | sed 's/CXXFLAGS = -g -O2/CXXFLAGS = -g -O2  -fPIC/g' > temp
mv temp ./src/methods/hopspack/src-nappspack/Makefile

cat ./src/methods/hopspack/src-cddlib/Makefile | sed 's/CFLAGS = -g -O2/CFLAGS = -g -O2 -fPIC/g' > temp
mv temp  ./src/methods/hopspack/src-cddlib/Makefile

cat ./src/methods/hopspack/src-shared/Makefile | sed 's/CFLAGS = -g -O2/CFLAGS = -g -O2 -fPIC/g' > temp
mv temp  ./src/methods/hopspack/src-shared/Makefile

cat ./src/methods/hopspack/src-shared/Makefile | sed 's/CXXFLAGS = -g -O2/CXXFLAGS = -g -O2  -fPIC/g' > temp
mv temp  ./src/methods/hopspack/src-shared/Makefile

cat ./src/methods/hopspack/src-conveyor/Makefile | sed 's/CXXFLAGS = -g -O2/CXXFLAGS = -g -O2 -fPIC/g' > temp
mv temp  ./src/methods/hopspack/src-conveyor/Makefile

cat ./src/methods/hopspack/src-appspack/Makefile | sed 's/CXXFLAGS = -g -O2/CXXFLAGS = -g -O2  -fPIC/g' > temp
mv temp ./src/methods/hopspack/src-appspack/Makefile

cat ./src/methods/acro/packages/colin/src/Makefile | sed 's/CXXFLAGS = -O2 -fpermissive/CXXFLAGS = -O2 -fpermissive -fPIC/g' > temp
mv temp ./src/methods/acro/packages/colin/src/Makefile

cat ./src/methods/acro/packages/coliny/src/Makefile | sed 's/CXXFLAGS = -O2 -fpermissive/CXXFLAGS = -O2 -fpermissive -fPIC/g' > temp
mv temp ./src/methods/acro/packages/coliny/src/Makefile

cat ./src/methods/acro/packages/tpl/3po/Makefile | sed 's/CFLAGS = -O2/CFLAGS = -O2 -fPIC/g' > temp
mv temp  ./src/methods/acro/packages/tpl/3po/Makefile

cat ./src/methods/acro/packages/tpl/3po/Makefile | sed 's/CXXFLAGS = -O2 -fpermissive/CFLAGS = -O2 -fpermissive -fPIC/g' > temp
mv temp  ./src/methods/acro/packages/tpl/3po/Makefile

cat ./src/packages/ampl/Makefile | sed 's/CFLAGS = -g -O2/CFLAGS = -g -O2 -fPIC/g' > temp
mv temp  ./src/packages/ampl/Makefile

#Compile and install dakota
cd src
if [ -z $NUMCPUS ];
then
	make
	make install
else
	make -j $NUMCPUS
	make -j $NUMCPUS install
fi
cd ..

#Weird behaviour of Dakota: libamplsolver.a and amplsolver.a are not the same thing!
cd install/lib
mv libamplsolver.a libamplsolver.a.bak
ln -s ../../src/packages/ampl/amplsolver.a ./libamplsolver.a
