#!/bin/sh

# SPDX-FileCopyrightText: 2014 Boris Pek <tehnick-8@yandex.ru>
# SPDX-FileCopyrightText: 2018 Jeremy Lainé <jeremy.laine@m4x.org>
#
# SPDX-License-Identifier: CC0-1.0

set -e

install_cmake() {
    NAME=$1
    URL=$2
    OPTS=$3
    CURRENT_DIR=$(pwd)

    cd
    git clone $URL $NAME
    mkdir -p $NAME-build
    cmake $NAME -B./$NAME-build/ -DCMAKE_INSTALL_PREFIX="/usr/local" -DCMAKE_BUILD_TYPE=Release $OPTS
    cmake --build $NAME-build --parallel
    sudo cmake --build $NAME-build --target install

    cd $CURRENT_DIR
}

# Used for QCA cmake options
case "$QT_VERSION" in
6*)
    BUILD_WITH_QT6="ON"
    ;;
5*)
    BUILD_WITH_QT6="OFF"
    ;;
esac

# install base tools
sudo apt-get update -qq
sudo apt-get install -qq clang cmake git curl

# install extra dependencies
case "$CONFIG" in
full*)
    sudo apt-get install -qq \
        doxygen graphviz \
        libgstreamer1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly \
        libprotobuf-c-dev \
        libssl-dev

    # Build and install qca and libomemo-c
    install_cmake libomemo-c https://github.com/dino/libomemo-c "-DBUILD_SHARED_LIBS=ON"
    install_cmake qca https://invent.kde.org/libraries/qca "-DBUILD_TESTS=OFF -DBUILD_TOOLS=OFF -DBUILD_WITH_QT6=${BUILD_WITH_QT6}"
    ;;
esac
