#!/bin/bash

# omarchy:summary=Upload logs to logs.omarchy.org
# omarchy:args=<log-file>
# omarchy:hidden=true

LOG_TYPE="${1:-install}"
TEMP_LOG="/tmp/upload-log.txt"
SYSTEM_INFO="/tmp/system-info.txt"

# Get system information if fastfetch is available
if omarchy-cmd-present fastfetch; then
  {
    echo "========================================="
    echo "SYSTEM INFORMATION"
    echo "========================================="
    # Use fastfetch with no logo to get clean output
    fastfetch --logo none --pipe 2>/dev/null || echo "Failed to get system info"
    echo ""
    echo "========================================="
    echo "LOG CONTENT"
    echo "========================================="
    echo ""
  } >"$SYSTEM_INFO"
else
  # Fallback to basic info if fastfetch isn't available
  {
    echo "========================================="
    echo "SYSTEM INFORMATION"
    echo "========================================="
    echo "Hostname: $(uname -n)"
    echo "Kernel: $(uname -r)"
    echo "Date: $(date)"
    echo ""
    echo "========================================="
    echo "LOG CONTENT"
    echo "========================================="
    echo ""
  } >"$SYSTEM_INFO"
fi

same_or_prefix_log() {
  local a="$1" b="$2" size_a size_b min_size
  [[ -s $a && -s $b ]] || return 1
  cmp -s "$a" "$b" && return 0

  size_a=$(stat -c '%s' "$a" 2>/dev/null) || return 1
  size_b=$(stat -c '%s' "$b" 2>/dev/null) || return 1
  if (( size_a < size_b )); then
    min_size=$size_a
  else
    min_size=$size_b
  fi
  (( min_size > 0 )) || return 1

  cmp -n "$min_size" "$a" "$b" >/dev/null 2>&1
}

case "$LOG_TYPE" in
install)
  # In the live ISO before reboot, the target system is mounted at /mnt so the
  # omarchy install log lives at /mnt/var/log/omarchy-install.log. On the
  # installed system it's at /var/log/omarchy-install.log. Prefer whichever
  # exists, with /mnt taking precedence (only present in the ISO context, and
  # there it's the right one).
  ARCHINSTALL_LOG="/var/log/archinstall/install.log"
  LIVE_OMARCHY_LOG="/var/log/omarchy-install.log"
  TARGET_OMARCHY_LOG="/mnt/var/log/omarchy-install.log"

  cat "$SYSTEM_INFO" >"$TEMP_LOG"
  if [[ -s $ARCHINSTALL_LOG ]]; then
    printf '\n========================================\nARCHINSTALL SUBSYSTEM LOG (%s)\n========================================\n\n' "$ARCHINSTALL_LOG" >>"$TEMP_LOG"
    cat "$ARCHINSTALL_LOG" >>"$TEMP_LOG"
  fi

  if [[ -s $LIVE_OMARCHY_LOG ]]; then
    printf '\n========================================\nOMARCHY INSTALL LOG (%s)\n========================================\n\n' "$LIVE_OMARCHY_LOG" >>"$TEMP_LOG"
    cat "$LIVE_OMARCHY_LOG" >>"$TEMP_LOG"
  fi

  if [[ -s $TARGET_OMARCHY_LOG ]] && ! same_or_prefix_log "$LIVE_OMARCHY_LOG" "$TARGET_OMARCHY_LOG"; then
    printf '\n========================================\nTARGET OMARCHY INSTALL LOG (%s)\n========================================\n\n' "$TARGET_OMARCHY_LOG" >>"$TEMP_LOG"
    cat "$TARGET_OMARCHY_LOG" >>"$TEMP_LOG"
  fi

  if [[ ! -s $LIVE_OMARCHY_LOG && ! -s $TARGET_OMARCHY_LOG ]]; then
    echo "Warning: omarchy install log not found (looked at /var/log and /mnt/var/log)" >&2
  fi

  if [[ ! -s $TEMP_LOG ]]; then
    echo "Error: No install logs found"
    exit 1
  fi

  echo "Uploading installation log to logs.omarchy.org..."
  ;;

this-boot)
  # Combine system info with boot logs
  cat "$SYSTEM_INFO" >"$TEMP_LOG"
  journalctl -b 0 >>"$TEMP_LOG" 2>/dev/null

  if [[ ! -s $TEMP_LOG ]]; then
    echo "Error: No logs found for current boot"
    exit 1
  fi

  echo "Uploading current boot logs to logs.omarchy.org..."
  ;;

last-boot)
  # Combine system info with previous boot logs
  cat "$SYSTEM_INFO" >"$TEMP_LOG"
  journalctl -b -1 >>"$TEMP_LOG" 2>/dev/null

  if [[ ! -s $TEMP_LOG ]]; then
    echo "Error: No logs found for previous boot"
    exit 1
  fi

  echo "Uploading previous boot logs to logs.omarchy.org..."
  ;;

installed|system-info)
  # System info plus all installed packages
  cat "$SYSTEM_INFO" >"$TEMP_LOG"
  {
    echo ""
    echo "========================================="
    echo "INSTALLED PACKAGES (pacman -Q)"
    echo "========================================="
    pacman -Q 2>/dev/null || echo "Failed to get package list"
  } >>"$TEMP_LOG"

  if [[ ! -s $TEMP_LOG ]]; then
    echo "Error: Failed to gather system information"
    exit 1
  fi

  echo "Uploading system information to logs.omarchy.org..."
  ;;

*)
  echo "Usage: $0 [install|this-boot|last-boot|installed|system-info]"
  echo "  install      - Upload installation logs (default)"
  echo "  this-boot    - Upload logs from current boot"
  echo "  last-boot    - Upload logs from previous boot"
  echo "  installed    - Upload system info and installed packages"
  exit 1
  ;;
esac

echo ""

URL=$(curl -sf -F "file=@$TEMP_LOG" -Fexpires=24 https://logs.omarchy.org/)

if (( $? == 0 )) && [[ -n $URL ]]; then
  echo "✓ Log uploaded successfully!"
  echo "Share this URL:"
  echo ""
  echo "  $URL"
else
  echo "Error: Failed to upload log file"
  exit 1
fi
