#!/bin/bash

INSTALL_DIR="/usr/local/bin"
BINARY_NAME="go-glitch"
BASE_URL="https://git.nixc.us/Nixius/go-glitch/raw/branch/master/dist"


# Supported architectures
ARCHITECTURES=("linux/amd64" "linux/arm64" "linux/arm/v7" "darwin/amd64" "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

BINARY_URL="${BASE_URL}/${BINARY_NAME}_${OS}_${ARCH}"

echo "Downloading and installing $BINARY_NAME from $BINARY_URL..."

# Check if we have write permission to the install directory
if [ -w "${INSTALL_DIR}" ]; then
    curl -sSL "$BINARY_URL" -o "${INSTALL_DIR}/${BINARY_NAME}"
    chmod +x "${INSTALL_DIR}/${BINARY_NAME}"
else
    sudo curl -sSL "$BINARY_URL" -o "${INSTALL_DIR}/${BINARY_NAME}"
    sudo chmod +x "${INSTALL_DIR}/${BINARY_NAME}"
fi

echo "Installed $BINARY_NAME to $INSTALL_DIR"
