#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$(dirname "$(dirname "${BASH_SOURCE[0]}")")/functions"

cmd-google-auth-disable() {
  declare desc="remove Google sign-in from an app"
  local cmd="google-auth:disable"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1"

  [[ -n "$APP" ]] || dokku_log_fail "usage: dokku google-auth:disable <app>"
  verify_app_name "$APP"

  fn-ga-app-set-enabled "$APP" false
  fn-ga-apply "$APP"
  dokku_log_info2 "Google auth disabled for $APP"

  local auth_host
  auth_host="$(fn-ga-global-get auth-host)"
  if [[ -n "$auth_host" && -f "$DOKKU_ROOT/$APP/VHOST" ]] && grep -qxF "$auth_host" "$DOKKU_ROOT/$APP/VHOST"; then
    dokku_log_warn "$APP served the auth host '$auth_host'; sign-in for other apps will break until another enabled app serves it"
  fi
}

cmd-google-auth-disable "$@"
