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

trigger-builder-railpack-core-post-extract() {
  declare desc="builder-railpack post-extract plugin trigger"
  declare trigger="post-extract"
  declare APP="$1" SOURCECODE_WORK_DIR="$2"
  local NEW_RAILPACK_JSON="$(fn-builder-railpack-computed-railpackjson-path "$APP")"

  pushd "$SOURCECODE_WORK_DIR" >/dev/null

  if [[ -z "$NEW_RAILPACK_JSON" ]]; then
    return
  fi

  if [[ ! -f "$NEW_RAILPACK_JSON" ]]; then
    rm -f railpack.json
    return
  fi

  if [[ "$NEW_RAILPACK_JSON" != "railpack.json" ]]; then
    mv "$NEW_RAILPACK_JSON" railpack.json
  fi
  popd &>/dev/null || pushd "/tmp" >/dev/null
}

trigger-builder-railpack-core-post-extract "$@"
