#!/bin/bash
#
# COS SDK CMake Build script. This script can be used to build all available source libraries included in the COS 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 COS 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.

CIPHER_DEV_VER=1.0
AES_CIPHER_VER=1.0.1
COS_VER=7.0

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

# Clean up the build CMake directories and create them anew if needed.
rm -fr dof-cipher-plugins-dev
rm -fr dof-aes-cipher-plugin
rm -fr cos

mkdir dof-cipher-plugins-dev
mkdir dof-aes-cipher-plugin
mkdir cos

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

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

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