#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/builder-herokuish/functions"
source "$PLUGIN_AVAILABLE_PATH/builder-herokuish/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

trigger-builder-herokuish-builder-detect() {
  declare desc="builder-herokuish builder-detect plugin trigger"
  declare trigger="builder-detect"
  declare APP="$1" SOURCECODE_WORK_DIR="$2"
  local ARCHITECTURE="$(dpkg --print-architecture 2>/dev/null || true)"

  if [[ "$ARCHITECTURE" == "armhf" ]] || [[ "$ARCHITECTURE" == "arm64" ]]; then
    if [[ "$(fn-builder-herokuish-computed-allowed "$APP")" != "true" ]]; then
      return
    fi
  fi

  if fn-has-buildpacks-file "$SOURCECODE_WORK_DIR"; then
    echo "herokuish"
    return
  fi

  if fn-has-buildpack-dotenv "$SOURCECODE_WORK_DIR"; then
    echo "herokuish"
    return
  fi

  if fn-has-buildpack-env "$APP"; then
    echo "herokuish"
    return
  fi
}

trigger-builder-herokuish-builder-detect "$@"
