#!/bin/csh
#set verbose echo
set is_parallel_opt       = FALSE
set is_driver_opt         = FALSE
set is_arch_opt           = FALSE 
set is_compile_opt        = FALSE
set default_compile_flags = "%PROD_FFLAGS"
set fcm_path=none

setenv P_P ""
setenv SECTIONS_PARA ""

top:
if ($#argv > 0) then
    switch ($1)

    case -h

########################################################################
# Manuel en ligne
########################################################################
more <<eod


makeorchidee_fcm [Options] 

[ -parallel|-p PARALLEL_TYPE ] : activate parallelization with 3 options : 
    PARALLEL_TYPE =
    ( mpi | MPI )  : use Message Passing Interface standard
    ( omp | OMP )  : use OpenMP standard
    ( mpi_omp | MPI_OMP ) : use both MPI and OpenMP

[ -driver ] : compilation of ORCHIDEE driver if equal TRUE

[ -a|-arch ARCHitecture ] : if TRUE, change architecture for ARCHitecture

[ -prod ] : compilation for production (all optimization)

[ -dev ] : compilation for development (low optimization and -g)

[ -d|-debug ] : compilation for debugging (no optmization and all debug options)

[-fcm_path path]           : path for fcm (default: the default fcm path in environment)

[ -doc ] : generate documentation with Doxygen (exit after doc computation)

[ -doc_para ] : generate documentation with Doxygen with parallelization calls

[ -rmdoc ] : remove documentation directory (before generate new documentation with Doxygen ?)

[ -doc_tree ] : generate tree of ORCHIDEE directories for each file

eod
exit

########################################################################
# Lecture des differentes options
########################################################################

    case -parallel
        set is_parallel_opt = TRUE
        set parallel=$2 ; shift ; shift ; goto top

    case -p
        set is_parallel_opt = TRUE
        set parallel=$2 ; shift ; shift ; goto top
                        
    case -driver
        set is_driver_opt = TRUE
        shift ; goto top

    case -a
    case -arch
        set is_arch_opt = TRUE
        set arch="$2" ; shift ; shift ; goto top

    case -prod
        set is_compile_opt = TRUE
        set compile_flags="%PROD_FFLAGS"
        shift ; goto top

    case -dev
        set is_compile_opt = TRUE
        set compile_flags="%DEV_FFLAGS"
        shift ; goto top

    case -d
    case -debug
        set is_compile_opt = TRUE
        set compile_flags="%DEBUG_FFLAGS"
        shift ; goto top

    case -fcm_path
       set fcm_path=$2 ; shift ; shift ; goto top

    case -doc_para
        setenv P_P -DCPP_PARA
	setenv SECTIONS_PARA "-e s&ENABLED_SECTIONS\ *= &ENABLED_SECTIONS       = CPP_PARA&g"
	goto doc

    case -doc
doc:    
        unalias cd
	setenv REV `svn info | grep vision | sed -e 's&.*vision.*: \([0-9]*\)&\1&' | head -n 1`
	setenv TAG `svn info | grep URL | sed -e 's&.*URL.*: svn://forge.ipsl.jussieu.fr/orchidee/\(.*\)&\1&' | head -n 1 | xargs dirname`
       	cd ..
	\rm -rf modeles_doc
	setenv MODELPATH ${PWD}
	\mkdir modeles_doc
	setenv SRCPATH ${MODELPATH}/modeles_doc

	find IOIPSL/src \( -not -path '*.svn*' \
			-a \( -name '*.f90' -o -name "*.F90" -o -name "*.h" \) \) \
		    -exec bash -c 'mkdir -p '${SRCPATH}'/$( dirname {} ); cp -p {} '${SRCPATH}'/$( dirname {} )' \;

	find ORCHIDEE \( -not -path '*.svn*' \
			-a \( -name '*.f90' -o -name "*.F90" -o -name "*.h" \) \) \
		    -exec bash -c 'mkdir -p '${SRCPATH}'/$( dirname {} ); cp -p {} '${SRCPATH}'/$( dirname {} )' \;
	cd ${SRCPATH}
	# Use standard preprocessor to suppress all preproc directives
	\find . -name "*.f90" -exec cpp -P -C -traditional -x assembler-with-cpp ${P_P} '{}' '{}'_ \;
	\find . -name "*.f90" -print -exec mv -f '{}'_ '{}' \;

	# use codeinc script to encaspulate code parts in Doxygen documentation
	\find . -name "*.f90" -exec gawk -f ${MODELPATH}/ORCHIDEE/DOC/TOOLS/codeinc.awk '{}' \; > /dev/null
	\find . -name "*.f90" -print -exec mv -f '{}'_preproc_codeinc '{}' \;
	# use codedox script to transform almost all comments in Doxygen comments for documentation (use with care !)
	\find . -name "*.f90" -exec gawk -f ${MODELPATH}/ORCHIDEE/DOC/TOOLS/codedox.awk '{}' \; > /dev/null
	\find . -name "*.f90" -print -exec mv -f '{}'_preproc_codedox '{}' \;

	# use codealgo script to emphasize main algorithm description of each routine in their Doxygen documentation
	\find . -name "*.f90" -exec gawk -f ${MODELPATH}/ORCHIDEE/DOC/TOOLS/codealgo.awk '{}' \; > /dev/null
	\find . -name "*.f90" -print -exec mv -f '{}'_preproc_codealgo '{}' \;
        cd ../ORCHIDEE
	\rm -f ${MODELPATH}/ORCHIDEE/Doxyfile_ORCHIDEE
	sed -e 's&MYPATH&'${MODELPATH}'&g' -e 's&SRCPATH&'${SRCPATH}'&g' \
            -e 's&MYTAG&'${TAG}'&' -e 's&MYREV&'${REV}'&' ${SECTIONS_PARA} \
            ${MODELPATH}/ORCHIDEE/Doxyfile_ORCHIDEE.init > ${MODELPATH}/ORCHIDEE/Doxyfile_ORCHIDEE
	\rm -f ${MODELPATH}/ORCHIDEE/DOC/header.tex
	sed -e "s&MYTAG&${TAG}&" -e "s&MYREV&${REV}&" \
            ${MODELPATH}/ORCHIDEE/DOC/header.tex.init > ${MODELPATH}/ORCHIDEE/DOC/header.tex
	ln -s /home/orchidee01/maignan/ORCHIDEE/DOC/IMAGES ${MODELPATH}/ORCHIDEE/DOC/IMAGES
	gmake doc
	gmake bib
	gmake toc
	\rm -rf ${MODELPATH}/ORCHIDEE/webdoc
	\mv ${MODELPATH}/ORCHIDEE/docs/html ${MODELPATH}/ORCHIDEE/webdoc
	gmake index
	gmake toc
	\rm -rf ${SRCPATH}
	cp ${MODELPATH}/ORCHIDEE/docs/latex/refman.pdf ${MODELPATH}/ORCHIDEE/documentation.pdf
	exit

    case -rmdoc
	\rm -rf docs
        shift ; goto top

    case -doc_tree
        cd ..
	find ORCHIDEE \( -not -path '*.svn*' \
			-a \( -name '*.f90' -o -name "*.F90" -o -name "*.h" \) \) \
		    -exec bash -c 'mkdir -p ORCHIDEE/DOC/$( dirname {} )/$( echo $( basename {} ) | sed -e "s/\..*//" )' \;
	cd ORCHIDEE
        shift ; goto top

    default
        echo "unknown option "$1" , exiting..."
        exit 
   endsw
endif

###############################################################
# mettre le chemin du fcm dans le path
###############################################################
if  ( "$fcm_path" != 'none' ) then
    setenv PATH ${fcm_path}:${PATH}
endif

echo "Chemin du fcm utlise :" 
which fcm


#define architecture files
if ( $is_arch_opt == TRUE) then
  if ( -e arch/arch-${arch}.fcm ) then
    rm -f arch.fcm
    ln -s arch/arch-${arch}.fcm arch.fcm
  else
    echo "architecture file : << arch/arch-${arch}.fcm >> is missing, exiting...."
    exit
  endif
  
  if ( -e arch/arch-${arch}.path ) then
    rm -f arch.path
    ln -s arch/arch-${arch}.path arch.path
  else
    echo "architecture file : << arch/arch-${arch}.path >> is missing, exiting...."
    exit
  endif
else
  echo "Warning : architecture not specified, taking default file <<arch.fcm>> and <<arch.path>>" 
  if ( ! -e arch.fcm ) then
    echo "architecture file : << arch.fcm >> is missing, exiting...."
    exit
  endif

  if ( ! -e arch.fcm ) then
    echo "architecture file : << arch.path >> is missing, exiting...."
    exit
  endif
endif
  
# set compiler flags
set FFLAGS="%BASE_FFLAGS"
set LD_FFLAGS="%BASE_LD"
set CPP_KEY="%FPP_DEF"

# set compiler flags for optimisation
if ( $is_compile_opt == FALSE ) then
  set compile_flags=$default_compile_flags
endif
set FFLAGS=${FFLAGS}" "$compile_flags

# set compiler flags for parallelism
if ( $is_parallel_opt == TRUE ) then
  echo $parallel 
  switch ($parallel)
    case mpi
    case MPI
      set FFLAGS="${FFLAGS} %MPI_FFLAGS"
      set LD_FFLAGS="%MPI_LD ${LD_FFLAGS}"
      set CPP_KEY="CPP_PARA ${CPP_KEY}"
      breaksw
    case omp
    case OMP
      set FFLAGS="${FFLAGS} %OMP_FFLAGS"
      set LD_FFLAGS="%OMP_LD ${LD_FFLAGS}"
      set CPP_KEY="CPP_OMP CPP_PARA ${CPP_KEY}"
      breaksw
    case mpi_omp
    case MPI_OMP
      set FFLAGS="${FFLAGS} %MPI_FFLAGS %OMP_FFLAGS"
      set LD_FFLAGS="%MPI_LD %OMP_LD ${LD_FFLAGS}"
      set CPP_KEY="CPP_OMP CPP_PARA ${CPP_KEY}"
      breaksw
    
    default
        echo "unknown option for parallelism :$parallel , only <<mpi>>, <<MPI>> or <<omp>>, <<OMP>> or <<mpi_omp>> <<MPI_OMP>> allowed , exiting..."
        exit 
   endsw
endif
       
# set target
set TARGET = liborchidee.a
if ( $is_driver_opt == TRUE) then
  set TARGET="liborchidee_ol.a dim2_driver.exe teststomate.exe forcesoil.exe"
endif

source ./arch.path

# build config file
set config_fcm="config.fcm"
rm -f $config_fcm
touch $config_fcm

echo "%ARCH          $arch"                             >> $config_fcm 
echo "%FFLAGS        $FFLAGS"                           >> $config_fcm 
echo "%CPP_KEY       $CPP_KEY"                          >> $config_fcm
echo "%EXEC          $TARGET"                           >> $config_fcm
echo "%LD_FFLAGS     $LD_FFLAGS"                        >> $config_fcm
echo "%INCDIR        -I$NETCDF_INCDIR -I$IOIPSL_INCDIR" >> $config_fcm
echo "%LIBDIR        -L$NETCDF_LIBDIR -L$IOIPSL_LIBDIR" >> $config_fcm


fcm build
