#!/usr/bin/env bash
# Live Wallpaper for Linux (GNOME): https://editz.pro/wallpaper
#   curl -fsSL https://editz.pro/wallpaper/install.sh | bash
# Installs into your home folder only (no sudo, except to add missing system packages if you agree).
set -euo pipefail

VERSION=1.1.1
URL="${LW_URL:-https://editz.pro/downloads/live-wallpaper-$VERSION.tar.gz}"
DEST="$HOME/.local/share/live-wallpaper"
UUID="live-wallpaper@editz.pro"
EXT_DIR="$HOME/.local/share/gnome-shell/extensions"

bold() { printf '\033[1m%s\033[0m\n' "$*"; }
say() { printf '  %s\n' "$*"; }

bold "Live Wallpaper $VERSION"

# --- desktop check
if ! command -v gnome-shell >/dev/null 2>&1; then
  say "This is made for GNOME (Ubuntu, Fedora, Zorin, Debian GNOME…). gnome-shell wasn't found,"
  say "so the clock, widgets and background integration won't work here."
  read -r -p "  Install anyway? [y/N] " ans </dev/tty || ans=n
  [[ "$ans" =~ ^[Yy] ]] || exit 1
else
  ver=$(gnome-shell --version | grep -oE '[0-9]+' | head -1)
  if [ "${ver:-0}" -lt 45 ]; then
    say "GNOME $ver found; the helper needs GNOME 45 or newer. The video window will still work."
  fi
fi

# --- dependencies
missing=()
command -v mpv >/dev/null 2>&1 || missing+=(mpv)
command -v ffmpeg >/dev/null 2>&1 || missing+=(ffmpeg)
python3 - <<'PY' 2>/dev/null || missing+=(python-gtk)
import gi
gi.require_version('Gtk', '3.0'); gi.require_version('Wnck', '3.0'); gi.require_version('PangoCairo', '1.0')
from gi.repository import Gtk, Wnck, PangoCairo
import cairo
PY

if [ ${#missing[@]} -gt 0 ]; then
  if command -v apt-get >/dev/null; then
    cmd="sudo apt-get install -y mpv ffmpeg python3-gi python3-gi-cairo gir1.2-gtk-3.0 gir1.2-wnck-3.0"
  elif command -v dnf >/dev/null; then
    cmd="sudo dnf install -y mpv ffmpeg python3-gobject python3-cairo gtk3 libwnck3"
  elif command -v pacman >/dev/null; then
    cmd="sudo pacman -S --needed mpv ffmpeg python-gobject python-cairo gtk3 libwnck3"
  elif command -v zypper >/dev/null; then
    cmd="sudo zypper install -y mpv ffmpeg python3-gobject python3-gobject-cairo typelib-1_0-Gtk-3_0 typelib-1_0-Wnck-3_0"
  else
    cmd=""
  fi
  say "Missing: ${missing[*]}"
  if [ -n "$cmd" ]; then
    say "They can be installed with:"
    say "  $cmd"
    read -r -p "  Run that now? [Y/n] " ans </dev/tty || ans=n
    if [[ ! "$ans" =~ ^[Nn] ]]; then
      $cmd
    else
      say "Install them, then run this installer again."; exit 1
    fi
  else
    say "Install mpv, ffmpeg, PyGObject with GTK 3 and libwnck 3, then run this again."; exit 1
  fi
  [[ "$cmd" == *dnf* ]] && ! command -v mpv >/dev/null && say "On Fedora, mpv comes from RPM Fusion (rpmfusion.org)."
fi

# --- download and unpack
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
say "Downloading…"
curl -fsSL "$URL" -o "$tmp/lw.tar.gz"
tar -xzf "$tmp/lw.tar.gz" -C "$tmp"
mkdir -p "$DEST"
rm -rf "$DEST/desktop" "$DEST/chrome-extension"  # chrome-extension: left over from 1.0
cp -r "$tmp/live-wallpaper/." "$DEST/"
chmod +x "$DEST/desktop/livewall.py" "$DEST/uninstall.sh"

# --- GNOME helper (linked, not copied)
mkdir -p "$EXT_DIR"
ln -sfn "$DEST/desktop/shell-helper/$UUID" "$EXT_DIR/$UUID"
if command -v gsettings >/dev/null && gsettings list-schemas | grep -qx org.gnome.shell; then
  python3 - "$UUID" <<'PY'
import sys
from gi.repository import Gio
s = Gio.Settings.new('org.gnome.shell')
uuid = sys.argv[1]
enabled = s.get_strv('enabled-extensions')
if uuid not in enabled:
    s.set_strv('enabled-extensions', enabled + [uuid])
s.set_boolean('disable-user-extensions', False)
PY
fi

# --- app menu entry
mkdir -p "$HOME/.local/share/applications"
cat > "$HOME/.local/share/applications/live-wallpaper.desktop" <<EOF
[Desktop Entry]
Type=Application
Name=Live Wallpaper
Comment=Moving wallpaper, lock-screen clock and widgets
Exec=python3 $DEST/desktop/livewall.py
Icon=preferences-desktop-wallpaper
Categories=Settings;DesktopSettings;
StartupWMClass=livewall
EOF

echo
bold "Installed."
say "1. Open Live Wallpaper from your app menu and pick a scene or a video."
if [ "${XDG_SESSION_TYPE:-}" = "wayland" ]; then
  say "2. Log out and back in once, so GNOME loads the helper."
else
  say "2. Press Alt+F2, type r and press Enter (or log out and in), so GNOME loads the helper."
fi
say "Paste your license key in Settings → License (buy one for \$2 at editz.pro/wallpaper, or use your Eddie key)."
say "Remove it any time: $DEST/uninstall.sh"
