#!/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_BUILD_TYPE=Release $OPTS
    cmake --build $NAME-build --parallel
    sudo cmake --build $NAME-build --target install

    cd $CURRENT_DIR
}

if [ "$(uname -s)" = "Darwin" ]; then
    brew update
    brew unlink python@3
    brew install python@3 || true
    brew link --overwrite python@3

    case "$CONFIG" in
    full*)
        brew install doxygen gstreamer gst-plugins-base gst-plugins-good gst-plugins-ugly
        ;;
    esac
else
    sudo apt-get update -qq
    sudo apt-get install -qq clang cmake

    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
        ;;
    esac
fi

case "$CONFIG" in
full*)
    # Enable OMEMO for Qt > 5.15 (including Qt 6)
    # Build and install qca and libomemo-c
    case "$QT_VERSION" in
    5.15*)
        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=OFF"
        ;;
    6*)
        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=ON"
        ;;
    esac
    ;;
esac
