#!/bin/bash

# Install desktop entry and icon for StreamFlow Desktop App
# This makes the app appear with proper icon in system launcher

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ICON_PATH="$SCRIPT_DIR/build/icon.png"
DESKTOP_FILE="$SCRIPT_DIR/build/streamflow.desktop"

# Create local directories if they don't exist
mkdir -p ~/.local/share/applications
mkdir -p ~/.local/share/icons/hicolor/512x512/apps

# Copy icon
echo "Installing icon..."
cp "$ICON_PATH" ~/.local/share/icons/hicolor/512x512/apps/streamflow.png

# Update icon cache
if command -v gtk-update-icon-cache &> /dev/null; then
    gtk-update-icon-cache ~/.local/share/icons/hicolor/ -f
fi

# Create desktop file with correct paths
echo "Creating desktop entry..."
cat > ~/.local/share/applications/streamflow.desktop << EOF
[Desktop Entry]
Name=StreamFlow
Comment=IPTV Desktop Application
Exec=$SCRIPT_DIR/node_modules/.bin/electron $SCRIPT_DIR --dev
Icon=streamflow
Terminal=false
Type=Application
Categories=AudioVideo;Video;Player;TV;
StartupWMClass=StreamFlow - IPTV Player
StartupNotify=true
EOF

# Update desktop database
if command -v update-desktop-database &> /dev/null; then
    update-desktop-database ~/.local/share/applications/
fi

echo "✅ Desktop entry installed successfully!"
echo "Icon: ~/.local/share/icons/hicolor/512x512/apps/streamflow.png"
echo "Desktop file: ~/.local/share/applications/streamflow.desktop"
echo ""
echo "The app should now appear in your application launcher with the proper icon."
echo "You may need to log out and back in or restart your desktop environment for changes to take effect."
