#!/bin/csh
set verbose echo
set has_arch_opt           = FALSE 
set has_compile_opt        = FALSE
set default_compile_flags  = "%PROD_FFLAGS"
set has_use_vt             = FALSE
set has_oasis              = FALSE
set has_no_mpi             = FALSE

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

    case -h

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


configure_ioserver [Options] 

bla, bla, bla
eod
exit

########################################################################
# Lecture des differentes options
########################################################################
                   
    case -a
    case -arch
        set has_arch_opt = TRUE
        set arch="$2" ; shift ; shift ; goto top

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

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

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

    case -use_vt
        set has_use_vt = TRUE
        shift ; goto top
        
    case -oasis
        set has_oasis = TRUE
        shift ; goto top

    case -no_mpi
        set has_no_mpi = TRUE
        shift ; goto top
   
   default
        echo "unknown option "$1" , exiting..."
        exit 
   endsw
endif


#define architecture files
if ( $has_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 %MPI_LD"
set CPP_KEY="%FPP_DEF"
set INCDIR=""
set LIB=""

source ./arch.path

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


# add path for xmlf90 library

set INCDIR="$INCDIR $XMLF90_INCDIR"
set LIB="$LIB $XMLF90_LIBDIR $XMLF90_LIB"

# add path for ioipsl library
set INCDIR="$INCDIR $IOIPSL_INCDIR"
set LIB="$LIB $IOIPSL_LIBDIR $IOIPSL_LIB"

if ( $has_oasis == TRUE ) then
  set INCDIR="$INCDIR $OASIS_INCDIR"
  set LIB="$LIB $OASIS_LIBDIR $OASIS_LIB"
  set CPP_KEY="$CPP_KEY USE_OASIS"
endif

# add path for netcdf library
set INCDIR="$INCDIR $NETCDF_INCDIR"
set LIB="$LIB $NETCDF_LIBDIR $NETCDF_LIB"

if ( $has_use_vt == TRUE ) then
  set INCDIR="$INCDIR $VAMPIR_INCDIR"
  set LIB="$LIB $VAMPIR_LIBDIR $VAMPIR_LIB"
  set CPP_KEY="$CPP_KEY USE_VT"
endif

if ( $has_no_mpi == TRUE ) then
  set CPP_KEY="NO_MPI $CPP_KEY"
endif


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

echo "%FFLAGS        $FFLAGS"                           >  $config_fcm 
echo "%CPP_KEY       $CPP_KEY"                          >> $config_fcm
echo "%LD_FFLAGS     $LD_FFLAGS"                        >> $config_fcm
echo "%INCDIR        $INCDIR"                           >> $config_fcm
echo "%LIB           $LIB"                              >> $config_fcm
