From 007397c88e4167dcd46fab1543b990c93485d275 Mon Sep 17 00:00:00 2001 From: Moririn Date: Tue, 12 Sep 2023 16:53:05 +0100 Subject: [PATCH] Read theme from central $XDG_CONFIG_DIR location rather than being duplicated per profile --- foxtheme.sh | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/foxtheme.sh b/foxtheme.sh index 21576ba..4cf172f 100755 --- a/foxtheme.sh +++ b/foxtheme.sh @@ -1,42 +1,69 @@ #!/bin/sh SCRIPTLOCATION=$(dirname -- "$(readlink -f "$0")") +XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" [ -d "$SCRIPTLOCATION" ] || { echo "$0: Could not verify script location as an existing directory. This is probably a bug."; exit 2; } [ -d "$SCRIPTLOCATION"/chrome ] || { echo "$0: Could not proceed with installation as 'chrome' directory is missing from '$SCRIPTLOCATION'."; exit 2; } [ -f "$SCRIPTLOCATION"/user.js ] || { echo "$0: Could not proceed with installation as 'user.js' file is missing from '$SCRIPTLOCATION'."; exit 2; } helptext() { echo "Usage: ./foxtheme.sh [OPTIONS] [PROFILE | -a PROFILESDIR] " - echo "-a Apply theme to all (current) browser profiles" - echo "-j Just create or modify user.js and not user-overrides.js" - echo "-o Remove and replace an existing theme if found" echo "-h Display this help message" + echo "-o Remove and replace an existing theme if found" + echo "-a Apply theme to all (current) browser profiles" + echo "-r Apply modifications to user-overrides.js instead of user.js" + echo "-d Don't modify existing user.js values. May cause the theme to behave incorrectly." } installtoprofile() { targetprofile="$1" + if [ "$OVERRIDES" ]; then + userjs="${targetprofile}/user-overrides.js" + else + userjs="${targetprofile}/user.js" + [ -f "$userjs" ] && echo "$0: Warning - Existing user.js file found at '$userjs' (pass -r to modify user-overrides.js instead)" + fi [ -d "$targetprofile" ] || { echo "$0: Profile at '$targetprofile' does not exist"; exit 2; } [ -d "$targetprofile/chrome/" ] && { - [ "$OVERWRITE" ] || { echo "$0: A theme for this profile '${targetprofile}/chrome/' already exists. Pass -o to overwrite existing themes."; exit 2; } - rm -r "${targetprofile:?}/chrome/" + [ "$OVERWRITE" ] || { echo "$0: A theme for this profile already exists at '${targetprofile}/chrome/'. Pass -o to overwrite existing themes."; exit 2; } + rm -r -f "${targetprofile:?}/chrome" } - cp -r "${SCRIPTLOCATION}/chrome/" "${targetprofile}/" - cat "${SCRIPTLOCATION}/user.js" >> "${targetprofile:?}/user.js" - [ "$NOOVERRIDE" ] || cat "${SCRIPTLOCATION}/user.js" >> "${targetprofile:?}/user-overrides.js" + [ -d "$XDG_CONFIG_HOME"/foxtheme/chrome ] && { + if ! diff -q "${SCRIPTLOCATION}/chrome" "${XDG_CONFIG_HOME}/foxtheme/chrome" >/dev/null 2>&1; then + [ "$OVERWRITE" ] || { echo "$0: A theme already exists at '${XDG_CONFIG_HOME}/foxtheme/chrome/'. Pass -o to overwrite existing themes."; exit 2; } + rm -r -f "${XDG_CONFIG_HOME:?}/foxtheme/chrome" + fi + } + + mkdir -p "${XDG_CONFIG_HOME:?}/foxtheme/" + cp -r "${SCRIPTLOCATION:?}/chrome/" "${XDG_CONFIG_HOME:?}/foxtheme/" + ln -s -f "${XDG_CONFIG_HOME:?}/foxtheme/chrome/" "${targetprofile:?}" + [ -f "$userjs" ] || touch "$userjs" + for pref in $(sed --posix -e "s/^.*(\"//" -e "s/\",.*)\;$//" "${SCRIPTLOCATION}/user.js"); do + if [ "$NOCHANGEJS" ]; then + grep -q -s "$pref" "$userjs" || grep "\"$pref\"" "${SCRIPTLOCATION}/user.js" >> "$userjs" + else + sed -i "/\"$pref\"/d" "${userjs:?}" + grep "\"$pref\"" "${SCRIPTLOCATION}/user.js" >> "${userjs:?}" + fi + done echo "Successfully installed to '${targetprofile}'" } -while getopts "ajoh" opt; do +while getopts "hoard" opt; do case $opt in a) ALL=1 ;; - j) - NOOVERRIDE=1 + d) + NOCHANGEJS=1 ;; o) OVERWRITE=1 ;; + r) + OVERRIDES=1 + ;; h) helptext exit