#!/bin/bash
#
# Core C SDK CMake Build script. This script can be used to build all available source libraries of the Core C included in the SDK.
#
# USAGE: ./cmake-build [option]
# OPTIONS: CMAKE Install prefix path, if default install path is not desired. The path can be relative to the location where cmake is executed below, or the full designated path.

# To correctly build all of the Core C libraries, add the desired versions of the respective libraries below.
# If you plan on using a separate install prefix, rather than the default, clean up the prefix directory as needed before executing.
# The versions of the libraries included in the SDK are populated by default for simplicity and convenience, but can be changed if a different version is desired.

PCR_DEV_VER=7.0.1
PCR_POSIX_VER=2.0.2
CIPHER_VER=2.0.1
OAL_VER=7.1.7
INET_POSIX_VER=2.0.3
RECON_VER=2.1

# Changing to the SDK's library directory in order to create needed CMake directories for build preparation.
cd lib/c/

# Clean up the build CMake directories and create them anew if needed.
rm -fr dof-pcr-dev
rm -fr dof-pcr-posix
rm -fr dof-cipher-plugins
rm -fr dof-oal
rm -fr dof-inet-posix
rm -fr dof-connection-reconnecting-listener

mkdir dof-pcr-dev
mkdir dof-pcr-posix
mkdir dof-cipher-plugins
mkdir dof-oal
mkdir dof-inet-posix
mkdir dof-connection-reconnecting-listener

# Start the building of each source library in the order of dependent modules first.
cd dof-pcr-dev/
if [ -n "$1" ];
then
  cmake ../core-c-dof-pcr-dev-$PCR_DEV_VER/ -DCMAKE_INSTALL_PREFIX=$1
else
  cmake ../core-c-dof-pcr-dev-$PCR_DEV_VER/
fi
cmake --build . --target install

cd ..
cd dof-pcr-posix/
if [ -n "$1" ];
then
  cmake ../core-c-dof-pcr-posix-$PCR_POSIX_VER/ -DCMAKE_INSTALL_PREFIX=$1
else
  cmake ../core-c-dof-pcr-posix-$PCR_POSIX_VER/
fi
cmake --build . --target install

cd ..
cd dof-cipher-plugins/
if [ -n "$1" ];
then
  cmake ../core-c-dof-cipher-plugins-$CIPHER_VER/ -DCMAKE_INSTALL_PREFIX=$1
else
  cmake ../core-c-dof-cipher-plugins-$CIPHER_VER/
fi
cmake --build . --target install

cd ..
cd dof-oal/
if [ -n "$1" ];
then
  cmake ../core-c-dof-oal-$OAL_VER/ -DCMAKE_INSTALL_PREFIX=$1
else
  cmake ../core-c-dof-oal-$OAL_VER/
fi
cmake --build . --target install

cd ..
cd dof-inet-posix/
if [ -n "$1" ];
then
  cmake ../core-c-dof-inet-posix-$INET_POSIX_VER/ -DCMAKE_INSTALL_PREFIX=$1
else
  cmake ../core-c-dof-inet-posix-$INET_POSIX_VER/
fi
cmake --build . --target install

cd ..
cd dof-connection-reconnecting-listener/
if [ -n "$1" ];
then
  cmake ../core-c-dof-connection-reconnecting-listener-$RECON_VER/ -DCMAKE_INSTALL_PREFIX=$1
else
  cmake ../core-c-dof-connection-reconnecting-listener-$RECON_VER/
fi
cmake --build . --target install