#!/bin/bash
# =============================================================
#  FONTEX - Skrypt instalacyjny dla Linux
#  Uruchom ten skrypt po rozpakowaniu paczki.
# =============================================================

MODULE_NAME="FONTEX"
DISPLAY_NAME="Fontex"
CATEGORIES="Graphics;2DGraphics;"

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
EXEC_FILE="$SCRIPT_DIR/$MODULE_NAME"
ICON_FILE="$SCRIPT_DIR/Assets/Image/fontex.png"
DESKTOP_FILE="$HOME/.local/share/applications/$MODULE_NAME.desktop"

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

echo ""
echo "============================================="
echo "  $DISPLAY_NAME - Instalacja (Linux)"
echo "============================================="
echo ""

# --- 1. Sprawdzenie .NET 8+ ---
echo "[ 1/3 ] Sprawdzanie .NET Runtime..."
if ! command -v dotnet &> /dev/null; then
    echo -e "${RED}BŁĄD: .NET Runtime nie jest zainstalowany.${NC}"
    echo ""
    echo "Zainstaluj .NET 8 Runtime:"
    echo "  Ubuntu/Debian : sudo apt install -y dotnet-runtime-8.0"
    echo "  Fedora/RHEL   : sudo dnf install -y dotnet-runtime-8.0"
    echo "  Arch Linux    : sudo pacman -S dotnet-runtime-8.0"
    echo "  Inne          : https://dotnet.microsoft.com/download/dotnet/8.0"
    echo ""
    exit 1
fi

DOTNET_MAJOR=$(dotnet --version 2>/dev/null | cut -d'.' -f1)
if [ -z "$DOTNET_MAJOR" ] || [ "$DOTNET_MAJOR" -lt 8 ]; then
    echo -e "${RED}BŁĄD: Wymagany .NET 8 lub nowszy. Zainstalowana wersja: $(dotnet --version)${NC}"
    exit 1
fi
echo -e "${GREEN}  ✓ .NET $(dotnet --version) - OK${NC}"

# --- 2. Nadanie uprawnień wykonania ---
echo "[ 2/3 ] Nadawanie uprawnień wykonania..."
if [ -f "$EXEC_FILE" ]; then
    chmod +x "$EXEC_FILE"
    echo -e "${GREEN}  ✓ chmod +x $MODULE_NAME${NC}"
else
    echo -e "${YELLOW}  ! Plik wykonywalny nie znaleziony: $EXEC_FILE${NC}"
    echo -e "${YELLOW}    Aplikację możesz uruchomić przez: dotnet $MODULE_NAME.dll${NC}"
fi

# --- 3. Tworzenie skrótu desktopowego ---
echo "[ 3/3 ] Tworzenie skrótu aplikacji..."
mkdir -p "$HOME/.local/share/applications"

ICON_PATH="application-x-executable"
if [ -f "$ICON_FILE" ]; then
    ICON_PATH="$ICON_FILE"
fi

cat > "$DESKTOP_FILE" << DESKTOP_EOF
[Desktop Entry]
Version=1.0
Type=Application
Name=$DISPLAY_NAME
Comment=RetroTools - $DISPLAY_NAME
Exec=bash -c 'cd "$SCRIPT_DIR" && ./$MODULE_NAME'
Icon=$ICON_PATH
Terminal=false
StartupNotify=true
Categories=$CATEGORIES
DESKTOP_EOF

chmod +x "$DESKTOP_FILE"
echo -e "${GREEN}  ✓ Wpis w menu aplikacji: $DESKTOP_FILE${NC}"

# Odswiezenie bazy danych menu
if command -v update-desktop-database &> /dev/null; then
    update-desktop-database "$HOME/.local/share/applications" 2>/dev/null
fi

# Skrot na pulpicie (jezeli istnieje folder Desktop lub Pulpit)
DESKTOP_FOLDER=""
if [ -d "$HOME/Desktop" ]; then
    DESKTOP_FOLDER="$HOME/Desktop"
elif [ -d "$HOME/Pulpit" ]; then
    DESKTOP_FOLDER="$HOME/Pulpit"
fi

if [ -n "$DESKTOP_FOLDER" ]; then
    cp "$DESKTOP_FILE" "$DESKTOP_FOLDER/"
    chmod +x "$DESKTOP_FOLDER/$MODULE_NAME.desktop"
    if command -v gio &> /dev/null; then
        gio set "$DESKTOP_FOLDER/$MODULE_NAME.desktop" metadata::trusted true 2>/dev/null
    fi
    echo -e "${GREEN}  ✓ Skrót na pulpicie: $DESKTOP_FOLDER/$MODULE_NAME.desktop${NC}"
fi

echo ""
echo -e "${GREEN}=============================================${NC}"
echo -e "${GREEN}  Instalacja zakończona pomyślnie!${NC}"
echo -e "${GREEN}=============================================${NC}"
echo ""
echo "Uruchomienie: $EXEC_FILE"
echo ""
