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

fn-scheduler-docker-local-stop-container() {
  declare CONTAINER_ID_OR_NAME="$1" DOCKER_STOP_TIMEOUT="$2"

  if [ ! "$("$DOCKER_BIN" container ls -q -f "name=$CONTAINER_ID_OR_NAME" 2>/dev/null)" ]; then
    dokku_log_warn "Specified container does not exist"
    return 0
  fi

  [[ $DOKKU_DOCKER_STOP_TIMEOUT ]] && DOCKER_STOP_TIME_ARG="-t=${DOKKU_DOCKER_STOP_TIMEOUT}"
  if "$DOCKER_BIN" container stop $DOCKER_STOP_TIME_ARG "$CONTAINER_ID_OR_NAME"; then
    return
  fi

  if "$DOCKER_BIN" container kill "$CONTAINER_ID_OR_NAME"; then
    return
  fi

  dokku_log_warn "Unable to stop container ${CONTAINER_ID_OR_NAME}"
  return 1
}

fn-scheduler-docker-local-stop-app-run-container() {
  declare desc="stops a named run container belonging to an app"
  declare APP="$1" CONTAINER_NAME="$2" DOCKER_STOP_TIMEOUT="$3"
  local NAME

  while IFS= read -r NAME; do
    if [[ "$NAME" == "$CONTAINER_NAME" ]]; then
      fn-scheduler-docker-local-stop-container "$CONTAINER_NAME" "$DOCKER_STOP_TIMEOUT"
      return $?
    fi
  done < <("$DOCKER_BIN" container ls --filter "label=com.dokku.app-name=$APP" --filter "label=com.dokku.container-type=run" --format '{{ .Names }}')

  dokku_log_warn "Specified container does not exist"
}

trigger-scheduler-docker-local-scheduler-run-stop() {
  declare desc="stops an app run container"
  declare trigger="scheduler-run-stop"
  declare DOKKU_SCHEDULER="$1" APP="$2" CONTAINER_NAME="$3"

  if [[ "$DOKKU_SCHEDULER" != "docker-local" ]]; then
    return
  fi

  if [[ -z "$APP" ]] && [[ -z "$CONTAINER_NAME" ]]; then
    dokku_log_fail "No container or app specified"
  fi

  if [[ -n "$CONTAINER_NAME" ]]; then
    if [[ "$(echo "$CONTAINER_NAME" | grep -o '\.' | wc -l)" -ne 2 ]]; then
      dokku_log_fail "Invalid container name specified: $CONTAINER_NAME"
    fi

    local CONTAINER_APP="$(echo "$CONTAINER_NAME" | cut -d'.' -f1)"
    if [[ -n "$APP" ]] && [[ "$APP" != "$CONTAINER_APP" ]]; then
      dokku_log_fail "Specified app does not match app in container name"
    fi

    APP="$CONTAINER_APP"
    STATE="$(echo "$CONTAINER_NAME" | cut -d'.' -f2)"
    if [[ "$STATE" != "run" ]]; then
      dokku_log_fail "Specified container must be a run container"
    fi
  fi

  verify_app_name "$APP"

  DOKKU_DOCKER_STOP_TIMEOUT="$(plugn trigger ps-get-property "$APP" stop-timeout-seconds || true)"
  if [[ -n "$CONTAINER_NAME" ]]; then
    fn-scheduler-docker-local-stop-app-run-container "$APP" "$CONTAINER_NAME" "$DOKKU_DOCKER_STOP_TIMEOUT"
    return $?
  fi

  CONTAINERS="$("$DOCKER_BIN" container ls --all --filter "label=com.dokku.app-name=$APP" --filter "label=com.dokku.container-type=run" --format '{{ .Names }}')"
  if [[ -z "$CONTAINERS" ]]; then
    dokku_log_quiet "No run containers exist"
    return
  fi

  exit_code=0
  while IFS= read -r CONTAINER_NAME; do
    if ! fn-scheduler-docker-local-stop-container "$CONTAINER_NAME" "$DOKKU_DOCKER_STOP_TIMEOUT"; then
      exit_code=1
    fi
  done <<<"$CONTAINERS"

  return "$exit_code"
}

trigger-scheduler-docker-local-scheduler-run-stop "$@"
