#!/bin/bash
# get_sapphire_ip.sh — Copy Sapphire SoC IP files from your Efinity installation.
#
# Run this BEFORE opening church_soc_cm.xml in Efinity or running make bitstream.
# sapphire.v and sapphire_define.vh are generated by the Efinix Sapphire SoC IP
# wizard and cannot be distributed with this package.
#
# Usage:
#   bash get_sapphire_ip.sh                    # auto-detect Efinity install
#   bash get_sapphire_ip.sh ~/efinity/2026.1   # specify Efinity install dir
#
# What it does:
#   Searches common Efinity install locations for sapphire.v, copies it and
#   sapphire_define.vh to the current directory alongside church_soc_cm.xml.

set -euo pipefail

DEST="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

#
# Locate Efinity root
#
if [ $# -ge 1 ]; then
    EFINITY="$1"
else
    # Try common install locations
    for candidate in \
        "$HOME/efinity/2026.1" \
        "$HOME/efinity/2026.2" \
        "$HOME/efinity/2025.2" \
        "$HOME/efinity" \
        "/opt/efinity/2026.1" \
        "/opt/efinity/2025.2" \
        "/opt/efinity" \
        "/usr/local/efinity"
    do
        if [ -d "$candidate" ]; then
            EFINITY="$candidate"
            break
        fi
    done
fi

if [ -z "${EFINITY:-}" ] || [ ! -d "${EFINITY:-}" ]; then
    echo ""
    echo "ERROR: Could not find Efinity installation directory."
    echo ""
    echo "Please specify it explicitly:"
    echo "  bash get_sapphire_ip.sh ~/efinity/2026.1"
    echo ""
    echo "Or find it with:"
    echo "  find ~ /opt /usr/local -maxdepth 4 -name 'efx_map' 2>/dev/null"
    exit 1
fi

echo "==> Efinity root: $EFINITY"

#
# Find sapphire.v under the Efinity install
#
SAPPHIRE_V="$(find "$EFINITY" -name "sapphire.v" 2>/dev/null | grep -i "Ti60F225\|ti60f225\|sapphire/sapphire.v" | head -1)"
if [ -z "$SAPPHIRE_V" ]; then
    SAPPHIRE_V="$(find "$EFINITY" -name "sapphire.v" 2>/dev/null | head -1)"
fi

if [ -z "$SAPPHIRE_V" ]; then
    echo ""
    echo "ERROR: sapphire.v not found under $EFINITY"
    echo ""
    echo "Make sure you have installed the Sapphire SoC IP via the Efinity IP Manager:"
    echo "  Efinity IDE → IP Manager → Sapphire RISC-V SoC → Install"
    echo ""
    echo "Then re-run: bash get_sapphire_ip.sh"
    exit 1
fi

SAPPHIRE_DIR="$(dirname "$SAPPHIRE_V")"
SAPPHIRE_DEFINE="$SAPPHIRE_DIR/sapphire_define.vh"

if [ ! -f "$SAPPHIRE_DEFINE" ]; then
    # Try adjacent .vh file
    SAPPHIRE_DEFINE="$(find "$EFINITY" -name "sapphire_define.vh" 2>/dev/null | head -1)"
fi

if [ -z "$SAPPHIRE_DEFINE" ] || [ ! -f "$SAPPHIRE_DEFINE" ]; then
    echo ""
    echo "ERROR: sapphire_define.vh not found under $EFINITY"
    echo "Found sapphire.v at: $SAPPHIRE_V"
    echo "But sapphire_define.vh was not found alongside it."
    echo ""
    echo "Please check your Efinity IP installation."
    exit 1
fi

echo "==> Found: $SAPPHIRE_V"
echo "==> Found: $SAPPHIRE_DEFINE"

cp "$SAPPHIRE_V"      "$DEST/sapphire.v"
cp "$SAPPHIRE_DEFINE" "$DEST/sapphire_define.vh"

echo ""
echo "==> Copied to:"
echo "    $DEST/sapphire.v"
echo "    $DEST/sapphire_define.vh"
echo ""
echo "You can now run:  make bitstream"
echo "Or open:          church_soc_cm.xml  in Efinity IDE"
echo ""
echo "NOTE: After running 'make bitstream' (or step 4 of the manual build),"
echo "patch_sapphire_init.py will embed the compiled firmware into sapphire.v."
echo "Re-run this script only if you need to reset sapphire.v to the original."
