#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

trigger-builder-pack-builder-build() {
  declare desc="builder-pack builder-build plugin trigger"
  declare trigger="builder-build"
  declare BUILDER_TYPE="$1" APP="$2" SOURCECODE_WORK_DIR="$3"

  if [[ "$BUILDER_TYPE" != "pack" ]]; then
    return
  fi

  local stack="$(plugn trigger buildpack-stack-name "$APP")"
  if [[ -n "$stack" ]]; then
    DOKKU_CNB_BUILDER="$stack"
  fi

  dokku_log_info1 "Building $APP from cnb stack $DOKKU_CNB_BUILDER..."

  if ! command -v "pack" &>/dev/null; then
    dokku_log_fail "Missing pack, install it"
  fi

  local IMAGE=$(get_app_image_name "$APP")

  pushd "$SOURCECODE_WORK_DIR" &>/dev/null

  ENV_ARGS=($(config_export app "$APP" --format pack-keys --merged))
  eval "$(config_export app "$APP" --merged)"

  if fn-plugn-trigger-exists "pre-build-pack"; then
    dokku_log_warn "Deprecated: please upgrade plugin to use 'pre-build' plugin trigger instead of pre-build-pack"
    plugn trigger pre-build-pack "$APP" "$SOURCECODE_WORK_DIR"
  fi
  plugn trigger pre-build "$BUILDER_TYPE" "$APP" "$SOURCECODE_WORK_DIR"

  local DOCKER_ARGS=$(: | plugn trigger docker-args-build "$APP" "$BUILDER_TYPE")
  DOCKER_ARGS+=$(: | plugn trigger docker-args-process-build "$APP" "$BUILDER_TYPE")
  [[ "$DOKKU_TRACE" ]] && DOCKER_ARGS+=" --env=TRACE=true "

  DOCKER_ARGS=" $DOCKER_ARGS "
  eval set -- "$DOCKER_ARGS"

  declare -a PACK_ARGS
  while true; do
    case "$1" in
      -b | --buildpack)
        PACK_ARGS+=("--buildpack")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      -b=* | --buildpack=*)
        if [[ "$1" == "--buildpack=*" ]]; then
          PACK_ARGS+=("--buildpack" "${1#--buildpack=}")
        elif [[ "$1" == "-b=*" ]]; then
          PACK_ARGS+=("--buildpack" "${1#-b=}")
        fi
        shift 1
        ;;
      -r | --buildpack-registry)
        PACK_ARGS+=("--buildpack-registry")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      -r=* | --buildpack-registry=*)
        if [[ "$1" == "--buildpack-registry=*" ]]; then
          PACK_ARGS+=("--buildpack-registry" "${1#--buildpack-registry=}")
        elif [[ "$1" == "-r=*" ]]; then
          PACK_ARGS+=("--buildpack-registry" "${1#-r=}")
        fi
        shift 2
        ;;
      -e | --env)
        PACK_ARGS+=("--env")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      -e=* | --env=*)
        if [[ "$1" == "--env=*" ]]; then
          PACK_ARGS+=("--env" "${1#--env=}")
        elif [[ "$1" == "-e=*" ]]; then
          PACK_ARGS+=("--env" "${1#-e=}")
        fi
        shift 1
        ;;
      -t | --tag)
        PACK_ARGS+=("--tag")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      -t=* | --tag=*)
        if [[ "$1" == "--tag=*" ]]; then
          PACK_ARGS+=("--tag" "${1#--tag=}")
        elif [[ "$1" == "-t=*" ]]; then
          PACK_ARGS+=("--tag" "${1#-t=}")
        fi
        shift 1
        ;;
      --volume | -v)
        PACK_ARGS+=("--volume")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --volume=* | -v=*)
        if [[ "$1" == "--volume=*" ]]; then
          PACK_ARGS+=("--volume" "${1#--volume=}")
        elif [[ "$1" == "-v=*" ]]; then
          PACK_ARGS+=("--volume" "${1#-v=}")
        fi
        shift 1
        ;;
      --cache)
        PACK_ARGS+=("--cache")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --cache=*)
        PACK_ARGS+=("--cache" "${1#--cache=}")
        shift 1
        ;;
      --cache-image)
        PACK_ARGS+=("--cache-image")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --cache-image=*)
        PACK_ARGS+=("--cache-image" "${1#--cache-image=}")
        shift 1
        ;;
      --creation-time)
        PACK_ARGS+=("--creation-time")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --creation-time=*)
        PACK_ARGS+=("--creation-time" "${1#--creation-time=}")
        shift 1
        ;;
      --env-file)
        PACK_ARGS+=("--env-file")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --env-file=*)
        PACK_ARGS+=("--env-file" "${1#--env-file=}")
        shift 1
        ;;
      --extension)
        PACK_ARGS+=("--extension")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --extension=*)
        PACK_ARGS+=("--extension" "${1#--extension=}")
        shift 1
        ;;
      --gid)
        PACK_ARGS+=("--gid")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --gid=*)
        PACK_ARGS+=("--gid" "${1#--gid=}")
        shift 1
        ;;
      --insecure-registry)
        PACK_ARGS+=("--insecure-registry")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --insecure-registry=*)
        PACK_ARGS+=("--insecure-registry" "${1#--insecure-registry=}")
        shift 1
        ;;
      --lifecycle-image)
        PACK_ARGS+=("--lifecycle-image")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --lifecycle-image=*)
        PACK_ARGS+=("--lifecycle-image" "${1#--lifecycle-image=}")
        shift 1
        ;;
      --network)
        PACK_ARGS+=("--network")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --network=*)
        PACK_ARGS+=("--network" "${1#--network=}")
        shift 1
        ;;
      --platform)
        PACK_ARGS+=("--platform")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --platform=*)
        PACK_ARGS+=("--platform" "${1#--platform=}")
        shift 1
        ;;
      --post-buildpack)
        PACK_ARGS+=("--post-buildpack")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --post-buildpack=*)
        PACK_ARGS+=("--post-buildpack" "${1#--post-buildpack=}")
        shift 1
        ;;
      --pre-buildpack)
        PACK_ARGS+=("--pre-buildpack")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --pre-buildpack=*)
        PACK_ARGS+=("--pre-buildpack" "${1#--pre-buildpack=}")
        shift 1
        ;;
      --previous-image)
        PACK_ARGS+=("--previous-image")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --previous-image=*)
        PACK_ARGS+=("--previous-image" "${1#--previous-image=}")
        shift 1
        ;;
      --pull-policy)
        PACK_ARGS+=("--pull-policy")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --pull-policy=*)
        PACK_ARGS+=("--pull-policy" "${1#--pull-policy=}")
        shift 1
        ;;
      --run-image)
        PACK_ARGS+=("--run-image")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --run-image=*)
        PACK_ARGS+=("--run-image" "${1#--run-image=}")
        shift 1
        ;;
      --uid)
        PACK_ARGS+=("--uid")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --uid=*)
        PACK_ARGS+=("--uid" "${1#--uid=}")
        shift 1
        ;;
      --workspace)
        PACK_ARGS+=("--workspace")
        PACK_ARGS+=("$2")
        shift 2
        ;;
      --workspace=*)
        PACK_ARGS+=("--workspace" "${1#--workspace=}")
        shift 1
        ;;
      --clear-cache | --disable-system-buildpacks | --publish | --trust-builder | --trust-extra-buildpacks | --userns-host)
        PACK_ARGS+=("$1")
        shift 1
        ;;
      *)
        if [[ -n "$1" ]]; then
          shift
          continue
        fi
        break
        ;;
    esac
  done

  if [ -n "$BUILDPACK_URL" ]; then
    PACK_ARGS+=("--builder")
    PACK_ARGS+=("$BUILDPACK_URL")
  elif [ -f "$SOURCECODE_WORK_DIR/.buildpacks" ]; then
    while read -r buildpack; do
      if [ -z "$buildpack" ]; then
        continue
      fi

      PACK_ARGS+=("--builder")
      PACK_ARGS+=("$buildpack")
    done <"$SOURCECODE_WORK_DIR/.buildpacks"
  fi

  local DOCKER_CONFIG
  DOCKER_CONFIG="$(fn-registry-docker-config-dir "$APP")"
  [[ -n "$DOCKER_CONFIG" ]] && export DOCKER_CONFIG
  pack build "$IMAGE" --builder "$DOKKU_CNB_BUILDER" --path "$SOURCECODE_WORK_DIR" --default-process web "${PACK_ARGS[@]}" "${ENV_ARGS[@]}"
  docker-image-labeler relabel --label=dokku --label=org.label-schema.schema-version=1.0 --label=org.label-schema.vendor=dokku --label=com.dokku.image-stage=build --label=com.dokku.builder-type=pack --label=com.dokku.app-name=$APP "$IMAGE"

  # ensure we have a port mapping
  plugn trigger ports-configure "$APP"
  plugn trigger ports-set-detected "$APP" "http:$(plugn trigger ports-get-property "$APP" proxy-port):5000"

  if fn-plugn-trigger-exists "post-build-pack"; then
    dokku_log_warn "Deprecated: please upgrade plugin to use 'post-build' plugin trigger instead of post-build-pack"
    plugn trigger post-build-pack "$APP" "$SOURCECODE_WORK_DIR"
  fi
  plugn trigger post-build "$BUILDER_TYPE" "$APP" "$SOURCECODE_WORK_DIR"
}

trigger-builder-pack-builder-build "$@"
