#!/bin/bash
# This script needs to be run before the  cmake  command for CMake
# builds. It should be called from all cmake scripts. 
#
# This script auto-generates files that were autogerenated as part 
# of the autoconf system.

echo
echo "Auto-generating glint_io.F90, glint_mbal_io.F90,"
echo "glad_io.F90, glad_mbal_io.F90 in build directory."
echo

mkdir -p fortran_autogen_srcs
pushd fortran_autogen_srcs

if [ $# -eq 1 ];
  then
    export CISM_SOURCE_DIR=$1 
  else
    export CISM_SOURCE_DIR=../../../
echo $1
fi
echo "Auto-generate script is using CISM source directory: "
echo $CISM_SOURCE_DIR

##########################
# The following lines generate *_io.F90 files, based on variable definition files:

echo "  --creating glint_io.F90, glint_mbal_io.F90, glad_io.F90 and glad_mbal_io.F90 "

# Save path to source files used in autogeneration
export NCDF_TEMPL_PATH=$CISM_SOURCE_DIR/libglimmer/ncdf_template.F90.in
export GLINT_VARS_PATH=$CISM_SOURCE_DIR/libglint/glint_vars.def
export GLINT_MBAL_PATH=$CISM_SOURCE_DIR/libglint/glint_mbal_vars.def
export GLAD_VARS_PATH=$CISM_SOURCE_DIR/libglad/glad_vars.def
export GLAD_MBAL_PATH=$CISM_SOURCE_DIR/libglad/glad_mbal_vars.def

# Call python script with source file arguments
python3 -V
python3 $CISM_SOURCE_DIR/utils/build/generate_ncvars.py $GLINT_VARS_PATH $NCDF_TEMPL_PATH
python3 $CISM_SOURCE_DIR/utils/build/generate_ncvars.py $GLINT_MBAL_PATH $NCDF_TEMPL_PATH
python3 $CISM_SOURCE_DIR/utils/build/generate_ncvars.py $GLAD_VARS_PATH $NCDF_TEMPL_PATH
python3 $CISM_SOURCE_DIR/utils/build/generate_ncvars.py $GLAD_MBAL_PATH $NCDF_TEMPL_PATH

popd


