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