#!/usr/bin/env bash
#/ Usage: ghe-gc-disable [<option>...] <host>
#/
#/ Helper to disable and drain GC operations on a GitHub Enterprise server.
#/
#/ OPTIONS:
#/   -F	<configfile>		Alternative SSH per-user configuration file.
#/
set -e

# Bring in the backup configuration
# shellcheck source=share/github-backup-utils/ghe-backup-config
. "$( dirname "${BASH_SOURCE[0]}" )/ghe-backup-config"

while true; do
  case "$1" in
    -F)
      opts="$1 $2"
      shift 2
      ;;
    *)
      host="$1"
      shift
      break
      ;;
  esac
done

# Show usage with no host
[ -z "$host" ] && print_usage

# Exit early when testing
[ -n "$GHE_TEST_REMOTE_VERSION" ] && exit 0

# gc_disable is a function defined in ghe-backup-config
echo "set -o pipefail; $(declare -f gc_disable); gc_disable \"$SYNC_IN_PROGRESS_FILE\" \"$GHE_GIT_COOLDOWN_PERIOD\"" | ghe-ssh $opts "$host" -- /bin/bash || {
  res=$?
  if [ $res = 7 ]; then
    log_error "Error: Git GC processes remain after $GHE_GIT_COOLDOWN_PERIOD seconds. Aborting..." 1>&2
  fi
  exit $res
}
