#!/bin/bash

# Namida Installation Script, Generated by claude.ai
# Automatically installs the latest release

set -e

INSTALL_DIR="/opt/namida"
TEMP_DIR="/tmp/namida-install-$"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# Check if running as root
if [ "$EUID" -ne 0 ]; then
  echo -e "${RED}Error: This script must be run as root (use sudo)${NC}"
  exit 1
fi

echo -e "${GREEN}Starting Namida installation...${NC}"

# Create temporary directory
mkdir -p "$TEMP_DIR"
cd "$TEMP_DIR"


# Get latest releases and download URL for .tar.gz
echo -e "${YELLOW}Fetching release information...${NC}"
API_URL="https://api.github.com/repos/namidaco/namida-snapshots/releases"

# Try to find a .tar.gz in the latest 4 releases
RELEASES_JSON=$(curl -s "$API_URL?per_page=4")
DOWNLOAD_URL=$(echo "$RELEASES_JSON" | grep "browser_download_url.*\.tar\.gz" | grep -v "_login" | cut -d '"' -f 4 | head -n 1)

if [ -z "$DOWNLOAD_URL" ]; then
  echo -e "${RED}Error: Could not find .tar.gz file in the last 4 releases${NC}"
  rm -rf "$TEMP_DIR"
  exit 1
fi

# Get the download URL for the .tar.gz file (latest release only)
# API_URL="https://api.github.com/repos/namidaco/namida-snapshots/releases/latest"
# DOWNLOAD_URL=$(curl -s "$API_URL" | grep "browser_download_url.*\.tar\.gz" | cut -d '"' -f 4)

if [ -z "$DOWNLOAD_URL" ]; then
  echo -e "${RED}Error: Could not find .tar.gz file in latest release${NC}"
  rm -rf "$TEMP_DIR"
  exit 1
fi

# Extract filename from URL
TARBALL=$(basename "$DOWNLOAD_URL")

echo -e "${GREEN}Found: $TARBALL${NC}"

# Download tarball
echo -e "${YELLOW}Downloading latest version...${NC}"
if ! curl -L -o "$TARBALL" "$DOWNLOAD_URL"; then
  echo -e "${RED}Error: Failed to download tarball${NC}"
  rm -rf "$TEMP_DIR"
  exit 1
fi

# Extract tarball
echo -e "${YELLOW}Extracting archive...${NC}"
tar -xzf "$TARBALL"

# Create installation directory
echo -e "${YELLOW}Installing to ${INSTALL_DIR}...${NC}"
mkdir -p "$INSTALL_DIR"

# Install namida binary
install -Dm755 "namida" "${INSTALL_DIR}/namida"
if [ -f "namida_bin" ]; then
  install -Dm755 "namida_bin" "${INSTALL_DIR}/namida_bin"
fi

# Install /bin directory
if [ -d "bin" ]; then
  install -dm755 "${INSTALL_DIR}/bin"
  for binary in bin/*; do
    if [ -f "$binary" ]; then
      install -m755 "$binary" "${INSTALL_DIR}/bin/"
    fi
  done
fi

# Install /lib directory
if [ -d "lib" ]; then
  install -dm755 "${INSTALL_DIR}/lib"
  for libfile in lib/*; do
    if [ -f "$libfile" ]; then
      install -m755 "$libfile" "${INSTALL_DIR}/lib/"
    elif [ -d "$libfile" ]; then
      subdir=$(basename "$libfile")
      install -dm755 "${INSTALL_DIR}/lib/${subdir}"
      for subfile in "$libfile"/*; do
        if [ -f "$subfile" ]; then
          install -m755 "$subfile" "${INSTALL_DIR}/lib/${subdir}/"
        fi
      done
    fi
  done
fi

# Install /data directory
if [ -d "data" ]; then
  install -dm755 "${INSTALL_DIR}/data"
  find data -type d -exec install -dm755 "${INSTALL_DIR}/{}" \;
  find data -type f -exec install -Dm644 {} "${INSTALL_DIR}/{}" \;
fi

# Install desktop file
if [ -f "share/applications/namida.desktop" ]; then
  install -Dm644 "share/applications/namida.desktop" \
    "/usr/share/applications/namida.desktop"
fi

# Install metainfo
if [ -f "share/metainfo/namida.metainfo.xml" ]; then
  install -Dm644 "share/metainfo/namida.metainfo.xml" \
    "/usr/share/metainfo/namida.metainfo.xml"
fi

# Install icons

if [ -f "share/icons/namida_512.png" ]; then
  install -Dm644 "share/icons/namida_512.png" \
    "/usr/share/icons/hicolor/512x512/apps/namida.png"
fi

if [ -f "share/icons/namida_128.png" ]; then
  install -Dm644 "share/icons/namida_128.png" \
    "/usr/share/icons/hicolor/128x128/apps/namida.png"
fi

if [ -f "share/icons/namida_256.png" ]; then
  install -Dm644 "share/icons/namida_256.png" \
    "/usr/share/icons/hicolor/256x256/apps/namida.png"
fi

# Create symlink to /usr/bin
mkdir -p /usr/bin
ln -sf "${INSTALL_DIR}/namida" "/usr/bin/namida"

# Update icon cache
if command -v gtk-update-icon-cache &> /dev/null; then
  echo -e "${YELLOW}Updating icon cache...${NC}"
  gtk-update-icon-cache -f -t /usr/share/icons/hicolor &> /dev/null || true
fi

# Update desktop database
if command -v update-desktop-database &> /dev/null; then
  echo -e "${YELLOW}Updating desktop database...${NC}"
  update-desktop-database -q /usr/share/applications &> /dev/null || true
fi

# Cleanup
cd /
rm -rf "$TEMP_DIR"

echo -e "${GREEN}Installation complete!${NC}"
echo -e "${GREEN}You can now run 'namida' from the command line or find it in your application menu.${NC}"
