#!/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
INSTALL_DIR="$HOME/.lubis"

for arg in "$@"; do
  case $arg in
    -g|--global) INSTALL_DIR="/opt/lubis" ;;
    -l|--local) INSTALL_DIR="$HOME/.lubis" ;;
    --dir=*) INSTALL_DIR="${arg#*=}" ;;
    --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

if [ -n "$INSTALL_DIR" ]; then
  case "$INSTALL_DIR" in
    /*) ;;
    *) INSTALL_DIR="$PWD/$INSTALL_DIR" ;;
  esac
fi

# 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
BINARY_DIR="$INSTALL_DIR/bin"

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

TEMP_ARCHIVE="/tmp/lubisup_archive_$$.tar.gz"
LOCAL_FILE="$BINARY_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 "$BINARY_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 [ -z "$HOME" ]; then
  IN_HOME=false
else
  case "$INSTALL_DIR" in
    "$HOME"/*) IN_HOME=true ;;
    *) IN_HOME=false ;;
  esac
fi

if [ "$IN_HOME" = false ]; then
  if [ -d "/etc/profile.d" ] && [ -w "/etc/profile.d" ]; then
      echo "export PATH=\"\$PATH:$BINARY_DIR\"" > "/etc/profile.d/lubis.sh"
      echo "alias lubup=lubisup" >> "/etc/profile.d/lubis.sh"
      echo "Added $BINARY_DIR to /etc/profile.d/lubis.sh"
      echo "Restart your terminal to reload PATH."
  else
      echo "Warning: Could not write /etc/profile.d. Add $BINARY_DIR to PATH manually."
  fi
else
  # Home install: update the user's shell rc.
  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 "$BINARY_DIR" "$PROFILE_FILE" 2>/dev/null; then
    echo "# LUBIS installer ---------------------------" >> "$PROFILE_FILE"
    echo "export PATH=\"$BINARY_DIR:\$PATH\"" >> "$PROFILE_FILE"
    echo "alias lubup=lubisup" >> "$PROFILE_FILE"
    echo "# LUBIS installer ---------------------------" >> "$PROFILE_FILE"
    echo "Added $BINARY_DIR to $PROFILE_FILE"
    echo "Run 'source $PROFILE_FILE' to use it."
  fi
fi

echo "Installation complete."