# echo 'Simple connection to prove that it works'
ssh "$1" true || exit 1

# echo 'Set up the master connection in the background'
ssh -S /tmp/ssh-control-connection-%r@%h:%p -fgMNn "$1"

# echo 'Sleep to give it a chance to finish'
sleep 2

# echo 'Confirm the master connection is running'
ssh -S /tmp/ssh-control-connection-%r@%h:%p -O check "$1"

# echo 'Start a long-running process that should use the master connection'
ssh -S /tmp/ssh-control-connection-%r@%h:%p -n "$1" bash -c 'echo starting; sleep 60; echo finishing' &

# echo 'Sleep to give it a chance to connect properly'
sleep 2

# echo 'Terminate the master connection, which should terminate the long-running task too'
ssh -S /tmp/ssh-control-connection-%r@%h:%p -O exit "$1"

# echo 'Waiting for everything to finish'
wait
