#!/usr/bin/env bash

# This script creates a build directory and sticks a config.sh script into it.
# Then config.sh can be edited and run within the build directory.

# Print usage info.
if [ "$1" = "" ]; then
  echo "setup: Creates a build directory with a configuration file."
  echo "Optional argument 2 is path to haero install location."
  echo "Usage: setup build_dir [haero_dir]"
  exit 1
fi

# assign optional argument 2 to DIR_HAERO if it exists
if [ -z "$2" ]; then
  # if the optional arg isn't given, we print this placeholder
  export DIR_HAERO=/path/to/haero
else
  # expand arg to full path, in case it's relative
  export DIR_HAERO=$(readlink -f $2)
fi

# Create the build directory if it doesn't exist.
if [ ! -d $1 ]; then
  mkdir -p $1
fi

# Copy our template config script into place.
echo -e "#!/usr/bin/env bash\n" > $1/config.sh
echo "SOURCE_DIR=$PWD" >> $1/config.sh
cat <<EOT >> $1/config.sh
# ^^^^^^ location of mam4xx source code.

# config.sh -- A CMake configuration script.
# Edit this file to change the parameters in your build. Uncomment exactly one
# value for each parameter.

#-----------------------------------------------------------------------------
#                             Installation prefix
#-----------------------------------------------------------------------------
PREFIX=$PWD/$1

#-----------------------------------------------------------------------------
#                                   HAERO
#-----------------------------------------------------------------------------

# To build MAM4xx, you must have HAERO installed. Set this variable to a path
# with include/, lib/, and share/ directories in which HAERO has been installed.
# For instructions on building HAERO, surf to
# https://github.com/eagles-project/haero
# and check out the README.md file there.

HAERO_DIR=$DIR_HAERO

#-----------------------------------------------------------------------------
#                         Build features and parameters
#-----------------------------------------------------------------------------

# Set this to
# * 'Debug' for development (debugging symbols, no optimization)
# * 'Release' for production (no symbols, optimization).
BUILD_TYPE=Debug

# Uncomment this if you want really verbose builds.
#VERBOSE=ON

# Uncomment this to enable code coverage instrumentation.
#COVERAGE=ON

#-----------------------------------------------------------------------------
#                   Don't change anything below here.
#-----------------------------------------------------------------------------

# We use good old-fashioned UNIX makefiles.
GENERATOR="Unix Makefiles"

if [ "\$VERBOSE" = "ON" ]; then
  OPTIONS="\$OPTIONS -DCMAKE_VERBOSE_MAKEFILE=ON"
fi

if [ "\$COVERAGE" = "ON" ]; then
  OPTIONS="\$OPTIONS -DENABLE_COVERAGE=ON"
fi

# Clear the build cache.
rm -f CMakeCache.txt

# Configure the build.
cmake \
 -DCMAKE_INSTALL_PREFIX:PATH=\$PREFIX \
 -DCMAKE_BUILD_TYPE=\$BUILD_TYPE \
 -DMAM4XX_HAERO_DIR=\$HAERO_DIR \
 -DENABLE_SKYWALKER=ON \
 \$OPTIONS \
 -G "\$GENERATOR" \
 \$SOURCE_DIR
EOT

# Make config.sh executable.
chmod a+x $1/config.sh

# Give instructions.
echo "Your build directory '$1' is ready."
# assign optional argument 2 to DIR_HAERO if it exists
if [ ! -z "$2" ]; then
  # expand arg to full path, in case it's relative
  echo "You have given '$DIR_HAERO' as the HAERO library install location."
fi
echo "To configure your build:"
echo "  1. cd $1"
echo "  2. Edit config.sh"
echo "  3. ./config.sh"
echo "  4. Build using 'make -j'."

