#!/bin/sh

#
# Copyright 2022 Signal Messenger, LLC
# SPDX-License-Identifier: AGPL-3.0-only
#

set -e

# shellcheck source=bin/env.sh
. "$(dirname "$0")"/env.sh

usage()
{
    echo 'usage: build-gctc [-d|-r|-c]
    where:
        -d to create a debug build (default)
        -r to create a release build
        -c to clean the build artifacts'
}

clean()
{
    # Remove all possible artifact directories.
    rm -rf ./src/node/build
    rm -rf ./src/node/dist
    rm -rf ./src/node/node_modules
    cargo clean
}

BUILD_TYPE=debug

while [ "$1" != "" ]; do
    case $1 in
        -d | --debug )
            BUILD_TYPE=debug
            ;;
        -r | --release )
            BUILD_TYPE=release
            ;;
        -c | --clean )
            clean
            exit
            ;;
        -h | --help )
            usage
            exit
            ;;
        * )
            usage
            exit 1
    esac
    shift
done

get_default_platform()
{
    hash rustup 2>/dev/null || { echo >&2 "Make sure you have rustup installed and properly configured! Aborting."; exit 1; }

    case "$(rustup show active-toolchain)" in
        *"x86_64-apple-darwin"* | *"aarch64-apple-darwin"* )
            echo "darwin"
            ;;
        *"x86_64-pc-windows"* )
            echo "win32"
            ;;
        *"x86_64-unknown-linux"* )
            echo "linux"
            ;;
        * )
            echo "unknown"
    esac
}

DEFAULT_PLATFORM=$(get_default_platform)
if [ "${DEFAULT_PLATFORM}" = "unknown" ]
then
    printf "Unknown platform detected!\nPlease make sure you have installed a valid Rust toolchain via rustup! Aborting.\n"
    exit 1
fi

export MACOSX_DEPLOYMENT_TARGET="10.15"

# Ensure that experimental compact relocation is disabled until upstream projects properly set it.
# https://issues.webrtc.org/issues/407797634
# https://chromium-review.googlesource.com/c/chromium/src/+/5938657
if [ "$(uname)" = "Linux" ]
then
  # Comment out the line that enables experimental crel.
  sed -i '/^[^#].*--allow-experimental-crel/ s/^/#/' src/webrtc/src/build/config/compiler/BUILD.gn
fi

# Build WebRTC.
(
    (
      cd src/webrtc/src
      WEBRTC_ARGS="rtc_build_examples=false rtc_build_tools=false rtc_include_tests=false rtc_enable_protobuf=false rtc_use_x11=false rtc_enable_sctp=false rtc_libvpx_build_vp9=true rtc_disable_metrics=true rtc_disable_trace_events=true"

      if [ "${BUILD_TYPE}" = "release" ]
      then
        WEBRTC_ARGS="${WEBRTC_ARGS} is_debug=false"
      fi
      gn gen -C out/"${BUILD_TYPE}" "--args=${WEBRTC_ARGS}"
      third_party/siso/cipd/siso ninja -C out/"${BUILD_TYPE}"
    )
    WEBRTC_OUT_PATH="src/webrtc/src/out"

    mkdir -p "${OUTPUT_DIR}/${BUILD_TYPE}/obj"
    STATIC_LIB_PATH="${BUILD_TYPE}"/obj/webrtc.lib
    if [ ! -e "${WEBRTC_OUT_PATH}/${STATIC_LIB_PATH}" ]; then
      STATIC_LIB_PATH="${BUILD_TYPE}"/obj/libwebrtc.a
    fi
    cp -f "${WEBRTC_OUT_PATH}/${STATIC_LIB_PATH}" "${OUTPUT_DIR}/${BUILD_TYPE}/obj"
)

# Build and link the final RingRTC library.
(
    if [ "${BUILD_TYPE}" = "debug" ]
    then
        OUTPUT_DIR="${OUTPUT_DIR}" cargo build --package ringrtc --bin group_call --features=native,sim_http
        echo "Can run with target/debug/group_call"
    else
        OUTPUT_DIR="${OUTPUT_DIR}" cargo build --package ringrtc --bin group_call --features=native,sim_http --release
        echo "Can run with target/release/group_call"
    fi
)
