#!/bin/bash

INSTALL_DIR="/usr/local/bin"
BINARY_NAME="sitemapper"
BASE_URL="https://git.nixc.us/Nixius/sitemapper/raw/branch/main/dist"

declare -A binaries
binaries["linux/amd64"]="sitemapper"
binaries["linux/arm64"]="sitemapper_linux_arm64"
binaries["linux/arm/v7"]="sitemapper_linux_arm"
binaries["darwin/amd64"]="sitemapper_darwin_amd64"
binaries["darwin/arm64"]="sitemapper_darwin_arm64"

OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"

case $ARCH in
    x86_64) ARCH="amd64" ;;
    arm64 | aarch64) ARCH="arm64" ;;
    arm*) ARCH="arm/v7" ;;
    *) echo "Unsupported architecture: $ARCH"; exit 1 ;;
esac

KEY="${OS}/${ARCH}"

if [[ -z "${binaries[$KEY]}" ]]; then
    echo "No pre-built binary for your system architecture ($KEY)."
    exit 1
fi

BINARY_URL="${BASE_URL}/${binaries[$KEY]}"

echo "Downloading and installing $BINARY_NAME from $BINARY_URL..."
sudo curl -sSL "$BINARY_URL" -o "${INSTALL_DIR}/${BINARY_NAME}"

sudo chmod +x "${INSTALL_DIR}/${BINARY_NAME}"
echo "Installed $BINARY_NAME to $INSTALL_DIR"
