Nouvelle version :
- Correction de la fonction de vérification des paquets requis
- Ajout d'une option (-r) pour supprimer un précédent répertoire de travail
- Ajout d'une option (-d) pour supprimer le répertoire de travail après utilisation
#!/bin/bash
## Add custom background to gdm3 login screen
## Tested with Ubuntu 18.04, 20.04 and 20.10
## Requires : libglib2.0-dev-bin '(glib-compile-resources)' libxml2-utils '(xmllint)' imagemagick '(convert)'
## Script version : 0.9
################################################################################
# Constants ####################################################################
UBUNTU_VERSION=$(lsb_release -rs)
SUPPORTED_VERSION="18.04 20.04 20.10"
REQUIRED_PACKAGES="xmllint glib-compile-resources convert"
GST=/usr/share/gnome-shell/gnome-shell-theme.gresource
WORKDIR=theme
BACKGROUND_WIDTH=1280
BACKGROUND_HEIGHT=768
BACKGROUND_TARGET_NAME=noise-texture.png
BACKGROUND_FILE=""
VERBOSE=0
ORIGINAL_BACKGROUND_20_10="#lockDialogGroup {\n background-color: #41494c; }"
EDITED_BACKGROUND_20_10="#lockDialogGroup {\n background: #41494c url(file:///usr/share/gnome-shell/theme/${BACKGROUND_TARGET_NAME});\n background-size: ${BACKGROUND_WIDTH}px ${BACKGROUND_HEIGHT}px;\n background-repeat: none;\n}"
ORIGINAL_BACKGROUND_20_04="#lockDialogGroup {\n background-color: #41494c; }"
EDITED_BACKGROUND_20_04="#lockDialogGroup {\n background: #41494c url(file:///usr/share/gnome-shell/theme/${BACKGROUND_TARGET_NAME});\n background-size: ${BACKGROUND_WIDTH}px ${BACKGROUND_HEIGHT}px;\n background-repeat: none;\n}"
ORIGINAL_BACKGROUND_18_04="#lockDialogGroup {\n background: #2e3436 url(resource:///org/gnome/shell/theme/noise-texture.png);\n background-repeat: repeat; }"
EDITED_BACKGROUND_18_04="#lockDialogGroup {\n background: #2e3436 url(file:///usr/share/gnome-shell/theme/${BACKGROUND_TARGET_NAME});\n background-size: ${BACKGROUND_WIDTH}px ${BACKGROUND_HEIGHT}px;\n background-repeat: none;\n}"
################################################################################
# Functions ####################################################################
supported_version() {
echo $SUPPORTED_VERSION | \
grep -w $UBUNTU_VERSION >/dev/null || { \
echo "Your Ubuntu version (${UBUNTU_VERSION}) is not supported!"
echo
echo "Make sure to use Ubuntu 18.04 LTS, Ubuntu 20.04 LTS or Ubuntu 20.10."
exit 1;
}
}
required_packages() {
[[ $(command -v ${REQUIRED_PACKAGES} | wc -l) -lt 3 ]] && {
echo "==> Make sure these packages are installed to continue:"
echo "--> libglib2.0-dev-bin"
echo "--> libxml2-utils"
echo "--> imagemagick"
echo
echo "==> You can copy/paste the following command into your terminal:"
echo "sudo apt install libglib2.0-dev-bin libxml2-utils imagemagick"
exit 1;
}
}
working_directory_theme() {
# Check working directory 'theme'
if [ -d "${PWD}/${WORKDIR}" ] && [ "${REMOVE_PREVIOUS_DIR_THEME}" = YES ]; then
rm -rf ${PWD}/${WORKDIR}
echo "--> An existing working directory has been removed successfully."
echo
elif [ -d "${PWD}/${WORKDIR}" ]; then
# Delete or rename previous working directory manually before proceed
echo "==> A working '${WORKDIR}' directory already exist !"
echo "Use '-r' option to remove it or delete it manually to continue."
exit
fi
}
background_status() {
# Let's make the background file is set to continue
[ "${BACKGROUND_FILE}" = "" ] && \
{
echo "==> The background file must be select to continue!"
exit 1
}
# Checking of file format validity
# If needed, converting other picture format to PNG and copy as 'noise-texture.png' filename to working directory 'theme'
case $(mimetype -b ${BACKGROUND_FILE}) in
image/png*) mkdir -p "${WORKDIR}"
extract_current_theme
cp -f ${BACKGROUND_FILE} "${WORKDIR}"/"${BACKGROUND_TARGET_NAME}"
echo "Your background file ${BACKGROUND_FILE##*/} has been copied to working directory ${WORKDIR} as ${BACKGROUND_TARGET_NAME}."
echo
;;
image/*) mkdir -p "${WORKDIR}"
extract_current_theme
echo "Convert in progress..."
convert ${BACKGROUND_FILE} "${WORKDIR}"/"${BACKGROUND_TARGET_NAME}" 2>&1 >/dev/null
echo "--> Done!"
echo
echo "Your background file ${BACKGROUND_FILE##*/} has been converted and copied to working directory ${WORKDIR} as ${BACKGROUND_TARGET_NAME.}"
echo
;;
*) echo "${BACKGROUND_FILE} is not a valid format picture!"
exit 1
;;
esac
}
extract_current_theme() {
for r in `gresource list "${GST}"`; do
gresource extract "${GST}" "$r" > "${WORKDIR}"/"${r##*/}"
done
}
create_new_gresource() {
# Write a new "gnome-shell-theme.gresource.xml" file
FILES=$(find "${WORKDIR}" -type f -printf "%P\n" | xargs -i echo " <file>{}</file>")
cat <<EOF >"${WORKDIR}/gnome-shell-theme.gresource.xml"
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gnome/shell/theme">
${FILES}
</gresource>
</gresources>
EOF
# Compile this XML file into a new gresource file
echo "Compiling gnome-shell-theme.gresource..."
cd "${WORKDIR}"
glib-compile-resources gnome-shell-theme.gresource.xml
echo "--> The file 'gnome-shell-theme.gresource' has been successfully created!"
echo
cd ..
}
edit_css_content() {
if test -f "${WORKDIR}"/gnome-shell.css; then
css_file=gnome-shell.css
ORIGINAL_BACKGROUND="${ORIGINAL_BACKGROUND_18_04}"
EDITED_BACKGROUND="${EDITED_BACKGROUND_18_04}"
elif test -f "${WORKDIR}"/gdm3.css; then
css_file=gdm3.css
ORIGINAL_BACKGROUND="${ORIGINAL_BACKGROUND_20_04}"
EDITED_BACKGROUND="${EDITED_BACKGROUND_20_04}"
elif test -f "${WORKDIR}"/gdm3.css; then
css_file=gdm3.css
ORIGINAL_BACKGROUND="${ORIGINAL_BACKGROUND_20_10}"
EDITED_BACKGROUND="${EDITED_BACKGROUND_20_10}"
else
echo "==> No css file found!"
exit 1
fi
# Apply customization to the .css file
sed -i -z "s%${ORIGINAL_BACKGROUND}%${EDITED_BACKGROUND}%" "${WORKDIR}/${css_file}"
}
install_new_theme() {
# Copy your background file to /usr/share/gnome-shell/theme
echo
echo "Copy your background file to /usr/share/gnome-shell/theme"
sudo cp "${WORKDIR}"/"${BACKGROUND_TARGET_NAME}" /usr/share/gnome-shell/theme/"${BACKGROUND_TARGET_NAME}"
echo "--> Done !"
echo
# Make sure to create a backup of original 'gnome-shell-theme.gresource' file to be able to restore in case of problem...
# If a backup file exist, copy 'gnome-shell-theme.gresource.backup' to 'gnome-shell-theme.gresource'
echo "Saving original 'gnome-shell-theme.gresource' to 'gnome-shell-theme.gresource.backup'"
if test -f "${GST}".backup; then
sudo cp "${GST}".backup "${GST}"
echo "--> Done !"
else
# If 'gnome-shell-theme.gresource.backup' does not exist, copy 'gnome-shell-theme.gresource' to 'gnome-shell-theme.gresource.backup'
sudo cp "${GST}"{,.backup}
echo "--> Done !"
echo
fi
# Now, copy your custom 'gnome-shell-theme.gresource' to /usr/share/gnome-shell
echo
echo "Copy your custom 'gnome-shell-theme.gresource' to /usr/share/gnome-shell"
sudo cp "${WORKDIR}"/gnome-shell-theme.gresource /usr/share/gnome-shell
echo "--> Done !"
echo
}
apply_new_alternative_gresource() {
if [[ "${UBUNTU_VERSION}" =~ "18.04" ]]; then
sudo update-alternatives --set gdm3.css /usr/share/gnome-shell/theme/ubuntu.css
echo -e "Making new CSS alternative... \n--> Done !"
echo
elif [[ "${UBUNTU_VERSION}" =~ "20.04" ]]; then
sudo update-alternatives --set gdm3-theme.gresource /usr/share/gnome-shell/gnome-shell-theme.gresource
echo -e "Making new CSS alternative... \n--> Done !"
echo
elif [[ "${UBUNTU_VERSION}" =~ "20.10" ]]; then
sudo update-alternatives --set gdm3-theme.gresource /usr/share/gnome-shell/gnome-shell-theme.gresource
echo -e "Making new CSS alternative... \n--> Done !"
echo
fi
}
usage() {
cat << EOF
Usage: $0 [ -f, --file "/path/to/my/picture.{png,jpeg,jpg,etc}" ] [ -w, --width "PICTURE_WIDTH" ] [ -h, --height "PICTURE_HEIGHT" ]
Required argument:
-f, --file Background picture file
Optional arguments:
-r, --remove Remove previous existing '$WORKDIR' directory
-d, --delete Delete working directory '$WORKDIR' after install
-w, --width Width value in pixel (default value is the picture size)
-h, --height Height value in pixel (default value is the picture size)
-v, --verbose Increase verbosity
Help:
--help Show this help
EOF
}
verbose_mode() {
# verbose mode, if set
(( $VERBOSE > 0 )) &&
cat <<-EOF
Preview settings:
Wallpaper is '${BACKGROUND_FILE##*/}'
Resolution is '${BACKGROUND_WIDTH}x${BACKGROUND_HEIGHT}'
EOF
}
delete_workdir_after_install() {
# Delete working directory after install
if [[ "${DELETE_DIR_THEME_AFTER_INSTALL}" = "YES" ]]; then
rm -rf ${PWD}/${WORKDIR}
echo
echo "--> The working directory has been removed successfully."
echo
fi
}
################################################################################
# "Main" part of the script ####################################################
supported_version
required_packages
# If no arg then exit
if [ $# -eq "0" ]; then
echo "==> No argument found!"
echo
echo "Type '$0 --help' for details."
exit 1
fi
# Handling arguments
while [ $# -gt 0 ]; do
case "$1" in
--help) usage; exit ;;
-r|--remove) REMOVE_PREVIOUS_DIR_THEME=YES ;;
-d|--delete) DELETE_DIR_THEME_AFTER_INSTALL=YES ;;
-w|--width) if [ -z $2 ]; then echo "Set the wished width of your background file"; exit 1; elif ! [[ "$2" == ?(-)+([0-9.]) ]]; then echo "Width must be a number!"; exit 1; else BACKGROUND_WIDTH="$2"; shift; fi ;;
-h|--height) if [ -z $2 ]; then echo "Set the wished height of your background file"; exit 1; elif ! [[ "$2" == ?(-)+([0-9.]) ]]; then echo "Height must be a number!"; exit 1; else BACKGROUND_HEIGHT="$2"; shift; fi ;;
-f|--file) if [ -z $2 ]; then echo "Select your background file to continue"; exit 1; elif ! test "$(file -b $2| grep -oi 'image')"; then echo "The background file must be a valid picture format!"; exit 1; else BACKGROUND_FILE="$2"; shift; fi ;;
-v|--verbose) VERBOSE=$((VERBOSE + 1)) ;;
*) echo "Unknown option"; exit ;;
esac
shift
done
verbose_mode
working_directory_theme
background_status
edit_css_content
create_new_gresource
echo "####################################################"
echo "# The next operations require superuser privileges #"
echo "# #"
install_new_theme
apply_new_alternative_gresource
echo "# #"
echo "# End superuser operations #"
echo "####################################################"
delete_workdir_after_install
echo
echo "### Restart your computer and enjoy :) ###"
echo
################################################################################