#!/bin/sh
set -e

VERSION="2.0.0"
DOWNLOAD_URL="http://47.75.127.45/releases/alphameta-lite.tar.gz"
INSTALL_DIR="${HOME}/.local/bin"
BINARY_PATH="${INSTALL_DIR}/__main__.dist/alphameta"

echo "AlphaMeta Installer v${VERSION}"
echo "============================"

if [ -f "$BINARY_PATH" ]; then
    echo "AlphaMeta is already installed at $BINARY_PATH"
    echo "To update, please remove the existing installation first."
    exit 0
fi

echo "Creating install directory..."
mkdir -p "$INSTALL_DIR"

echo "Downloading from $DOWNLOAD_URL ..."

if command -v curl >/dev/null 2>&1; then
    curl -#L "$DOWNLOAD_URL" -o /tmp/alphameta-lite.tar.gz
elif command -v wget >/dev/null 2>&1; then
    wget -v "$DOWNLOAD_URL" -O /tmp/alphameta-lite.tar.gz
else
    echo "Error: Neither curl nor wget found. Please install one of them."
    exit 1
fi

echo "Extracting..."
tar -xzf /tmp/alphameta-lite.tar.gz -C "$INSTALL_DIR"
rm /tmp/alphameta-lite.tar.gz

echo "Renaming binary to alphameta..."
mv "${INSTALL_DIR}/__main__.dist/__main__.bin" "${INSTALL_DIR}/__main__.dist/alphameta"

echo ""
echo "✅ Installed successfully!"
echo ""
echo "To run AlphaMeta, add to your PATH:"
echo "  export PATH=\"\$HOME/.local/bin:\$PATH\""
echo ""
echo "Or run directly:"
echo "  $BINARY_PATH"
