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

trigger-ps-post-extract() {
  declare desc="ps post-extract plugin trigger"
  declare trigger="post-extract"
  declare APP="$1" TMP_WORK_DIR="$2" REV="$3"

  if [[ ! -f "$TMP_WORK_DIR/Procfile" ]]; then
    return
  fi

  pushd "$TMP_WORK_DIR" >/dev/null
  for process_type in $(procfile-util list); do
    if echo "$process_type" | grep -vqE '^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$'; then
      dokku_log_fail "Invalid process type name ($process_type)"
    fi
  done
  popd >/dev/null
}

trigger-ps-post-extract "$@"
