Make downloading work on a stock buster installation

Previously it was relying on an old version of libssl being installed.

Change-Id: I9d2bb58dc96d0e7a403c7602ab7e8e034e65de91
diff --git a/WORKSPACE b/WORKSPACE
index 1385ad7..5f29700 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -249,11 +249,12 @@
     url = "http://www.frc971.org/Build-Dependencies/rsync.tar.gz",
 )
 
+# //debian:ssh
 http_archive(
     name = "ssh",
     build_file = "@//debian:ssh.BUILD",
-    sha256 = "43c760bc5c32d5a8c24837d1b4c356424a157f59ff8a08654651856ae90b16d2",
-    url = "http://www.frc971.org/Build-Dependencies/ssh.tar.gz",
+    sha256 = "470fdc1252a2133a9d3c3da778e892a5b88f04f402cb04d8eb1cff7853242034",
+    url = "http://www.frc971.org/Build-Dependencies/ssh_v3.tar.gz",
 )
 
 http_archive(
diff --git a/debian/BUILD b/debian/BUILD
index 9b7ad4d..5c9d814 100644
--- a/debian/BUILD
+++ b/debian/BUILD
@@ -243,7 +243,7 @@
 )
 
 generate_deb_tarball(
-    name = "ssh",
+    name = "ssh_v3",
     files = ssh_debs,
 )
 
@@ -281,3 +281,7 @@
     name = "python_gtk",
     files = python_gtk_debs,
 )
+
+exports_files([
+    "ssh_wrapper.sh",
+])
diff --git a/debian/ssh.BUILD b/debian/ssh.BUILD
index fb7f120..f26d19a 100644
--- a/debian/ssh.BUILD
+++ b/debian/ssh.BUILD
@@ -1,11 +1,31 @@
-filegroup(
-    name = "ssh",
-    srcs = ["usr/bin/ssh"],
+_tools = [
+    "ssh",
+    "scp",
+]
+
+[genrule(
+    name = "copy_%s_wrapper" % tool,
+    srcs = ["@//debian:ssh_wrapper.sh"],
+    outs = ["%s_wrapper.sh" % tool],
+    cmd = "cat $< | sed 's,%%(TOOL),usr/bin/%s,g' > $@" % tool,
+) for tool in _tools]
+
+[sh_binary(
+    name = tool,
+    srcs = [
+        "%s_wrapper.sh" % tool,
+    ],
+    data = [
+        "usr/bin/%s" % tool,
+        ":libs",
+        "@bazel_tools//tools/bash/runfiles",
+    ],
     visibility = ["//visibility:public"],
-)
+) for tool in _tools]
 
 filegroup(
-    name = "scp",
-    srcs = ["usr/bin/scp"],
-    visibility = ["//visibility:public"],
+    name = "libs",
+    srcs = glob([
+        "usr/lib/x86_64-linux-gnu/**",
+    ]),
 )
diff --git a/debian/ssh.bzl b/debian/ssh.bzl
index 005768c..77b913c 100644
--- a/debian/ssh.bzl
+++ b/debian/ssh.bzl
@@ -1,3 +1,4 @@
 files = {
-    "openssh-client_6.7p1-5+deb8u8_amd64.deb": "50bf902cc680fd1442556325e47d892f24621d7f0c4baf826f298d737a1e8030",
+    "openssh-client_7.9p1-10+deb10u1_amd64.deb": "1c30bcaf37dafe198783cf691096fe557e8eacbc9435631f51af62b3f705ee12",
+    "libssl1.1_1.1.1d-0+deb10u2_amd64.deb": "31c15130e0e4b2c907ef7cd92e50be23320a22c0c3b54e130b5258fe6bd8df2d",
 }
diff --git a/debian/ssh_wrapper.sh b/debian/ssh_wrapper.sh
new file mode 100755
index 0000000..687ed87
--- /dev/null
+++ b/debian/ssh_wrapper.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+
+# --- begin runfiles.bash initialization v2 ---
+# Copy-pasted from the Bazel Bash runfiles library v2.
+set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
+source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
+  source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
+  source "$0.runfiles/$f" 2>/dev/null || \
+  source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+  source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+  { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
+# --- end runfiles.bash initialization v2 ---
+
+LIB_PATH="$(rlocation ssh/usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0)"
+LIB_PATH="${LIB_PATH%/libcrypto.so.1.0.0}"
+export LD_LIBRARY_PATH="${LIB_PATH}"
+
+TOOL_PATH="$(rlocation ssh/%(TOOL))"
+
+exec "${TOOL_PATH}" "$@"
diff --git a/frc971/downloader/downloader.py b/frc971/downloader/downloader.py
index e49ee5d..e4826cf 100644
--- a/frc971/downloader/downloader.py
+++ b/frc971/downloader/downloader.py
@@ -10,22 +10,22 @@
 import os
 
 
-def install(ssh_target, pkg):
+def install(ssh_target, pkg, ssh_path, scp_path):
     """Installs a package from NI on the ssh target."""
     print("Installing", pkg)
     PKG_URL = "http://download.ni.com/ni-linux-rt/feeds/2015/arm/ipk/cortexa9-vfpv3/" + pkg
     subprocess.check_call(["wget", PKG_URL, "-O", pkg])
     try:
         subprocess.check_call([
-            "external/ssh/usr/bin/scp", "-S", "external/ssh/usr/bin/ssh", pkg,
+            scp_path, "-S", ssh_path, pkg,
             ssh_target + ":/tmp/" + pkg
         ])
         subprocess.check_call([
-            "external/ssh/usr/bin/ssh", ssh_target, "opkg", "install",
+            ssh_path, ssh_target, "opkg", "install",
             "/tmp/" + pkg
         ])
         subprocess.check_call(
-            ["external/ssh/usr/bin/ssh", ssh_target, "rm", "/tmp/" + pkg])
+            [ssh_path, ssh_target, "rm", "/tmp/" + pkg])
     finally:
         subprocess.check_call(["rm", pkg])
 
@@ -65,25 +65,31 @@
 
     ssh_target = "%s@%s" % (user, hostname)
 
+    ssh_path = "external/ssh/ssh"
+    scp_path = "external/ssh/scp"
+
     rsync_cmd = ([
-        "external/rsync/usr/bin/rsync", "-e", "external/ssh/usr/bin/ssh", "-c",
+        "external/rsync/usr/bin/rsync", "-e", ssh_path, "-c",
         "-v", "-z", "--copy-links"
     ] + srcs + ["%s:%s/%s" % (ssh_target, target_dir, relative_dir)])
     try:
         subprocess.check_call(rsync_cmd)
     except subprocess.CalledProcessError as e:
-        if e.returncode == 127:
+        if e.returncode == 127 or e.returncode == 12:
             print("Unconfigured roboRIO, installing rsync.")
-            install(ssh_target, "libattr1_2.4.47-r0.36_cortexa9-vfpv3.ipk")
-            install(ssh_target, "libacl1_2.2.52-r0.36_cortexa9-vfpv3.ipk")
-            install(ssh_target, "rsync_3.1.0-r0.7_cortexa9-vfpv3.ipk")
+            install(ssh_target, "libattr1_2.4.47-r0.36_cortexa9-vfpv3.ipk",
+                    ssh_path, scp_path)
+            install(ssh_target, "libacl1_2.2.52-r0.36_cortexa9-vfpv3.ipk",
+                    ssh_path, scp_path)
+            install(ssh_target, "rsync_3.1.0-r0.7_cortexa9-vfpv3.ipk",
+                    ssh_path, scp_path)
             subprocess.check_call(rsync_cmd)
         else:
             raise e
 
     if not recursive:
         subprocess.check_call(
-            ("external/ssh/usr/bin/ssh", ssh_target, "&&".join([
+            (ssh_path, ssh_target, "&&".join([
                 "chmod u+s %s/starter_exe" % target_dir,
                 "echo \'Done moving new executables into place\'",
                 "bash -c \'sync && sync && sync\'",