#!/bin/bash

# This script copies the libcsp library into this repository.
#
# It's written as a script (instead of the actions just being done manually) primarily for
# reproducibility and documentation purposes.
#
# It may be run in a GitHub Action to ensure that the files are copied in correctly and reliably.
# In a proper state, re-running this script should not make the repo dirty.
# 
# This janky script exists to emulate what the `waf` build system does. That is, it generates a
# `csp_autoconfig.h` file (with a bunch of #defines), and then selects the correct source files to
# include in the build.

# Halt on error.
set -e

# Print Usage: ./copy_in_libcsp.sh <path_to_libcsp_clone>
if [ "$#" -ne 1 ]; then
    echo "Usage: ./copy_in_libcsp.sh <path_to_libcsp_clone>"
    exit 1
fi

# Set the path to the libcsp clone.
SRC_LIBCSP_CLONE_PATH=$1

# Set the DEST_GIT_REPO_ROOT variable to the path to this repository.
DEST_GIT_REPO_ROOT=$(git rev-parse --show-toplevel)

# Assert that $SRC_LIBCSP_CLONE_PATH is a valid directory.
if [ ! -d "$SRC_LIBCSP_CLONE_PATH" ]; then
    echo "Usage: ./copy_in_libcsp.sh <path_to_libcsp_clone>"
    echo "Error: $SRC_LIBCSP_CLONE_PATH is not a directory."
    exit 2
fi

echo "Copying libcsp library from '$SRC_LIBCSP_CLONE_PATH' into this repository ('$DEST_GIT_REPO_ROOT')..."

# Remove existing libcsp files in this repository.
# Don't worry if the files don't exist (-f flag).
rm -rf $DEST_GIT_REPO_ROOT/firmware/Core/Inc/csp
rm -rf $DEST_GIT_REPO_ROOT/firmware/Core/Src/csp

# Run the build script's configure command. This will generate the `csp_autoconfig.h` file which gets copied later.
# First, store the current working directory so we can `cd` back after running waf.
pushd $SRC_LIBCSP_CLONE_PATH
git fetch -a
git checkout libcsp-cts-sat-1 # We are using the `libcsp-cts-sat-1` branch of https://github.com/CalgaryToSpace/libcsp repo.
echo "============= Running waf configure in $SRC_LIBCSP_CLONE_PATH ============="
$SRC_LIBCSP_CLONE_PATH/waf configure --with-os=freertos --with-loglevel=debug --enable-crc32 --enable-hmac --with-rtable=static
echo "============= Finished running waf configure ============="
# Return to the original working directory.
popd

# Copy the libcsp library's headers into this repository.
cp -r $SRC_LIBCSP_CLONE_PATH/include/csp $DEST_GIT_REPO_ROOT/firmware/Core/Inc/

# Copy the libcsp library's source files into this repository. Note the folder rename.
cp -r $SRC_LIBCSP_CLONE_PATH/src $DEST_GIT_REPO_ROOT/firmware/Core/Src/csp/

# Copy the `csp_autoconfig.h` file generated by the waf configure command.
cp $SRC_LIBCSP_CLONE_PATH/build/include/csp/csp_autoconfig.h $DEST_GIT_REPO_ROOT/firmware/Core/Inc/csp/csp_autoconfig.h

# Delete unused architectures, keeping only the FreeRTOS one.
rm -r $DEST_GIT_REPO_ROOT/firmware/Core/Src/csp/arch/macosx/
rm -r $DEST_GIT_REPO_ROOT/firmware/Core/Src/csp/arch/posix/
rm -r $DEST_GIT_REPO_ROOT/firmware/Core/Src/csp/arch/windows/
rm $DEST_GIT_REPO_ROOT/firmware/Core/Src/csp/arch/*.c

# Delete drivers for unused architectures.
rm $DEST_GIT_REPO_ROOT/firmware/Core/Src/csp/drivers/usart/usart_linux.c
rm $DEST_GIT_REPO_ROOT/firmware/Core/Src/csp/drivers/usart/usart_windows.c

# Delete unused routing table option (either csp_rtable_cidr.c or csp_rtable_static.c).
rm $DEST_GIT_REPO_ROOT/firmware/Core/Src/csp/rtable/csp_rtable_cidr.c

# Delete the unused Python bindings.
rm -r $DEST_GIT_REPO_ROOT/firmware/Core/Src/csp/bindings/

# Delete the bad CAN driver. It has includes that are not supported as-is.
rm -r $DEST_GIT_REPO_ROOT/firmware/Core/Src/csp/drivers/can/

echo "============= libcsp library copied into this repository. All done. ============="
