#!/bin/ksh

# ICON
#
# ------------------------------------------
# Copyright (C) 2004-2026, DWD, MPI-M, DKRZ, KIT, ETH, MeteoSwiss
# Contact information: icon-model.org
# See AUTHORS.TXT for a list of authors
# See LICENSES/ for license information
# SPDX-License-Identifier: BSD-3-Clause
# ------------------------------------------

# usage: get_ocean <from> <to>
# links the ocean only needed files from the <icon-dev full path> to the <ocean only full path>

set -x
thisfolder=`pwd`
# icon_from="${thisfolder}/git/$1"
# icon_ocean_to=$thisfolder/$2
icon_from=$1
icon_ocean_to=$2


if [ x$icon_ocean_to == "x" ]; then
  echo "get_ocean <from> <to>"
  exit
fi

if [ ! -d ${icon_ocean_to} ] ; then
  mkdir -p ${icon_ocean_to}
fi

#-----------------------------------------------------------------------------------
link_source_list()
{
  source_folder=$1
  source_files_list="$2"

  if [ ${source_folder} != "" ] ; then
    if [ ! -d ${icon_ocean_to}/${source_folder} ] ; then
      mkdir -p ${icon_ocean_to}/${source_folder}
    fi
  fi

  if [ "${source_files_list}" == "all" ] ; then
    ln -s ${icon_from}/${source_folder}*  ${icon_ocean_to}/${source_folder}
  else
    for source_file in ${source_files_list}
    do
      ln -s ${icon_from}/${source_folder}${source_file}  ${icon_ocean_to}/${source_folder}
    done
  fi

}

#-----------------------------------------------------------------------------------
copy_source_list()
{
  source_folder=$1
  source_files_list="$2"

  if [ ${source_folder} != "" ] ; then
    if [ ! -d ${icon_ocean_to}/${source_folder} ] ; then
      mkdir -p ${icon_ocean_to}/${source_folder}
    fi
  fi

  if [ "${source_files_list}" == "all" ] ; then
    cp -r ${icon_from}/${source_folder}*  ${icon_ocean_to}/${source_folder}
  else
    for source_file in ${source_files_list}
    do
      cp -r ${icon_from}/${source_folder}${source_file}  ${icon_ocean_to}/${source_folder}
    done
  fi

}



#-----------------------------------------------------------------------------------
#get all
link_list=`ls $icon_from`
link_source_list "" "$link_list"

#-----------------------------------------------------------------------------------
# get config, run, externals
rm $icon_ocean_to/config $icon_ocean_to/run $icon_ocean_to/externals
link_source_list "config/" all
link_source_list "run/" all
link_source_list "externals/" "ecrad tixi mtime self"
copy_source_list "externals/" "yaxt"


#-----------------------------------------------------------------------------------
# get src
rm $icon_ocean_to/src

link_source_list "src/" "ocean hamocc sea_ice parallel_infrastructure include shared"
link_source_list "src/io/" "shared restart"

# needed by sea-ice
link_source_list "src/advection/" "mo_advection_utils.f90"

# needed by src/io/shared/mo_util_cdi
link_source_list "src/lnd_phy_nwp/" "mo_nwp_sfc_tiles.f90"

link_source_list "src/configure_model/" "mo_dynamics_config.f90 mo_gribout_config.f90 mo_grid_config.f90 mo_io_config.f90 mo_name_list_output_config.f90
mo_parallel_config.f90 mo_run_config.f90 mo_time_config.f90 mo_coupling_config.f90 mo_master_config.f90 mo_interpol_config.f90 mo_upatmo_config.f90
mo_advection_config.f90"
# mo_master_config.f90"

link_source_list "src/drivers/" "icon.f90 mo_master_control.f90"

link_source_list "src/namelists/" "mo_dbg_nml.f90  mo_dynamics_nml.f90 mo_gribout_nml.f90 mo_grid_nml.f90 mo_io_nml.f90
mo_master_nml.f90 mo_parallel_nml.f90 mo_run_nml.f90 mo_sea_ice_nml.f90 mo_time_nml.f90"


link_source_list "src/shr_horizontal/" "mo_alloc_patches.f90 mo_ext_data_types.f90 mo_grid_geometry_info.f90 mo_grid_tools.f90
mo_intp_data_strc.f90 mo_lonlat_grid.f90 mo_model_domain.f90 mo_model_domimp_patches.f90
mo_model_domimp_setup.f90 mo_reorder_patches.f90 mo_icon_interpolation_scalar.f90 mo_intp_lonlat_types.f90"

link_source_list "src/testcases/" "mo_ape_params.f90"

# needed by src/configure_model/mo_upatmo_config
# (which in turn is required by src/shr_horizontal/mo_model_domimp_patches),
# and by src/io/restart/
link_source_list "src/upper_atmosphere/" "mo_upatmo_impl_const.f90 mo_upatmo_phy_config.f90 mo_upatmo_types.f90 mo_upatmo_utils.f90
mo_upatmo_flowevent_utils.f90"
link_source_list "src/atm_phy_nwp/" "mo_phy_events.f90"

#-----------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------
# # add some parameteres files
# rm -f ${icon_ocean_to}/config/local_flags
# cat > ${icon_ocean_to}/config/local_flags << EOF
# #===============================================
# # define the components to be compiled with ICON, and other flags
# icon_flags=NO_ICON_ATMO NO_JSBACH NO_ICON_TESTBED
# EOF


#-----------------------------------------------------------------------------------
cat > ${icon_ocean_to}/config/multiple_builds << EOF
#==============================================
# This file defines multiple builds for ICON
#==============================================

#==============================================
# Define the run scripts in run/ to be common along the following builds
run_scripts=*

#==============================================
# Define parameters for each build (testbed is disabled by default) - examples:
build_folder=gcc_yMyO configure_options=--with-fortran=gcc --with-mpi  --with-openmp --disable-atmo --disable-jsbach --disable-psrad --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=gcc_yMyO_hiopt configure_options=--with-fortran=gcc --with-mpi  --with-openmp --with-flags=ocean --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=gcc_nMnO configure_options=--with-fortran=gcc --without-mpi --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=gcc_nMyO configure_options=--with-fortran=gcc --without-mpi --with-openmp --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=gcc_yMnO configure_options=--with-fortran=gcc --with-mpi --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=gcc_yMnO_hiopt configure_options=--with-fortran=gcc --with-mpi --with-flags=ocean --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=nag_nMnO configure_options=--with-fortran=nag --without-mpi --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=nag_yMnO configure_options=--with-fortran=nag --with-mpi --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=intel_nMyO configure_options=--with-fortran=intel --without-mpi --with-openmp --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=intel_nMnO configure_options=--with-fortran=intel --without-mpi --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=intel_yMnO configure_options=--with-fortran=intel --with-mpi --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=intel_yMyO configure_options=--with-fortran=intel --with-mpi --with-openmp --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=intel_yMyO_hiopt configure_options=--with-fortran=intel --with-mpi --with-openmp --with-flags=ocean --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=intel_yMnO_hiopt configure_options=--with-fortran=intel --with-mpi --with-flags=ocean --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
build_folder=pgi_yMnO configure_options=--with-fortran=pgi  --with-mpi --disable-atmo --disable-jsbach --disable-psrad  --with-yac=no  --with-yaxt=yes --without-regard-for-quality
EOF

ln -s ${icon_ocean_to}/scripts/building/create_builds ${icon_ocean_to}

exit
