#!/usr/bin/env bash

# omarchy:summary=Install the Solana CLI and Anchor
# omarchy:requires-sudo=true

# Based: Solana through asdf, from the plugin Based maintains, on top of asdf's
# Rust, since Anchor builds with cargo: the version manager avm and its latest
# Anchor, except on aarch64 where avm has no build and anchor-cli builds
# directly.

set -eo pipefail

asdf_data=${ASDF_DATA_DIR:-$HOME/.asdf}
omarchy-pkg-add asdf
export PATH="$asdf_data/shims:$PATH"

omarchy-pkg-add build-essential linux=only libudev-dev linux=only libusb-1.0-0-dev linux=only libhidapi-dev linux=only

echo -e "Installing Rust...\n"
omarchy-dev-env-tool rust

echo -e "\nInstalling Solana...\n"
omarchy-dev-env-tool solana

echo -e "\nInstalling Anchor...\n"
if [[ $(uname -m) == aarch64 ]]; then
  cargo install --git https://github.com/solana-foundation/anchor anchor-cli --force
  asdf reshim rust
else
  cargo install --git https://github.com/solana-foundation/anchor avm --force
  asdf reshim rust
  avm install latest
  avm use latest
fi
