#!/bin/bash

#This script does the following:
#Clones the open62541 repository from GitHub
#Fetches the pull request which contains the recent version of TSN applications
#Builds the open62541 repository and compiles the TSN applications

#Usage: ./build_open62541.sh

#Program name and version
program=$(basename $0)
version=2.0

#Let us install open62541 to evaluate the performance of OPC UA application that includes PubSub and Time-Sensitive Networking
#Traverse to home directory
cd ~

#Exit when open62541 folder is already available
if test -d open62541; then
    echo "open62541 folder already exists. Terminating script execution"
    exit
fi

#Clone the open62541 stack from GitHub
git clone https://github.com/open62541/open62541.git

#Fetch the pull request and create a new branch
#This pull request contains the latest version of the TSN application required for our setup
cd open62541
git fetch origin pull/4384/head:local_branch && git checkout local_branch

#Create a build directory
mkdir build && cd build/

#Build the open62541 application with the cmake options
cmake -DUA_BUILD_EXAMPLES=ON -DUA_ENABLE_PUBSUB=ON -DUA_ENABLE_PUBSUB_ETH_UADP=ON ..

#Compile the open62541 application
make -j$(nproc)

