#!/usr/bin/bash
# [MISE] hide=true
# [MISE] sources=["Cargo.toml", "Cargo.lock"]
# [MISE] tools={"cargo-binstall"="latest"}

set -o errexit
set -o pipefail

SUDO=''
if (( $EUID != 0 )); then
    SUDO='sudo'
fi

update_needed="yes"
function install_if_needed {
  local bin=${1?}
  local pkg=${2-${bin}}
  if ! command -v "${bin}"; then
    echo "Needs to install ${pkg} ..."
    if [[ -n "${update_needed}" ]]; then
      ${SUDO} apt-get --yes update
      update_needed=""
    fi
    ${SUDO} apt-get --yes install "${pkg}"
  fi
}

install_if_needed arm-linux-gnueabihf-gcc gcc-arm-linux-gnueabihf
install_if_needed aarch64-linux-gnu-gcc gcc-aarch64-linux-gnu
install_if_needed cmake
install_if_needed musl-gcc musl-tools

rustup update
rustup target add armv7-unknown-linux-gnueabihf x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu
rustup component add clippy
rustup component add rustfmt
cargo binstall --no-confirm --no-cleanup cargo-nextest
