#!/bin/bash
set -euo pipefail

apt-get update
apt-get install -y curl

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash

export NVM_DIR="$HOME/.nvm"
# Source nvm with || true to handle nvm.sh's internal non-zero returns
\. "$NVM_DIR/nvm.sh" || true
# Verify NVM loaded successfully
command -v nvm &>/dev/null || { echo "Error: NVM failed to load" >&2; exit 1; }

nvm install 22
npm -v

{% if version %}
npm i -g opencode-ai@{{ version }}
{% else %}
npm i -g opencode-ai@latest
{% endif %}

opencode --version
