#!/usr/bin/env bash

OS=${1}
GITHUB_WORKSPACE=${2}
GITHUB_REF=${3}

if [[ ! ${OS} || ! ${GITHUB_WORKSPACE} ]]; then
    echo "Error: Invalid options"
    echo "Usage: ${0} <operating system> <github workspace path>"
    exit 1
fi
echo "----------------------------------------"
echo "OS: ${OS}"
echo "----------------------------------------"

if [[ ${OS} == "arm32v7-disable-wallet" || ${OS} == "linux-disable-wallet" || ${OS} == "aarch64-disable-wallet" ]]; then
    OS=`echo ${OS} | cut -d"-" -f1`
fi

echo "----------------------------------------"
echo "Building Dependencies for ${OS}"
echo "----------------------------------------"

cd depends
if [[ ${OS} == "windows" ]]; then
    make HOST=x86_64-w64-mingw32 -j2
elif [[ ${OS} == "osx" ]]; then
    mkdir SDKs
    cd SDKs
    SDK_TARBALL="Xcode-11.3.1-11C505-extracted-SDK-with-libcxx-headers.tar.gz"
    SDK_CACHE_PATH="${GITHUB_WORKSPACE}/SDKs/${SDK_TARBALL}"
    if [[ -n "${OSX_SDK_TARBALL}" && -f "${OSX_SDK_TARBALL}" ]]; then
        cp "${OSX_SDK_TARBALL}" "${SDK_TARBALL}"
    elif [[ -f "${SDK_CACHE_PATH}" ]]; then
        cp "${SDK_CACHE_PATH}" "${SDK_TARBALL}"
    else
        echo "Error: macOS SDK tarball not found in cache or at path '${OSX_SDK_TARBALL}'."
        exit 1
    fi
    tar -zxf "${SDK_TARBALL}" || { echo "Error: Failed to extract macOS SDK."; exit 1; }
    rm -f "${SDK_TARBALL}"
    cd ..
    make HOST=x86_64-apple-darwin14 -j2
elif [[ ${OS} == "linux" || ${OS} == "linux-disable-wallet" ]]; then
    make HOST=x86_64-linux-gnu -j2
elif [[ ${OS} == "arm32v7" || ${OS} == "arm32v7-disable-wallet" ]]; then
    make HOST=arm-linux-gnueabihf -j2
elif [[ ${OS} == "aarch64" || ${OS} == "aarch64-disable-wallet" ]]; then
    make HOST=aarch64-linux-gnu -j2
fi
