From f0eba63b3318a44321094ff5ee1c7e1efc6da337 Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Fri, 17 Sep 2021 17:52:24 +0200
Subject: [PATCH] [#107] Added basic script to run projects with proper working
 directory

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 scripts/build.sh | 26 ++++++++++++++++++++------
 scripts/run.sh   | 28 ++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 6 deletions(-)
 create mode 100755 scripts/run.sh

diff --git a/scripts/build.sh b/scripts/build.sh
index fcf1324c..7e41ca8e 100755
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -1,8 +1,17 @@
 #!/bin/sh
+# Check if release or debug build
+CMAKE_BUILD_DIR="build"
+CMAKE_FLAGS=""
+if [ "$1" = "--debug" ]; then
+	CMAKE_BUILD_DIR="cmake-build-debug"
+	CMAKE_FLAGS="-DCMAKE_BUILD_TYPE=Debug"
+elif [ "$1" = "--release" ]; then
+	CMAKE_BUILD_DIR="cmake-build-release"
+	CMAKE_FLAGS="-DCMAKE_BUILD_TYPE=Release"
+fi
 
 # Navigate to the main directory of the cloned repository
-BASEDIR=$(dirname "$0")
-cd $BASEDIR
+cd "$(dirname "$0")" || exit
 cd ..
 
 # Setup git lfs and the submodules
@@ -11,9 +20,14 @@ git submodule init
 git submodule update
 
 # Setup build directory
-mkdir build
-cd build
+mkdir $CMAKE_BUILD_DIR
+cd $CMAKE_BUILD_DIR || exit
+BUILD_THREADS=$(($(nproc --all) - 1))
+
+if [ $BUILD_THREADS -lt 1 ]; then
+	BUILD_THREADS=1
+fi
 
 # Build process
-cmake ..
-make
\ No newline at end of file
+cmake $CMAKE_FLAGS ..
+make -j $BUILD_THREADS "$@"
\ No newline at end of file
diff --git a/scripts/run.sh b/scripts/run.sh
new file mode 100755
index 00000000..8e2ead55
--- /dev/null
+++ b/scripts/run.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+# Navigate to the scripts directory
+cd "$(dirname "$0")" || exit
+
+# Check if there is a project name as argument
+if [ $# -lt 1 ]; then
+	echo "You need to specify a project name to run!">&2
+	exit
+fi
+
+PROJECT=$1
+PROJECT_DIR="../projects/$PROJECT"
+
+# Check if the project name is valid
+if [ ! -d "$PROJECT_DIR" ]; then
+	echo "There is no project with the name '$PROJECT'!">&2
+	exit
+fi
+
+./build.sh $PROJECT
+cd "$PROJECT_DIR" || exit
+
+if [ ! -f "$PROJECT" ]; then
+	echo "Building the project '$PROJECT' failed!">&2
+	exit
+fi
+
+./$PROJECT
\ No newline at end of file
-- 
GitLab