#!/bin/bash
set -euo pipefail
set -x
# get machine / kernel name
_get_platform() {
  uname | tr '[:upper:]' '[:lower:]'
}

# get macos sdk directory
_get_macos_sdk_dir() {
  if [[ -z "${MACOS_SDK_DIR-}" ]]; then
    platform=$(_get_platform)
    if [[ "$platform" = 'darwin' ]]; then
      if [[ "$(command -v xcrun)" ]]; then
        echo "$(xcrun --sdk macosx --show-sdk-path)"
      else
        echo "You need to have MacOS Command Line Tools installed, you can install it via '$ xcode-select --install'"
        exit 1
      fi
    else
      echo "You must set the MACOS_SDK_DIR env var to where you have the MacOS SDK installed"
      echo "If you do not have a MacOS SDK installed, see https://github.com/joseluisq/macosx-sdks/tree/12.3 to obtain one"
      exit 1
    fi
  else
    echo "$MACOS_SDK_DIR"
  fi
}

macos_sdk_dir=$(_get_macos_sdk_dir)
framework_search_path="/System/Library/Frameworks"
include_search_path='/usr/include'

ZIG_FLAGS_DARWIN="-isysroot$macos_sdk_dir \
  -F$macos_sdk_dir$framework_search_path \
  -iframeworkwithsysroot$framework_search_path \
  -iwithsysroot$include_search_path \
  -mmacosx-version-min=11.7.1" \
ZIG_EXEC=$(which zig) \
CHAINLINK_VERSION=$(jq -r '.version' package.json) \
goreleaser "$@"
