#!/bin/bash

# resolve symlinks and '..' in $PWD to avoid useless re-compilation
cd -P $PWD

echo ./make_icosa $* > rebuild
chmod a+x rebuild

compil_mode_defined="FALSE"
compil_mode="prod"

job=1
full_defined="FALSE"
with_xios_defined="FALSE"
arch_defined="FALSE"
parallel_defined="FALSE"
arch_default_path="arch"
arch_path="arch"
parallel="none"
physics="none"
external_ioipsl="FALSE"
external_physics="FALSE"
no_io_defined="FALSE"

CPP_KEY="CPP_NONE" 
ICOSA_LIB=""

while (($# > 0))
  do
  case $1 in
      "-h") cat <<fin
Usage :
makegcm [options] -m arch exec
[-h]                       : help
[-prod / -dev / -debug]    : compilation en mode production (default) / developpement / debug .
 -arch nom_arch            : target architecture, use paths and compilation options from arch files arch/arch-NAME.*
 -arch_path PATH           : use arch files PATH/arch-ARCH.*
 -parallel mpi|mpi_omp     : pure MPI or hybrid MPI/OpenMP parallelism
 -job N                    : parallel compilation as with make -j N 
 -full                     : full compilation
 -with_xios                : use XIOS for I/O (default is NetCDF-CF on native grid)
 -no_io                    : disable I/O altogether
 -external_ioipsl PATH     : use IOIPSL from PATH rather than tools/ioipsl
 -physics lmdz_generic     : use LMDZ generic physics (planets)
 -external_physics         : allows external physics
 -mixed_prec               : compiles unstructured dynamics in single precision (float32)
fin
	  exit;;

      "-prod")
	  compil_mode="prod" ; shift ;;

      "-dev")
	  compil_mode="dev" ; shift ;;

      "-debug")
	  compil_mode="debug" ; shift ;;

      "-arch")
	  arch=$2 ; arch_defined="TRUE"; shift ; shift ;;
 
      "-arch_path")
	  arch_path=$2 ; arch_path_defined="TRUE"; shift ; shift ;;

      "-parallel")
	  parallel=$2 ; parallel_defined="TRUE"; shift ; shift ;;

      "-physics")
	  physics=$2 ; physics_defined="TRUE"; shift ; shift ;;

      "-job")
	  job=$2 ; shift ; shift;;

      "-full")
	  full_defined="TRUE" ; shift ;;

      "-with_xios")
	  with_xios_defined="TRUE" ; shift ;;
  
      "-no_io")
	  no_io_defined="TRUE" ; shift ;;
  
      "-external_ioipsl")
	  external_ioipsl="TRUE" ; shift ;;

      "-external_physics")
	  external_physics="TRUE" ; shift ;;

      "-mixed_prec")
	  mixed_prec="TRUE" ; shift ;;
      *)
	  code="$1" ; shift ;;
  esac
done

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

function link_if_exists()
{
  suffix=$1
  rm -f arch.$suffix
  if test -f $arch_path/arch-${arch}.${suffix}
  then
    ln -s $arch_path/arch-${arch}.${suffix} arch.${suffix}
  elif test -f $arch_default_path/arch-${arch}.${suffix}
  then
    ln -s $arch_default_path/arch-${arch}.${suffix} arch.${suffix}
  else
    ln -sf .void_file arch.$suffix
  fi    
}

if [[ "$arch_defined" == "TRUE" ]]
then
  for suffix in env path fcm opt ; do link_if_exists $suffix ; done

  source arch.env
  source arch.path

else
  echo "Veuillez definir une architecture cible"
  exit 1
fi

LD_FLAGS="%BASE_LD"
COMPIL_FFLAGS=""

if [[ "$parallel" == "mpi" ]]
then
  CPP_KEY="$CPP_KEY CPP_USING_MPI"
elif [[ "$parallel" == "omp" ]]
then
  CPP_KEY="$CPP_KEY CPP_USING_OMP"
  COMPIL_FFLAGS="$COMPIL_FFLAGS %OMP_FFLAGS"
  LD_FLAGS="$LD_FLAGS %OMP_LD"
elif [[ "$parallel" == "mpi_omp" ]]
then
  CPP_KEY="$CPP_KEY CPP_USING_MPI CPP_USING_OMP"
  COMPIL_FFLAGS="$COMPIL_FFLAGS %OMP_FFLAGS"
  LD_FLAGS="$LD_FLAGS %OMP_LD"
elif [[ "$parallel" == "none" ]]
then
  parallel="none"
else 
  echo "-parallel value $parallel is invalid, only permited <none>, <mpi>, <omp> or <mpi_omp>"
  exit 1
fi

if [[ "$physics" == "lmdz_generic" ]]
then
  CPP_KEY="$CPP_KEY CPP_PHYSICS_LMDZ_GENERIC"
  COMPIL_FFLAGS="$COMPIL_FFLAGS $PHYSICS_INCDIR"
  ICOSA_LIB="$ICOSA_LIB $PHYSICS_LIBDIR $PHYSICS_LIB"
fi

if [[ "$no_io_defined" == "TRUE" ]]
then
  CPP_KEY="$CPP_KEY CPP_NO_IO"
  with_xios_defined="FALSE"
fi  

if [[ "$with_xios_defined" == "TRUE" ]]
then
  CPP_KEY="$CPP_KEY CPP_USING_XIOS"
  COMPIL_FFLAGS="$COMPIL_FFLAGS $XIOS_INCDIR"
  ICOSA_LIB="$ICOSA_LIB $XIOS_LIBDIR $XIOS_LIB"
fi  

if [[ "$external_ioipsl" == "TRUE" ]]
then
  ioipsl_path=".void_dir"
  use_ioipsl="use::ioipsl"
  COMPIL_FFLAGS="$COMPIL_FFLAGS $IOIPSL_INCDIR"
  ICOSA_LIB="$ICOSA_LIB $IOIPSL_LIBDIR $IOIPSL_LIB"
else 
  ioipsl_path="tools/ioipsl"
  use_ioipsl=""
fi  

if [[ "$external_physics" == "TRUE" ]]
then
  CPP_KEY="$CPP_KEY CPP_USING_EXTERNAL_PHYSICS"
fi

if [[ "$mixed_prec" == "TRUE" ]]
then
  CPP_KEY="$CPP_KEY CPP_MIXED_PREC"
fi

if [[ "$no_io_defined" == "FALSE" ]]
then 
  ICOSA_LIB="$ICOSA_LIB $NETCDF_LIBDIR $NETCDF_LIB $HDF5_LIBDIR $HDF5_LIB"
  COMPIL_FFLAGS="$COMPIL_FFLAGS $NETCDF_INCDIR"
fi

if [[ "$compil_mode" == "prod" ]]
then
  HOPT_FFLAGS="%FAST_FFLAGS $COMPIL_FFLAGS"
  COMPIL_FFLAGS="%PROD_FFLAGS $COMPIL_FFLAGS"
elif [[ "$compil_mode" == "dev" ]]
then
  COMPIL_FFLAGS="%DEV_FFLAGS $COMPIL_FFLAGS"
  HOPT_FFLAGS=$COMPIL_FFLAGS
elif [[ "$compil_mode" == "debug" ]]
then
  COMPIL_FFLAGS="%DEBUG_FFLAGS $COMPIL_FFLAGS"
  HOPT_FFLAGS=$COMPIL_FFLAGS
fi

rm -f config.fcm

echo "%COMPIL_FFLAGS $COMPIL_FFLAGS" >> config.fcm
echo "%HOPT_FFLAGS %BASE_FFLAGS $HOPT_FFLAGS" >> config.fcm
echo "%LD_FLAGS $LD_FLAGS" >> config.fcm
echo "%CPP_KEY $CPP_KEY" >> config.fcm
echo "%LIB $ICOSA_LIB">> config.fcm
echo "%IOIPSL_PATH $ioipsl_path">> config.fcm
echo "%USE_IOIPSL $use_ioipsl">> config.fcm

# generate signature
function signature()
{
    cat <<EOF
! Signature file generated by make_icosa.
! Please do NOT track with svn.
MODULE signature_mod
IMPLICIT NONE
CONTAINS
SUBROUTINE print_signature
EOF
    (cd -P src ; svn info .. ; svn status -q) | (while read line ; do echo "   PRINT *, '$line' " ; done)
    cat <<EOF
END SUBROUTINE print_signature
END MODULE signature_mod
EOF
}

signature > signature.f90
diff -q signature.f90 src/base/signature.f90 || cp -f signature.f90 src/base/
rm -f signature.f90

if [[ "$full_defined" == "TRUE" ]]
then
  ./build --job $job --full
else
  ./build --job $job
fi

# force recompilation of cython modules
touch Python/*/*.pyx
