#!/usr/bin/env bash
set -o errexit
set -o pipefail

echo "Stopping Sauce Connect"

# Sauce Connect 4 docs said that we can just kill -9 it if we don't care about
# failing any ongoing sessions. In practice, that sometimes worked but usually
# leaked a tunnel so badly that you couldn't even stop it from the web UI.
#
# Sauce Connect 5 appears to be *much* more prone to hung jobs. Hung jobs have
# a shutdown deadline of *three hours*, however, they appear to usually exit
# within a minute or so. Unfortunately the only thing the Sauce Connect 5 docs
# say about shutdown is that "you can stop your tunnel from the terminal where
# Sauce Connect is running by entering Ctrl+C". Nothing is said about what to
# do if Sauce Connect doesn't exit on it own or about non-interactive usage.
#
# So we do our best to be well-behaved without assuming that Sauce Connect
# always is: send it the same signal that it would get if an interactive user
# hit ctrl-c, wait a while for it to exit, then give up so that the CI task
# doesn't keep running indefinitely.

if ! pkill -INT '^sc$'; then
  echo "sc does not appear to be running" 1>&2
  exit 1
fi

# Wait up to 2 minutes, then give up if it's still running
n=0
while [ $n -lt 120 ] && pgrep '^sc$' > /dev/null; do
	sleep 1
	pkill -INT '^sc$' || true
	n=$(($n + 1))
done

if pgrep '^sc$' > /dev/null; then
	echo "Could not shut down Sauce Connect"
	exit 1
fi
