#!/usr/bin/env bash
# Run this inside a Toolbox container to get the K3s tools to work.
#
# IMPORTANT: If you try to run K3s or kubectl before running this script, it will
# try to set up a default installation, which makes a bit of a mess :-/

# initialize
set -euo pipefail
[ $(id -u) -eq 0 ] || exec sudo "$0" "$@"

# map the K3s data directories
ln -s /run/host/var/lib/rancher /var/lib/rancher
ln -s /run/host/var/lib/kubelet /var/lib/kubelet
ln -s /run/host/run/k3s /run/k3s

# install the K3s config
mkdir -p "/home/$SUDO_USER/.kube"
ln -s /run/host/etc/rancher/k3s/k3s.yaml "/home/$SUDO_USER/.kube/config"

# make the host's binaries available
mkdir -p "/home/$SUDO_USER/.bashrc.d"
echo "export PATH=$PATH:/run/host/usr/local/bin/" >"/home/$SUDO_USER/.bashrc.d/add-host-path"

echo "Exit and re-enter the Toolbox container to activate the K3s tools."
