Read theme from central $XDG_CONFIG_DIR location rather than being duplicated per profile

This commit is contained in:
Moririn 2023-09-12 16:53:05 +01:00
parent f654da2e7c
commit 007397c88e
1 changed files with 38 additions and 11 deletions

View File

@ -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