#!/bin/bash
# --
## SVN information
#  $Author:  $
#  $Date:  $
#  $Revision:  $
#  $Id:  $
#  $HeadURL: $
#

# Default values for command line parameters
install_dir=${PWD}
xios_dir="$(realpath ../XIOS)"
compil_full=false
	    
# Traitement de la ligne de commande
while ((${#} > 0)) ; do
    case ${1} in
        "-h"|"--help"|"-help")
            echo "make_mosaix - installs MOSAIX on your architecture"
	    echo "make_xios [options]"
	    echo "options :"
	    echo "       [--xios path] : path to XIOS. Default is ${xios_dir}"
	    echo "       [--full]      : to generate dependencies and recompile from scratch"
	    echo "Example : ./make_mosaix --xios ../XIOS"
            exit
	    ;;
        "--xios")  xios_dir=$(realpath ${2})   ; xios_path_defined="TRUE"; shift ; shift ;;
	"--full")  compil_full="true"  ; shift ;;
    esac
done


# Definition of the root directory of the build
install_dir=${PWD}

rm -f ${PWD}/bld_dir.cfg

echo "inc        ${install_dir}/arch.fcm"   >> ${PWD}/bld_dir.cfg 
echo "inc        ${install_dir}/config.fcm" >> ${PWD}/bld_dir.cfg
echo "dir::root  ${install_dir}"            >> ${PWD}/bld_dir.cfg

# Use the arch files used to compile XIOS
ln -fs ${xios_dir}/arch.fcm   .
ln -fs ${xios_dir}/arch.env   .
ln -fs ${xios_dir}/arch.path  .

rm -f .void_file
echo > .void_file
rm -rf .void_dir
mkdir .void_dir


XIOS_CINCDIR="$NETCDF_INCDIR $HDF5_INCDIR $MPI_INCDIR $BOOST_INCDIR $BLITZ_INCDIR"
XIOS_FINCDIR="$NETCDF_INCDIR $XIOS_FINCDIR $MPI_INCDIR $BOOST_INCDIR $BLITZ_INCDIR"
XIOS_LIB="$XIOS_LIB $NETCDF_LIBDIR $HDF5_LIBDIR $MPI_LIBDIR $NETCDF_LIB $HDF5_LIB $MPI_LIB"

# Use config.fcm from XIOS
rm -f ${install_dir}/config.fcm

echo "%XIOS_DIR      ${xios_dir} "    > ${install_dir}/config.fcm
cat ${xios_dir}/config.fcm >> ${install_dir}/config.fcm

make_dir=${PWD}

export PATH=${xios_dir}/tools/FCM/bin:${PATH}

source ${install_dir}/arch.env
source ${install_dir}/arch.path

if [[ "${compil_full}" == "true" ]]
then
    fcm build --clean --ignore-lock 
    fcm build -f --ignore-lock
else
    fcm build --ignore-lock
fi

build_exit_status=${?}

if [[ ${build_exit_status} == 0 ]]
then
    set nothing
fi

echo PATH : ${PATH}
echo fcm : $(which fcm)

exit ${build_exit_status}



