#!/bin/bash -e

# --------------------------------------------------
# Script to compile WW3 in coupled and uncoupled modes
# --------------------------------------------------
#
# Further Information:   
# http://www.croco-ocean.org
#  
# This file is part of CROCOTOOLS
#
# CROCOTOOLS is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# CROCOTOOLS is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA
#
# Copyright (c) 2018 S. Jullien
# swen.jullien@ifremer.fr
# --------------------------------------------------

#=========================================================================
#=======================  USER CHANGES  ==================================
#=========================================================================

source ../run_env

# Environment variables requested for ww3 compilation:
#
# define your local ww3 environment file
export WWATCH3_ENV=$hconf/wwatch3.env
# define your compilers
export WWATCH3_F77=ifort
export WWATCH3_CC=gcc
# define your netcdf format
export WWATCH3_NETCDF=NC4
# define your ww3 source model
export WWATCH3_DIR=$ww3
# define your ww3 temporary directory for compilation
export WWATCH3_TMP=$ww3/tmp
# define your oasis libraries directory (where oasis has been compiled)
export OASISDIR=$oasisdir

# define your compiler extension 
# (see what is available in $ww3/bin as comp.EXTENSION)
# for ADA
mycompil=Altix
# for DATARMOR
#mycompil=DATARMOR

# define your switch extension to be used for compilation 
# (see what is available in $ww3/bin or create one)
# (some examples are given in $WW3_IN_DIR/inputs_ww3/)
my_switches=(frc_BENGUELA \
             ow_BENGUELA)
# flag to copy switches if you have defined them on your local ww3_in/inputs_ww3 directory
flag_copy_switches=1

# define the ww3 programs you want to compile 
# make_MPI will compile all programs...
my_pgm=make_MPI
#my_pgm='ww3_prnc ww3_grid ww3_bounc ww3_strt ww3_shel ww3_ounf'
#my_pgm='w3_shel'

#=========================================================================
#====================== END USER CHANGES  ================================
#=========================================================================

# Creation of wwatch3.env

cat << End_Of_wwatch3env > $WWATCH3_ENV
#
# Environment variables for wavewatch III
# ---------------------------------------
#

WWATCH3_LPR      printer
WWATCH3_F77      $WWATCH3_F77
WWATCH3_CC       $WWATCH3_CC
WWATCH3_DIR      $WWATCH3_DIR
WWATCH3_TMP      $WWATCH3_TMP
WWATCH3_SOURCE   yes
WWATCH3_LIST     yes

End_Of_wwatch3env

# Entering ww3 model directory 
cd $ww3/bin

NB_switches=${#my_switches[@]}

# Loop on switch cases
for k in `seq 0 $(( ${NB_switches} - 1))` ; do

    MYSWITCH=${my_switches[$k]}

    # Copy switch files if necessary
    if [ $flag_copy_switches == 1 ]; then
        cp -f $WW3_IN_DIR/inputs_ww3/switch_$MYSWITCH .
    fi
    # Check if switch file is available
    if [ ! -e switch_$MYSWITCH ]; then
        echo 'ERROR switch file: switch_'$MYSWITCH' does not exist'
        exit 1 
    fi
    ./w3_setup .. -c $mycompil -s $MYSWITCH -q
    ./w3_clean -c
    ./w3_setup .. -c $mycompil -s $MYSWITCH -q
    if [ "$my_pgm" == make_MPI ]; then
        ./make_MPI
    else
        ./w3_make $my_pgm
    fi
    mkdir -p ../exe_$MYSWITCH
    mv -f ../exe/* ../exe_$MYSWITCH/.

done

