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

fn-builder-herokuish-ensure-cache() {
  declare APP="$1"

  existing_cache="$("$DOCKER_BIN" volume ls --quiet --filter "label=com.dokku.app-name=$APP" --filter label=com.dokku.builder-type=herokuish)"
  if [[ "$existing_cache" != "cache-$APP" ]]; then
    "$DOCKER_BIN" volume rm "cache-$APP" &>/dev/null || true
    "$DOCKER_BIN" volume create "--label=org.label-schema.schema-version=1.0" "--label=org.label-schema.vendor=dokku" "--label=com.dokku.app-name=$APP" "--label=com.dokku.builder-type=herokuish" "cache-$APP" >/dev/null
  fi
}

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

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

  local stack="$(plugn trigger buildpack-stack-name "$APP")"
  if [[ -n "$stack" ]]; then
    dokku_log_info1 "Building $APP from $stack"
    DOKKU_IMAGE="$stack"
  else
    dokku_log_info1 "Building $APP from herokuish"
  fi

  local IMAGE=$(get_app_image_name "$APP")
  local DOCKER_COMMIT_LABEL_ARGS=("--change" "LABEL dokku=" "--change" "LABEL org.label-schema.schema-version=1.0" "--change" "LABEL org.label-schema.vendor=dokku" "--change" "LABEL com.dokku.image-stage=build" "--change" "LABEL com.dokku.builder-type=herokuish" "--change" "LABEL com.dokku.app-name=$APP")
  local DOCKER_RUN_LABEL_ARGS=("--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=herokuish" "--label=com.dokku.app-name=$APP")
  local CID TAR_CID

  pushd "$SOURCECODE_WORK_DIR" &>/dev/null
  if [[ -f "$SOURCECODE_WORK_DIR/.dockerignore" ]]; then
    rm -f "$SOURCECODE_WORK_DIR/.dockerignore"
  fi

  eval "$(config_export app "$APP")"
  plugn trigger builder-create-dokku-image "$BUILDER_TYPE" "$APP" "$SOURCECODE_WORK_DIR" "$DOKKU_IMAGE"
  NEW_DOKKU_IMAGE=$(plugn trigger builder-dokku-image "$BUILDER_TYPE" "$APP" "$SOURCECODE_WORK_DIR" "$DOKKU_IMAGE")
  [[ -n "$NEW_DOKKU_IMAGE" ]] && DOKKU_IMAGE="$NEW_DOKKU_IMAGE"

  local DOCKER_BUILD_LABEL_ARGS=("--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=herokuish" "--label=com.dokku.app-name=$APP" "--label=dokku")
  DOKKU_APP_USER=$(config_get "$APP" DOKKU_APP_USER || true)
  DOKKU_APP_USER=${DOKKU_APP_USER:="herokuishuser"}
  if ! suppress_output "$DOCKER_BIN" image build "${DOCKER_BUILD_LABEL_ARGS[@]}" $DOKKU_GLOBAL_BUILD_ARGS -f "$PLUGIN_AVAILABLE_PATH/builder-herokuish/dockerfiles/copy-source.Dockerfile" --build-arg APP_IMAGE="$DOKKU_IMAGE" --build-arg "DOKKU_APP_USER=$DOKKU_APP_USER" --build-arg "TRACE=$DOKKU_TRACE" -t $IMAGE "$SOURCECODE_WORK_DIR"; then
    DOKKU_SKIP_IMAGE_CLEANUP_REGISTRATION=1 plugn trigger scheduler-register-retired "$APP" "$TAR_CID"
    dokku_log_warn "Failure extracting app code"
    return 1
  fi

  if fn-plugn-trigger-exists "pre-build-buildpack"; then
    dokku_log_warn "Deprecated: please upgrade plugin to use 'pre-build' plugin trigger instead of pre-build-buildpack"
    plugn trigger pre-build-buildpack "$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")
  if [[ -n "$DOKKU_TRACE" ]]; then
    DOCKER_ARGS+=" --env=TRACE=true "
    DOCKER_ARGS+=" --env=BUILDPACK_XTRACE=1 "
  fi
  DOCKER_ARGS+=$(: | plugn trigger docker-args-process-build "$APP" "$BUILDER_TYPE")

  declare -a ARG_ARRAY
  eval "ARG_ARRAY=($DOCKER_ARGS)"

  local DOKKU_CONTAINER_EXIT_CODE=0
  fn-builder-herokuish-ensure-cache "$APP"
  if ! CID=$("$DOCKER_BIN" container create "${DOCKER_RUN_LABEL_ARGS[@]}" $DOKKU_GLOBAL_RUN_ARGS -v "cache-$APP:/cache" --env=CACHE_PATH=/cache "${ARG_ARRAY[@]}" "$IMAGE" /build); then
    plugn trigger scheduler-register-retired "$APP" "$CID"
    dokku_log_warn "Failure during app build"
    return 1
  fi

  plugn trigger post-container-create "app" "$CID" "$APP" "build"
  "$DOCKER_BIN" container start "$CID" >/dev/null || DOKKU_CONTAINER_EXIT_CODE=$?
  if ! "$DOCKER_BIN" container attach "$CID"; then
    plugn trigger scheduler-register-retired "$APP" "$CID"
    dokku_log_warn "Failure during app build"
    return 1
  fi

  DOKKU_CONTAINER_EXIT_CODE="$("$DOCKER_BIN" container wait "$CID" 2>/dev/null || echo "$DOKKU_CONTAINER_EXIT_CODE")"
  if [[ "$DOKKU_CONTAINER_EXIT_CODE" -ne 0 ]]; then
    plugn trigger scheduler-register-retired "$APP" "$CID"
    dokku_log_warn "Failure during app build"
    return 1
  fi

  "$DOCKER_BIN" container commit "${DOCKER_COMMIT_LABEL_ARGS[@]}" "$CID" "$IMAGE" >/dev/null
  plugn trigger scheduler-register-retired "$APP" "$CID"

  # 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-buildpack"; then
    dokku_log_warn "Deprecated: please upgrade plugin to use 'post-build' plugin trigger instead of post-build-buildpack"
    plugn trigger post-build-buildpack "$APP" "$SOURCECODE_WORK_DIR"
  fi
  plugn trigger post-build "$BUILDER_TYPE" "$APP" "$SOURCECODE_WORK_DIR"
}

trigger-builder-herokuish-builder-build "$@"
