Refactorise a little bit sxmo_migrate.sh

Some code could be deduplicated.

Signed-off-by: Stacy Harper <contact@stacyharper.net>
Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
master
Stacy Harper 3 years ago committed by Maarten van Gompel
parent cfa15765e2
commit 6dce18e579
  1. 42
      scripts/core/sxmo_migrate.sh

@ -12,24 +12,32 @@ smartdiff() {
fi fi
} }
defaultconfig() { resolvedifference() {
if [ ! -r "$2" ]; then userfile="$1"
mkdir -p "$(dirname "$2")" defaultfile="$2"
cp "$1" "$2"
chmod "$3" "$2"
else
if ! diff "$2" "$1" > /dev/null; then
( (
printf "\e[31mThe file \e[32m%s\e[31m differs\e[0m\n" "$2" printf "\e[31mThe file \e[32m%s\e[31m differs\e[0m\n" "$userfile"
smartdiff -ud "$2" "$1" smartdiff -ud "$userfile" "$defaultfile"
) | less -RF ) | less -RF
printf "\e[33mDo you want to apply the default? [y/N], or perhaps open an editor [e]?\e[0m " printf "\e[33mDo you want to apply the default? [y/N], or perhaps open an editor [e]?\e[0m "
read -r reply < /dev/tty read -r reply < /dev/tty
if [ "y" = "$reply" ]; then if [ "y" = "$reply" ]; then
cp "$1" "$2" cp "$defaultfile" "$userfile"
elif [ "e" = "$reply" ]; then elif [ "e" = "$reply" ]; then
$EDITOR "$2" "$1" $EDITOR "$userfile" "$defaultfile"
fi fi
}
defaultconfig() {
if [ ! -r "$2" ]; then
mkdir -p "$(dirname "$2")"
cp "$1" "$2"
chmod "$3" "$2"
else
if ! diff "$2" "$1" > /dev/null; then
resolvedifference "$2" "$1"
fi fi
fi fi
} }
@ -40,17 +48,7 @@ checkhooks() {
defaulthook="/usr/share/sxmo/default_hooks/$(basename "$hook")" defaulthook="/usr/share/sxmo/default_hooks/$(basename "$hook")"
if [ -f "$defaulthook" ]; then if [ -f "$defaulthook" ]; then
if ! diff "$hook" "$defaulthook" > /dev/null; then if ! diff "$hook" "$defaulthook" > /dev/null; then
( resolvedifference "$hook" "$defaulthook"
printf "\e[31mThe file \e[32m%s\e[31m differs\e[0m\n" "$hook"
smartdiff -ud "$hook" "$defaulthook"
) | less -RF
printf "\e[33mDo you want to remove the custom hook and fall back to the default? [y/N], or perhaps open an editor [e]?\e[0m"
read -r reply < /dev/tty
if [ "y" = "$reply" ]; then
rm "$hook"
elif [ "e" = "$reply" ]; then
$EDITOR "$hook" "$defaulthook"
fi
else else
printf "\e[33mHook %s is identical to the default, so you don't need a custom hook, remove it? [Y/n]\e[0m" "$hook" printf "\e[33mHook %s is identical to the default, so you don't need a custom hook, remove it? [Y/n]\e[0m" "$hook"
if [ "n" != "$reply" ]; then if [ "n" != "$reply" ]; then

Loading…
Cancel
Save