#!/bin/bash
# Hostess CLI installer
# Usage: curl -LsSf https://hostess.sh/install.sh | bash
set -euo pipefail

REPO="howl-cloud/hostess-cli"
INSTALL_DIR="${HOSTESS_INSTALL_DIR:-$HOME/.local/bin}"

# Detect OS
case "$(uname -s)" in
  Linux*)  OS="linux";;
  Darwin*) OS="darwin";;
  *)       echo "Unsupported OS: $(uname -s)"; exit 1;;
esac

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

BINARY="hostess-${OS}-${ARCH}"

# Get latest release tag
echo "Fetching latest release..."
TAG=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | grep '"tag_name"' | cut -d'"' -f4)
if [ -z "$TAG" ]; then
  echo "Failed to fetch latest release"
  exit 1
fi

echo "Installing Hostess CLI ${TAG} (${OS}/${ARCH})..."

# Download binary
URL="https://github.com/${REPO}/releases/download/${TAG}/${BINARY}"
TMP=$(mktemp)
trap 'rm -f "$TMP"' EXIT

curl -fsSL "$URL" -o "$TMP"
chmod +x "$TMP"

# Install
mkdir -p "$INSTALL_DIR"
mv "$TMP" "$INSTALL_DIR/hostess"

echo ""
echo "Hostess CLI ${TAG} installed to ${INSTALL_DIR}/hostess"

# Check if install dir is in PATH
if ! echo "$PATH" | tr ':' '\n' | grep -qx "$INSTALL_DIR"; then
  echo ""
  echo "Add this to your shell profile to put hostess on your PATH:"
  echo ""
  echo "  export PATH=\"${INSTALL_DIR}:\$PATH\""
fi

echo ""
echo "Run 'hostess --version' to verify."
