#!/bin/sh
set -e

API_BASE_URL="https://lux-turris-api.lubis-eda.com"

# 1. Detect Architecture
ARCH="$(uname -m)"
case "$ARCH" in
    x86_64) ARCH="amd64" ;;
    aarch64|arm64) ARCH="arm64" ;;
    *) echo "Error: Unsupported architecture: $ARCH"; exit 1 ;;
esac

OS_TYPE="linux"
if [ -f /etc/os-release ]; then
    DISTRO=$(grep ^ID= /etc/os-release | cut -d= -f2 | tr -d '"')
else
    DISTRO="linux"
fi

# 3. Handle Arguments
GLOBAL_INSTALL=false

for arg in "$@"; do
  case $arg in
    -g|--global) GLOBAL_INSTALL=true ;;
    --platform=*) 
        PLATFORM_VAL="${arg#*=}"
        OS_TYPE=$(echo "$PLATFORM_VAL" | cut -d'-' -f1)
        ARCH=$(echo "$PLATFORM_VAL" | cut -d'-' -f2)
        DISTRO=$(echo "$PLATFORM_VAL" | cut -d'-' -f3)
        ;;
    --api-url=*)
      API_BASE_URL="${arg#*=}"
      ;;
  esac
done

# 3. Prompt for the Access Token
printf "Please enter your Lux Turris Access Token: "
stty -echo < /dev/tty
read -r API_TOKEN < /dev/tty
stty echo < /dev/tty
printf "\n"

if [ -z "$API_TOKEN" ]; then
    echo "Error: Token cannot be empty!"
    exit 1
fi

# 4. Construct URL
DOWNLOAD_URL="$API_BASE_URL/tools/installer/download?platform=$OS_TYPE-$ARCH-$DISTRO"

# 5. Prepare Directory
if [ "$GLOBAL_INSTALL" = true ]; then
  INSTALL_DIR="/opt/lubis/bin"
else
  INSTALL_DIR="$HOME/.lubis/bin"
fi

if ! mkdir -p "$INSTALL_DIR" 2>/dev/null; then
    echo "Error: Cannot create $INSTALL_DIR (Permission denied)"
    exit 1
fi

TEMP_ARCHIVE="/tmp/lubisup_archive_$$.tar.gz"
LOCAL_FILE="$INSTALL_DIR/lubisup"

# 6. Download
echo "Downloading lubisup for $OS_TYPE-$ARCH-$DISTRO..."
if ! curl -f -L -H "Authorization: Bearer $API_TOKEN" "$DOWNLOAD_URL" -o "$TEMP_ARCHIVE"; then
    echo "Error: Download failed from $API_BASE_URL"
    rm -f "$TEMP_ARCHIVE"
    exit 1
fi
echo "Extracting archive..."
if ! tar -xzf "$TEMP_ARCHIVE" -C "$INSTALL_DIR"; then
    echo "Error: Failed to extract archive"
    rm -f "$TEMP_ARCHIVE"
    exit 1
fi
rm -f "$TEMP_ARCHIVE"
chmod +x "$LOCAL_FILE"

# 7. Update PATH
if [ "$GLOBAL_INSTALL" = true ]; then
  if [ -d "/etc/profile.d" ] && [ -w "/etc/profile.d" ]; then
      echo "export PATH=\"\$PATH:$INSTALL_DIR\"" > "/etc/profile.d/lubis.sh"
      echo "alias lubup=lubisup" >> "/etc/profile.d/lubis.sh"
  else
      echo "Warning: Could not auto-update global PATH. Add $INSTALL_DIR manually."
  fi
else
  # Local PATH logic
  PROFILE_FILE="$HOME/.profile"
  if [ -f "$HOME/.zshrc" ]; then PROFILE_FILE="$HOME/.zshrc"; 
  elif [ -f "$HOME/.bashrc" ]; then PROFILE_FILE="$HOME/.bashrc"; fi
  
  if ! grep -q "$INSTALL_DIR" "$PROFILE_FILE" 2>/dev/null; then
    echo "# LUBIS installer ---------------------------" >> "$PROFILE_FILE"
    echo "export PATH=\"$INSTALL_DIR:\$PATH\"" >> "$PROFILE_FILE"
    echo "alias lubup=lubisup" >> "$PROFILE_FILE"
    echo "# LUBIS installer ---------------------------" >> "$PROFILE_FILE"
    echo "Added $INSTALL_DIR to $PROFILE_FILE"
    echo "Run 'source $PROFILE_FILE' to use it."
  fi
fi

echo "Installation complete."