#!/usr/bin/env perl
# todo(2025-12-15): integrate tmux

if (! @ARGV) {
  die <<EOF
usage: goto [-a] [id] host
  -a - connect to any existing screen session
       (default is to only connect to sessions started by goto)
  id - name of screen session to connect to

This command ssh's to a host and runs a screen session. Will
auto-reconnect if the connection is dropped.
EOF
}

$ssh="mosh";

my @s=("-S", "goto") unless $ARGV[0] eq '-a';
my $t;
while (1) {
  $t=time;
  system($ssh, @ARGV, "--", "screen", "-DR", @s);
  my $delay=(time - $t > 2) ? 0 : 1;
  print "\n\n\nlost connection.. reconnecting in ${delay}s\n";
  sleep $delay;
}
