#!/bin/sh
# Installs the dbb1 CLI. Set DBB1_INSTALL_DIR to choose where (default: ~/.local/bin).
set -eu

base="https://github.com/dougbarrett/dbb1-apps/releases/latest/download"
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
  Darwin) os="apple-darwin" ;;
  Linux)  os="unknown-linux-musl" ;;
  *) echo "dbb1: unsupported OS $os (build from source: cargo install --git <repo> dbb1)" >&2; exit 1 ;;
esac
case "$arch" in
  arm64|aarch64) arch="aarch64" ;;
  x86_64|amd64)  arch="x86_64" ;;
  *) echo "dbb1: unsupported architecture $arch" >&2; exit 1 ;;
esac

target="$arch-$os"
dir="${DBB1_INSTALL_DIR:-$HOME/.local/bin}"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

echo "Downloading dbb1 for $target..."
curl -fsSL "$base/dbb1-$target.tar.gz" -o "$tmp/dbb1.tar.gz"
tar -xzf "$tmp/dbb1.tar.gz" -C "$tmp"
mkdir -p "$dir"
mv "$tmp/dbb1" "$dir/dbb1"
chmod +x "$dir/dbb1"

echo "Installed $dir/dbb1"
case ":$PATH:" in
  *":$dir:"*) ;;
  *) echo "Add it to your PATH:  export PATH=\"$dir:\$PATH\"" ;;
esac
echo "Next: dbb1 login"
