#!/bin/sh
set -eu
python3 -c 'import sys; sys.exit(0 if sys.version_info >= (3,9) else "sndbx needs Python 3.9 or newer.")'
command -v ssh >/dev/null
command -v ssh-keygen >/dev/null
bin_dir="${SNDBX_BIN_DIR:-$HOME/.local/bin}"
mkdir -p "$bin_dir"
tmp=$(mktemp "$bin_dir/.sndbx.XXXXXXXX")
trap 'rm -f "$tmp"' EXIT
curl --proto '=https' -fsSL 'https://sndbx.jupiter.beyond.ist/cli/releases/0.10.1/sndbx' -o "$tmp"
python3 -c 'import hashlib, pathlib, sys; sys.exit(0 if hashlib.sha256(pathlib.Path(sys.argv[1]).read_bytes()).hexdigest() == "14c50ffe097f3ca8dfbaa8b5245c3078a0c00d5ad871964451c1d018bc494419" else "Invalid sndbx checksum.")' "$tmp"
chmod 755 "$tmp"
"$tmp" --version
mv -f "$tmp" "$bin_dir/sndbox"
ln -s sndbox "$tmp"
mv -f "$tmp" "$bin_dir/sndbx"
echo "Installed $bin_dir/sndbox"
case ":$PATH:" in
    *":$bin_dir:"*) ;;
    *) echo "Add $bin_dir to your PATH." ;;
esac
