chore: format shell scripts (#17674)

This commit is contained in:
andig 2024-12-09 20:22:01 +01:00 • committed by GitHub
parent 782f83c3e2
commit 7a7d5aae9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 249 additions and 209 deletions

View file

@ -13,16 +13,15 @@ askUserKeepFile() {
echo "Shall '$1' be deleted? [Y/n]: "
read answer
case "$answer" in
n*|N*)
n* | N*)
echo "Ok. We will keep that file. Keep in mind that you may need to alter it if any changes are done upstream. Your answer is saved for the future."
return 1
;;
y*|Y*|"")
y* | Y* | "")
echo "The file will be deleted."
return 0
;;
*)
;;
*) ;;
esac
done
}
@ -53,7 +52,7 @@ if [ "$1" = "configure" ]; then
askUserKeepFile "$USR_LOCAL_BIN" || KEEP_USR_LOCAL_BIN=$?
fi
# Save the user decision
cat > "$USER_CHOICE_CONFIG" <<EOF
cat > "$USER_CHOICE_CONFIG" << EOF
#!/bin/sh
KEEP_ETC_SERVICE=$KEEP_ETC_SERVICE
KEEP_USR_LOCAL_BIN=$KEEP_USR_LOCAL_BIN
@ -71,31 +70,31 @@ EOF
fi
fi
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ]; then
# This will only remove masks created by d-s-h on package removal.
deb-systemd-helper unmask evcc.service >/dev/null || true
deb-systemd-helper unmask evcc.service > /dev/null || true
# was-enabled defaults to true, so new installations run enable.
if deb-systemd-helper --quiet was-enabled evcc.service; then
# Enables the unit on first installation, creates new
# symlinks on upgrades if the unit file has changed.
deb-systemd-helper enable evcc.service >/dev/null || true
deb-systemd-helper enable evcc.service > /dev/null || true
else
# Update the statefile to add new symlinks (if any), which need to be
# cleaned up on purge. Also remove old symlinks.
deb-systemd-helper update-state evcc.service >/dev/null || true
deb-systemd-helper update-state evcc.service > /dev/null || true
fi
# Restart only if it was already started
if [ -d /run/systemd/system ]; then
systemctl --system daemon-reload >/dev/null || true
systemctl --system daemon-reload > /dev/null || true
if [ -f $RESTART_FLAG_FILE ]; then
deb-systemd-invoke start evcc.service >/dev/null || true
deb-systemd-invoke start evcc.service > /dev/null || true
rm $RESTART_FLAG_FILE
elif [ -n "$2" ]; then
deb-systemd-invoke try-restart evcc.service >/dev/null || true
deb-systemd-invoke try-restart evcc.service > /dev/null || true
else
deb-systemd-invoke start evcc.service >/dev/null || true
deb-systemd-invoke start evcc.service > /dev/null || true
fi
fi
fi

View file

@ -2,25 +2,25 @@
set -e
if [ -d /run/systemd/system ]; then
systemctl --system daemon-reload >/dev/null || true
systemctl --system daemon-reload > /dev/null || true
fi
if [ "$1" = "remove" ]; then
if [ -x "/usr/bin/deb-systemd-helper" ]; then
deb-systemd-helper mask evcc.service >/dev/null || true
deb-systemd-helper mask evcc.service > /dev/null || true
fi
fi
if [ "$1" = "purge" ]; then
if [ -x "/usr/bin/deb-systemd-helper" ]; then
deb-systemd-helper purge evcc.service >/dev/null || true
deb-systemd-helper unmask evcc.service >/dev/null || true
deb-systemd-helper purge evcc.service > /dev/null || true
deb-systemd-helper unmask evcc.service > /dev/null || true
fi
fi
# if interactive: call `/usr/bin/evcc checkconfig` and check the return code (newer version)
# if return code is 0, do nothing
# else: Ask user if he wants to keep the old version (working) or the new version (not working)
# else: Ask user if he wants to keep the old version (working) or the new version (not working)
# Remember the choice with /tmp/.evccrollback and fail new-postrm failed-upgrade old-version new-version to initiate dpkg's rollback
if [ "$1" = "upgrade" ] && [ -t 0 ]; then
if ! EVCC_DATABASE_DSN=/var/lib/evcc/evcc.db /usr/bin/evcc checkconfig > /dev/null; then
@ -34,22 +34,21 @@ if [ "$1" = "upgrade" ] && [ -t 0 ]; then
echo "Do you want to keep your old (working) version? [Y/n]: "
read choice
case "$choice" in
n*|N*|"")
n* | N* | "")
echo "We will keep the new version. Your configuration stays untouched!"
break
;;
y*|Y*)
y* | Y*)
echo "The old version will be restored. Your configuration stays untouched! Following errors are intended:"
touch /tmp/.evccrollback
exit 1
break
;;
*)
;;
*) ;;
esac
done
fi
fi
fi
# if upgrade goal fails, new-postrm failed-upgrade old-version new-version is called. It should fail to initiate rollback
if [ "$1" = "failed-upgrade" ]; then

View file

@ -13,92 +13,92 @@ EVCC_HOME="/var/lib/$EVCC_USER"
RESTART_FLAG_FILE="/tmp/.restartEvccOnUpgrade"
copyDbToUserDir() {
CURRENT_USER=$(systemctl show -pUser evcc | cut -d= -f2)
if [ -z "$CURRENT_USER" ]; then
CURRENT_USER=root
fi
CURRENT_HOME=$(getent passwd "$CURRENT_USER" | cut -d: -f6)
COPIED_FLAG="$CURRENT_HOME/.evcc/.copiedToEvccUser"
if [ -f "$CURRENT_HOME/.evcc/evcc.db" ] && [ ! -f "$COPIED_FLAG" ]; then
if [ -f "$EVCC_HOME/evcc.db" ]; then
echo "--------------------------------------------------------------------------------"
echo "Not copying $CURRENT_HOME/.evcc/evcc.db to $EVCC_HOME/evcc.db, since there is"
echo "already a database there."
echo "Either delete one of the databases or run 'touch $COPIED_FLAG' to keep both,"
echo "then restart installation."
echo "Hint: usually the larger one is the one to keep."
ls -la "$CURRENT_HOME/.evcc/evcc.db" "$EVCC_HOME/evcc.db"
echo "--------------------------------------------------------------------------------"
exit 1
else
cp -Rp "$CURRENT_HOME"/.evcc/evcc.db "$EVCC_HOME"
fi
chown "$EVCC_USER:$EVCC_GROUP" "$EVCC_HOME/evcc.db"
touch "$COPIED_FLAG"
if [ -n "$(ls -A /etc/systemd/system/evcc.service.d 2>/dev/null)" ]; then
echo "--------------------------------------------------------------------------------"
echo "You have overrides defined in /etc/systemd/system/evcc.service.d."
echo "This update changes the evcc user to 'evcc' (from root) and the database file"
echo "to '/var/lib/evcc/evcc.db"
echo "Make sure that you neither override 'User' nor 'ExecStart'"
echo "Hint: you can delete all overrides with 'systemctl revert evcc'"
echo "As a precaution, evcc is not started even if it was previously started."
echo "--------------------------------------------------------------------------------"
rm -f "$RESTART_FLAG_FILE"
else
echo "--------------------------------------------------------------------------------"
echo "NOTE: evcc user has changed from $CURRENT_USER to $EVCC_USER, db has been copied to new"
echo "directory $EVCC_HOME/evcc.db, old db in $CURRENT_USER/.evcc has been retained."
echo "--------------------------------------------------------------------------------"
fi
fi
return 0
CURRENT_USER=$(systemctl show -pUser evcc | cut -d= -f2)
if [ -z "$CURRENT_USER" ]; then
CURRENT_USER=root
fi
CURRENT_HOME=$(getent passwd "$CURRENT_USER" | cut -d: -f6)
COPIED_FLAG="$CURRENT_HOME/.evcc/.copiedToEvccUser"
if [ -f "$CURRENT_HOME/.evcc/evcc.db" ] && [ ! -f "$COPIED_FLAG" ]; then
if [ -f "$EVCC_HOME/evcc.db" ]; then
echo "--------------------------------------------------------------------------------"
echo "Not copying $CURRENT_HOME/.evcc/evcc.db to $EVCC_HOME/evcc.db, since there is"
echo "already a database there."
echo "Either delete one of the databases or run 'touch $COPIED_FLAG' to keep both,"
echo "then restart installation."
echo "Hint: usually the larger one is the one to keep."
ls -la "$CURRENT_HOME/.evcc/evcc.db" "$EVCC_HOME/evcc.db"
echo "--------------------------------------------------------------------------------"
exit 1
else
cp -Rp "$CURRENT_HOME"/.evcc/evcc.db "$EVCC_HOME"
fi
chown "$EVCC_USER:$EVCC_GROUP" "$EVCC_HOME/evcc.db"
touch "$COPIED_FLAG"
if [ -n "$(ls -A /etc/systemd/system/evcc.service.d 2> /dev/null)" ]; then
echo "--------------------------------------------------------------------------------"
echo "You have overrides defined in /etc/systemd/system/evcc.service.d."
echo "This update changes the evcc user to 'evcc' (from root) and the database file"
echo "to '/var/lib/evcc/evcc.db"
echo "Make sure that you neither override 'User' nor 'ExecStart'"
echo "Hint: you can delete all overrides with 'systemctl revert evcc'"
echo "As a precaution, evcc is not started even if it was previously started."
echo "--------------------------------------------------------------------------------"
rm -f "$RESTART_FLAG_FILE"
else
echo "--------------------------------------------------------------------------------"
echo "NOTE: evcc user has changed from $CURRENT_USER to $EVCC_USER, db has been copied to new"
echo "directory $EVCC_HOME/evcc.db, old db in $CURRENT_USER/.evcc has been retained."
echo "--------------------------------------------------------------------------------"
fi
fi
return 0
}
if [ "$1" = "install" ] || [ "$1" = "upgrade" ]; then
if [ -d /run/systemd/system ] && /bin/systemctl status evcc.service > /dev/null 2>&1; then
deb-systemd-invoke stop evcc.service >/dev/null || true
touch "$RESTART_FLAG_FILE"
deb-systemd-invoke stop evcc.service > /dev/null || true
touch "$RESTART_FLAG_FILE"
fi
if ! getent group "$EVCC_GROUP" > /dev/null 2>&1; then
addgroup --system "$EVCC_GROUP" --quiet
fi
if ! getent passwd "$EVCC_USER" > /dev/null 2>&1; then
adduser --quiet --system --ingroup "$EVCC_GROUP" \
--disabled-password --shell /bin/false \
--gecos "evcc runtime user" --home "$EVCC_HOME" "$EVCC_USER"
chown -R "$EVCC_USER:$EVCC_GROUP" "$EVCC_HOME"
adduser --quiet "$EVCC_USER" dialout
else
adduser --quiet "$EVCC_USER" dialout
homedir=$(getent passwd "$EVCC_USER" | cut -d: -f6)
if [ "$homedir" != "$EVCC_HOME" ]; then
mkdir -p "$EVCC_HOME"
chown "$EVCC_USER:$EVCC_GROUP" "$EVCC_HOME"
process=$(pgrep -u "$EVCC_USER") || true
if [ -z "$process" ]; then
usermod -d "$EVCC_HOME" "$EVCC_USER"
if [ -f "$homedir/.evcc/evcc.db" ]; then
cp "$homedir/.evcc/evcc.db" "$EVCC_HOME" && touch "$homedir/.evcc/.copiedToEvccUser"
fi
else
echo "--------------------------------------------------------------------------------"
echo "Warning: evcc's home directory is incorrect ($homedir)"
echo "but can't be changed because at least one other process is using it."
echo "Stop offending process(es), then restart installation."
echo "Hint: You can list the offending processes using 'pgrep -u $EVCC_USER -a'"
echo "Note that you should NOT use the evcc user as login user, since that will"
echo "inevitably lead to this error."
echo "in that case, please create a different user as login user."
echo "--------------------------------------------------------------------------------"
exit 1
fi
fi
fi
if ! getent group "$EVCC_GROUP" > /dev/null 2>&1 ; then
addgroup --system "$EVCC_GROUP" --quiet
fi
if ! getent passwd "$EVCC_USER" > /dev/null 2>&1 ; then
adduser --quiet --system --ingroup "$EVCC_GROUP" \
--disabled-password --shell /bin/false \
--gecos "evcc runtime user" --home "$EVCC_HOME" "$EVCC_USER"
chown -R "$EVCC_USER:$EVCC_GROUP" "$EVCC_HOME"
adduser --quiet "$EVCC_USER" dialout
else
adduser --quiet "$EVCC_USER" dialout
homedir=$(getent passwd "$EVCC_USER" | cut -d: -f6)
if [ "$homedir" != "$EVCC_HOME" ]; then
mkdir -p "$EVCC_HOME"
chown "$EVCC_USER:$EVCC_GROUP" "$EVCC_HOME"
process=$(pgrep -u "$EVCC_USER") || true
if [ -z "$process" ]; then
usermod -d "$EVCC_HOME" "$EVCC_USER"
if [ -f "$homedir/.evcc/evcc.db" ]; then
cp "$homedir/.evcc/evcc.db" "$EVCC_HOME" && touch "$homedir/.evcc/.copiedToEvccUser"
fi
else
echo "--------------------------------------------------------------------------------"
echo "Warning: evcc's home directory is incorrect ($homedir)"
echo "but can't be changed because at least one other process is using it."
echo "Stop offending process(es), then restart installation."
echo "Hint: You can list the offending processes using 'pgrep -u $EVCC_USER -a'"
echo "Note that you should NOT use the evcc user as login user, since that will"
echo "inevitably lead to this error."
echo "in that case, please create a different user as login user."
echo "--------------------------------------------------------------------------------"
exit 1
fi
fi
fi
fi
if [ "$1" = "upgrade" ]; then
copyDbToUserDir
copyDbToUserDir
fi
exit 0
exit 0

View file

@ -2,5 +2,5 @@
set -e
if [ -d /run/systemd/system ] && [ "$1" = remove ]; then
deb-systemd-invoke stop evcc.service >/dev/null || true
deb-systemd-invoke stop evcc.service > /dev/null || true
fi