#!/bin/bash
if [[ $HOSTNAME = *"occigen"* ]]; then
	source machines/occigen-bdw/env
elif [[ $HOSTNAME = *"jean-zay"* ]]; then
	echo "Compilation start on jean-zay"
	source machines/jean-zay-cpu/env
elif [[ $HOSTNAME = *"irene"* ]]; then
	source machines/irene-amd/env
else
        echo "Hostname not recognized: abort"
        exit 1
fi

echo "************************************************************"
echo "* Compile "
echo "************************************************************"
compile(){
    cd $ramses_dir/ramses/bin
    make clean
    rm ramses3d
    # FFLAGS
    if [[ $HOSTNAME = *"irene"* ]]; then
         sed -i s/"FFLAGS = -cpp -DNDIM=\$(NDIM) -DNPRE=\$(NPRE) -DSOLVER\$(SOLVER) -DNVAR=\$(NVAR) -DNCHEM=\$(NCHEM)"/"FFLAGS = -mavx2 -O3 -ipo -no-prec-div -cpp -DNDIM=\$(NDIM) -DNPRE=\$(NPRE) -DSOLVER\$(SOLVER) -DNVAR=\$(NVAR) -DNCHEM=\$(NCHEM)"/g  $ramses_dir/ramses/bin/Makefile
    else
    sed -i s/"FFLAGS = -cpp -DNDIM=\$(NDIM) -DNPRE=\$(NPRE) -DSOLVER\$(SOLVER) -DNVAR=\$(NVAR) -DNCHEM=\$(NCHEM)"/"FFLAGS = -xHost -O3 -ip -cpp -DNDIM=\$(NDIM) -DNPRE=\$(NPRE) -DSOLVER\$(SOLVER) -DNVAR=\$(NVAR) -DNCHEM=\$(NCHEM)"/g  $ramses_dir/ramses/bin/Makefile
    fi
    if [[ $HOSTNAME = *"jean-zay"* ]]; then
        sed -i s/"F90 = mpif90"/"F90 = mpiifort"/g $ramses_dir/ramses/bin/Makefile    
    fi
    pwd
    make >& make.log
    if [ -z "ramses3d" ]; then
        echo "ramses3d binary not found/compiled"
        exit 1
    else 
        echo "Compilation of ramses3d succeeded"
    fi
}

compile
echo "************************************************************"
echo "* End Compile "
echo "************************************************************"
