#!/bin/bash
set -euo pipefail

# Bootstrap cargo into PVC-backed home if not already present
if [ ! -d "$HOME/.cargo/bin" ]; then
    echo "[init-home] First run: linking cargo toolchain..."
    mkdir -p "$HOME/.cargo/bin"
    # Copy cargo config and link binaries from shared rustup
    for bin in /opt/cargo/bin/*; do
        ln -sf "$bin" "$HOME/.cargo/bin/$(basename "$bin")"
    done
    echo "[init-home] Cargo toolchain linked."
fi

# Ensure sccache cache dir exists
mkdir -p "${SCCACHE_DIR:-$HOME/.cache/sccache}"

# Default .zshrc if none exists (oh-my-zsh is in the image, config in PVC)
if [ ! -f "$HOME/.zshrc" ]; then
    cp /home/dev/.oh-my-zsh/templates/zshrc.zsh-template "$HOME/.zshrc" 2>/dev/null || true
    echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> "$HOME/.zshrc"
    echo 'export SQLX_OFFLINE=true' >> "$HOME/.zshrc"
fi

echo "[init-home] Ready."
