#!/bin/bash

# Configure these parameters
sleep=5
alive_interval=10
host=baggins.c.googlers.com

# Tunnel ADB so locally connected devices work on cloudtop.
SSH_OPTS="-YXC -R 5037:localhost:5037 -oServerAliveInterval=$alive_interval"

initial_cloudtop_command="gcertstatus || gcert; tmuxinator dev"

gcertstatus || gcert

# Just keep reconnecting upon failure
while [ 1 ]; do
  ssh $host -t $SSH_OPTS "$initial_cloudtop_command"

  # Don't reconnect if disconnection not due to error (i.e., user detached)
  if [ $? -eq 0 ]; then break; fi

  echo "SSH connection lost, reconnecting in $sleep seconds..."
  sleep $sleep
done
