Last active 1 week ago

switch_wallpaper.sh Raw
1#!/bin/bash
2set -e
3
4# Help function
5usage() {
6 echo "Usage: sudo $0 <path-to-image>"
7 echo "Changes the GDM background to the specified image."
8 echo "Note: This script must be run as root."
9 exit 1
10}
11
12# Check for root
13if [ "$EUID" -ne 0 ]; then
14 echo "Error: This script must be run as root."
15 exit 1
16fi
17
18# Check arguments
19TARGET_IMAGE="$1"
20if [ -z "$TARGET_IMAGE" ]; then
21 usage
22fi
23
24if [ ! -f "$TARGET_IMAGE" ]; then
25 echo "Error: Image file '$TARGET_IMAGE' not found."
26 exit 1
27fi
28
29# Check dependencies
30if ! command -v glib-compile-resources &> /dev/null; then
31 echo "Error: 'glib-compile-resources' is not installed."
32 echo "Please install 'libglib2.0-dev-bin' (Ubuntu/Debian) or 'glib2-devel' (Fedora)."
33 exit 1
34fi
35
36# Define Paths
37REPO_DIR="$(dirname "$(readlink -m "${0}")")"
38THEME_ASSETS_DIR="${REPO_DIR}/other/gdm/theme"
39XML_FILE="${REPO_DIR}/other/gdm/gnome-shell-theme.gresource.xml"
40WORK_DIR="/tmp/gdm-switch-wallpaper-work"
41
42# Targeted GDM theme files (Priority order based on lib-core.sh)
43POP_OS_GR_FILE="/usr/share/gnome-shell/theme/Pop/gnome-shell-theme.gresource"
44YARU_GR_FILE="/usr/share/gnome-shell/theme/Yaru/gnome-shell-theme.gresource"
45ZORIN_GR_DARK_FILE="/usr/share/gnome-shell/theme/ZorinBlue-Dark/gnome-shell-theme.gresource"
46ZORIN_GR_LIGHT_FILE="/usr/share/gnome-shell/theme/ZorinBlue-Light/gnome-shell-theme.gresource"
47MISC_GR_FILE="/usr/share/gnome-shell/gnome-shell-theme.gresource"
48
49# Find which theme file is in use
50TARGET_FILE=""
51if [ -f "$POP_OS_GR_FILE" ]; then
52 TARGET_FILE="$POP_OS_GR_FILE"
53elif [ -f "$YARU_GR_FILE" ]; then
54 TARGET_FILE="$YARU_GR_FILE"
55elif [ -f "$ZORIN_GR_DARK_FILE" ]; then
56 TARGET_FILE="$ZORIN_GR_DARK_FILE"
57elif [ -f "$ZORIN_GR_LIGHT_FILE" ]; then
58 TARGET_FILE="$ZORIN_GR_LIGHT_FILE"
59elif [ -f "$MISC_GR_FILE" ]; then
60 TARGET_FILE="$MISC_GR_FILE"
61else
62 echo "Error: Could not find any supported GDM theme .gresource file."
63 exit 1
64fi
65
66echo "Target GDM theme file: $TARGET_FILE"
67
68# Prepare Workspace
69echo "Preparing temporary workspace at $WORK_DIR..."
70rm -rf "$WORK_DIR"
71mkdir -p "$WORK_DIR"
72
73# Copy base theme assets
74if [ ! -d "$THEME_ASSETS_DIR" ]; then
75 echo "Error: Theme assets not found at $THEME_ASSETS_DIR"
76 exit 1
77fi
78cp -r "$THEME_ASSETS_DIR"/* "$WORK_DIR/"
79
80# Copy and rename the new background image
81echo "Processing background image..."
82cp "$TARGET_IMAGE" "$WORK_DIR/background.png"
83
84# Backup existing theme file
85if [ ! -f "${TARGET_FILE}.bak" ]; then
86 echo "Backing up original theme to ${TARGET_FILE}.bak..."
87 cp "$TARGET_FILE" "${TARGET_FILE}.bak"
88else
89 echo "Backup already exists at ${TARGET_FILE}.bak, skipping backup."
90fi
91
92# Compile Resources
93echo "Compiling GDM resources..."
94glib-compile-resources \
95 --sourcedir="$WORK_DIR" \
96 --target="$TARGET_FILE" \
97 "$XML_FILE"
98
99# Cleanup
100rm -rf "$WORK_DIR"
101
102echo "Success! GDM background has been updated."
103echo "You may need to restart GDM or reboot to see the changes."
104echo " - To restart GDM: sudo systemctl restart gdm"
105