#!/bin/bash

function build_dynamico()
{
    NAME=$1 ; shift
    BUILD=$ROOT/build_$NAME
    LOGFILE=$ROOT/logs/dynamico_${NAME}.log
    cd $ROOT/build_$NAME
    echo "In $PWD : building DYNAMICO with options $*"
    echo "Watch logfile $LOGFILE"
#    ./make_icosa -full -arch $arch -job $job $* > $LOGFILE 2>&1 || ( tail $LOGFILE ; exit 1 )
    ./make_icosa -arch $arch -job $job $* > $LOGFILE 2>&1 || ( tail $LOGFILE ; exit 1 )
    cp -pr $BUILD/bin/icosa_gcm.exe  $ROOT/bin/icosa_$NAME.exe
}

function main()
{  
    LOGFILE=$ROOT/logs/build_xios.log
    cd $ROOT/XIOS
    echo "In $PWD : Building XIOS"
    echo "Watch logfile $LOGFILE"
    ./make_xios --arch $arch --job $job > $LOGFILE 2>&1 || ( tail $LOGFILE ; exit 1 )    
    cd $ROOT
    # compile all variants in parallel
#    build_dynamico serial -parallel none $FULL &
    build_dynamico mpi -parallel mpi -with_xios $other $FULL &
    build_dynamico mpi_omp -parallel mpi_omp -with_xios $other $FULL &
    wait
    # check success
#    ls bin/icosa_serial.exe || exit 1
    ls bin/icosa_mpi.exe || exit 1
    ls bin/icosa_mpi_omp.exe || exit 1
}

FULL=$1
source current_args
rm -rf bin logs
mkdir bin logs
( main ) || ( echo ; echo "***************** Build FAILED ******************" )

echo
echo "You may now run create_runs.sh"
