#!/bin/bash

OS=$(uname -s)
ARCH=$(uname -m)
BASE_URL="https://releases.levv.io"
UNSUPPORTED_MSG="We currently don't support your architecture $OS/$ARCH, stay tuned!"

case "$OS" in
    Linux)
        case "$ARCH" in
            x86_64)
                FILE="levv-cli-linux-amd64.tar.gz"
                ;;
            arm64|aarch64)
                FILE="levv-cli-linux-arm64.tar.gz"
                ;;
            *)
		echo $UNSUPPORTED_MSG
                exit 1
                ;;
        esac
        ;;
    Darwin)
        case "$ARCH" in
	    x86_64)
		FILE="levv-cli-darwin-amd64.tar.gz"
		;;
	    arm64|aarch64)
		FILE="levv-cli-darwin-arm64.tar.gz"
		;;
	    *)
		echo $UNSUPPORTED_MSG
		exit 1
		;;
	esac
	;;

    *)
	echo $UNSUPPORTED_MSG
        exit 1
        ;;
esac

DOWNLOAD_URL="$BASE_URL/$FILE"
echo "Downloading $FILE from $DOWNLOAD_URL..."
curl -O "$DOWNLOAD_URL"

if [ $EUID -gt 0 ]; then
        INSTALL_PATH="$HOME/.local/bin"
        echo "You don't have root privileges. Installing to $INSTALL_PATH..."
else
        INSTALL_PATH="/usr/local/bin"
        echo "Root privileges detected. Installing to $INSTALL_PATH..."
fi

mkdir -p "$INSTALL_PATH"
tar -xzf "$FILE" -C "$INSTALL_PATH"

echo "Installation completed. The CLI is now available at $INSTALL_PATH."

if [ "$INSTALL_PATH" == "$HOME/.local/bin" ]; then
    echo "Make sure $HOME/.local/bin is in your PATH by adding the following line to your ~/.bashrc or ~/.zshrc:"
    echo 'export PATH="$HOME/.local/bin:$PATH"'
fi
