#!/bin/bash
set -e

case $(uname -m) in
  x86_64|x64)
    vcpkg_arch=x64
    linux_arch=x86_64
    osx_arch=x86_64
    ;;
  aarch64|arm64)
    vcpkg_arch=arm64
    linux_arch=aarch64
    osx_arch=arm64
    ;;
  *)
    echo "Architecture not supported"
    exit 1
    ;;
esac

case $(uname) in
  Linux)
    os=linux
    nproc=$(nproc)
    ;;
  Darwin)
    os=osx
    nproc=$(sysctl -n hw.logicalcpu)
    if ! which brew >/dev/null || [ ! -x $(brew --prefix)/opt/bison/bin/bison ]
    then
      echo 'The version of bison provided with macOS is too old.'
      echo 'Please install a newer version with Homebrew (https://brew.sh):'
      echo '$ brew install bison'
      exit 1
    else
      export PATH="$(brew --prefix)/opt/bison/bin:$PATH"
    fi
    ;;
  *)
    echo "OS not supported"
    exit 1
    ;;
esac

triplet=$vcpkg_arch-$os

# Find vcpkg or download it if required
if [ -z "$VCPKG_INSTALLATION_ROOT" ]; then
  if [ -n "$VCPKG_ROOT" ]; then
    VCPKG_INSTALLATION_ROOT=$VCPKG_ROOT
  else
    VCPKG_INSTALLATION_ROOT=$PWD/build/vcpkg
    if [ ! -d "$VCPKG_INSTALLATION_ROOT" ]; then
        git clone https://github.com/microsoft/vcpkg.git "$VCPKG_INSTALLATION_ROOT"
        $VCPKG_INSTALLATION_ROOT/bootstrap-vcpkg.sh
    fi
  fi
fi

# Cmake build types
build_types="Debug Release"

# Only build release configuration in CI
if [ "$GITHUB_ACTIONS" = "true" ]
then
  build_types="Release"
  custom_triplets_dir=$PWD/build/custom-triplets
  mkdir -p "$custom_triplets_dir"
  for vcpkg_triplet_file in $VCPKG_INSTALLATION_ROOT/triplets/{,community/}$triplet.cmake
  do
    if [ -f "$vcpkg_triplet_file" ]; then
        custom_triplet_file="$custom_triplets_dir/$triplet.cmake"
        cp "$vcpkg_triplet_file" "$custom_triplet_file"
        echo "set(VCPKG_BUILD_TYPE release)" >> "$custom_triplet_file"
    fi
  done
  options+=" -D VCPKG_OVERLAY_TRIPLETS=$custom_triplets_dir"
fi

for build_type in $build_types
do
  echo ">> Building ParquetSharpNative $build_type for $triplet"
  build_dir=build/$triplet-$(tr A-Z a-z <<<$build_type)
  cmake -B $build_dir -S . -D VCPKG_TARGET_TRIPLET=$triplet -D CMAKE_TOOLCHAIN_FILE=$VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake -D CMAKE_BUILD_TYPE=$build_type $options
  cmake --build $build_dir -j ${nproc}
done