scripts: Update riscv-toolchain install script to support CI

If the tarball is preinstalled (as in the CI docker image), bypass the
fetch and only extract the tarball the cache toolchain location.

Change-Id: I2eca45980671ef9435340c3567d99910665b56a4
diff --git a/install-toolchain.sh b/install-toolchain.sh
index 7997678..2347a46 100755
--- a/install-toolchain.sh
+++ b/install-toolchain.sh
@@ -17,8 +17,10 @@
 # Install the prebuilt RISC-V GCC/LLVM toolchain.
 
 function clean {
-  rm "${DOWNLOAD_DIR}/${TOOLCHAIN_TARBALL}" \
-     "${DOWNLOAD_DIR}/${TOOLCHAIN_TARBALL}.sha256sum"
+  if [[ ! -f "${PREINSTALL_DIR}/${TOOLCHAIN_TARBALL}" ]]; then
+    rm "${DOWNLOAD_DIR}/${TOOLCHAIN_TARBALL}" \
+       "${DOWNLOAD_DIR}/${TOOLCHAIN_TARBALL}.sha256sum"
+  fi
 }
 
 function die {
@@ -59,16 +61,26 @@
     ;;
 esac
 
-DOWNLOAD_URL="https://storage.googleapis.com/shodan-public-artifacts/${TOOLCHAIN_TARBALL}"
-DOWNLOAD_DIR="${OUT}/tmp"
-
 trap clean EXIT
 
-wget -P "${DOWNLOAD_DIR}" "${DOWNLOAD_URL}"
-wget -P "${DOWNLOAD_DIR}" "${DOWNLOAD_URL}.sha256sum"
-pushd "${DOWNLOAD_DIR}" > /dev/null
-try sha256sum -c "${TOOLCHAIN_TARBALL}.sha256sum"
-popd > /dev/null
+# Used by the CI shodan docker image
+PREINSTALL_DIR=/usr/src
+
+if [[ -f "${PREINSTALL_DIR}/${TOOLCHAIN_TARBALL}" ]]; then
+  echo "${TOOLCHAIN_TARBALL} stored in the machine at ${PREINSTALL_DIR}"
+  DOWNLOAD_DIR=/usr/src
+else
+  echo "Download ${TOOLCHAIN_TARBALL} from GCS..."
+  DOWNLOAD_URL="https://storage.googleapis.com/shodan-public-artifacts/${TOOLCHAIN_TARBALL}"
+  DOWNLOAD_DIR="${OUT}/tmp"
+  mkdir -p "${DOWNLOAD_DIR}"
+
+  wget -P "${DOWNLOAD_DIR}" "${DOWNLOAD_URL}"
+  wget -P "${DOWNLOAD_DIR}" "${DOWNLOAD_URL}.sha256sum"
+  pushd "${DOWNLOAD_DIR}" > /dev/null
+  try sha256sum -c "${TOOLCHAIN_TARBALL}.sha256sum"
+  popd > /dev/null
+fi
 
 try tar -C "${CACHE}" -xf "${DOWNLOAD_DIR}/${TOOLCHAIN_TARBALL}"