From 3f6f9973eb78c707454b53a26dbe746f8667a924 Mon Sep 17 00:00:00 2001 From: C2N14 Date: Sun, 9 Jun 2019 23:49:54 -0600 Subject: [PATCH] try to fix SUDO_USER dilemma & add support for non-root installation --- .../lib/installation_files/autostart.desktop | 7 ++ .../lib/installation_files/sun-times.service | 8 ++ .../lib/installation_files/sun-times.timer | 9 ++ install_scripts/postinst.sh | 108 +++++++++++------- install_scripts/preremove.sh | 48 ++++++-- 5 files changed, 126 insertions(+), 54 deletions(-) create mode 100644 automathemely/lib/installation_files/autostart.desktop create mode 100644 automathemely/lib/installation_files/sun-times.service create mode 100644 automathemely/lib/installation_files/sun-times.timer diff --git a/automathemely/lib/installation_files/autostart.desktop b/automathemely/lib/installation_files/autostart.desktop new file mode 100644 index 0000000..f9c3bb1 --- /dev/null +++ b/automathemely/lib/installation_files/autostart.desktop @@ -0,0 +1,7 @@ +[Desktop Entry] +Type=Application +Name=AutomaThemely +Comment=Python application for changing between desktop themes periodically. +Exec=/usr/bin/env python3 /bin/autothscheduler.py +Icon=automathemely +Name[en_US]=autostart.desktop diff --git a/automathemely/lib/installation_files/sun-times.service b/automathemely/lib/installation_files/sun-times.service new file mode 100644 index 0000000..fba3a15 --- /dev/null +++ b/automathemely/lib/installation_files/sun-times.service @@ -0,0 +1,8 @@ +[Unit] +Description=Update automathemely sun times daily +After=network-online.target +Wants=network-online.target + +[Service] +Type=oneshot +ExecStart=/bin/bash /bin/systemd-trigger.sh "/.config/automathemely/sun_times" "/usr/bin/env python3 /autoth_tools/updsuntimes.py" diff --git a/automathemely/lib/installation_files/sun-times.timer b/automathemely/lib/installation_files/sun-times.timer new file mode 100644 index 0000000..89fcafc --- /dev/null +++ b/automathemely/lib/installation_files/sun-times.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Update automathemely sun times daily + +[Timer] +OnCalendar=daily +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/install_scripts/postinst.sh b/install_scripts/postinst.sh index 9a679d1..8adec2d 100755 --- a/install_scripts/postinst.sh +++ b/install_scripts/postinst.sh @@ -1,63 +1,83 @@ #!/bin/bash -USER_HOME="$(getent passwd $SUDO_USER | cut -d: -f6)" -packdir="$(sudo -u ${SUDO_USER} python3 -m pip show automathemely | grep -oP "^Location: \K.*")/automathemely" +packdir="$(python3 -m pip show automathemely | grep -oP "^Location: \K.*")/automathemely" if_not_dir_create () { - if [ ! -d "$1" ]; then + if [[ ! -d "$1" ]]; then mkdir -p "$1" fi } -# Autostart HEREDOC -autostart_path="/etc/xdg/autostart/automathemely.desktop" -if_not_dir_create "$(dirname "$autostart_path")" -tee "/etc/xdg/autostart/automathemely.desktop" < /dev/null -[Desktop Entry] -Type=Application -Name=AutomaThemely -Comment=Python application for changing between desktop themes periodically. -Exec=/usr/bin/env python3 ${packdir}/bin/autothscheduler.py -Icon=automathemely -EOF - -# Timer & service HEREDOCS -systemd_user_path="/usr/lib/systemd/user" -if_not_dir_create "$systemd_user_path" -tee "$systemd_user_path/automathemely.timer" < /dev/null -[Unit] -Description=Update automathemely sun times daily +local_install=false +## CHECK FOR ROOT PRIVILEGES +if ((${EUID:-0} || "$(id -u)")); then + + if [[ -z "$SUDO_USER" ]]; then + echo "SUDO_USER variable not found, trying to find user manually..." + SUDO_USER=$(logname) + SUDO_UID=$(id -u ${SUDO_USER}) + + if [[ -z "$SUDO_USER" ]]; then + echo "Could not find SUDO_USER, falling back to local only installation..." + local_install=true + fi + + fi + +else + echo "Root privileges not detected, falling back to local only installation..." + local_install=true + +fi + + +## SET RELEVANT VARIABLES +if [[ "$local_install" = true ]]; then + autostart_path="$HOME/.config/autostart" + systemd_user_path="$HOME/.config/systemd/user" + echo "NOTE: AutomaThemely is disabled from autostarting by default on local installation" + +else + autostart_path="/etc/xdg/autostart" + systemd_user_path="/usr/lib/systemd/user" + +fi + + +## MOVE FILES TO LOCATIONS +# Autostart on user log in +if_not_dir_create "$autostart_path" +cp "$packdir/installation_files/autostart.desktop" "$autostart_path/automathemely.desktop" +sed -i "s||$packdir|g" "$autostart_path/automathemely.desktop" + +# Timer & service units +if_not_dir_create "$systemd_user_path" +cp "$packdir/installation_files/sun-times.timer" "$systemd_user_path/automathemely.timer" +cp "$packdir/installation_files/sun-times.service" "$systemd_user_path/automathemely.service" +sed -i "s||$packdir|g" "$systemd_user_path/automathemely.service" -[Timer] -OnCalendar=daily -Persistent=true -[Install] -WantedBy=timers.target -EOF +## ENABLE SYSTEMD +if [[ "$local_install" = true ]]; then + systemctl --user daemon-reload + systemctl --user enable automathemely.timer + systemctl --user start automathemely.timer -tee "$systemd_user_path/automathemely.service" < /dev/null -[Unit] -Description=Update automathemely sun times daily -After=network-online.target -Wants=network-online.target +else + # Export some vars required to make some systemd user commands work + export XDG_RUNTIME_DIR="/run/user/${SUDO_UID}" + export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" -[Service] -Type=oneshot -ExecStart=/bin/bash ${packdir}/bin/systemd-trigger.sh "/.config/automathemely/sun_times" "/usr/bin/env python3 ${packdir}/autoth_tools/updsuntimes.py" -EOF + sudo -E -u ${SUDO_USER} systemctl --user --global daemon-reload + sudo systemctl --global enable automathemely.timer + sudo -E -u ${SUDO_USER} systemctl --user --global start automathemely.timer -# Export some vars required to make some systemd user commands work -export XDG_RUNTIME_DIR="/run/user/${SUDO_UID}" -export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" +fi -sudo -E -u $SUDO_USER systemctl --user --global daemon-reload -sudo systemctl --user --global enable automathemely.timer -sudo -E -u $SUDO_USER systemctl --user --global start automathemely.timer -# REMOVE OBSOLETE STUFF +## REMOVE OBSOLETE STUFF # Crontab removal # If crontabs are not installed carries on -(crontab -u $SUDO_USER -l | awk ' !/sunrise_change_theme/ && !/sunset_change_theme/ && !/update_sunhours_daily/ && !/update_sunhours_reboot/ { print }' | crontab -u $SUDO_USER -) || true +(crontab -u ${SUDO_USER} -l | awk ' !/sunrise_change_theme/ && !/sunset_change_theme/ && !/update_sunhours_daily/ && !/update_sunhours_reboot/ { print }' | crontab -u ${SUDO_USER} -) || true diff --git a/install_scripts/preremove.sh b/install_scripts/preremove.sh index 816cfec..e42defb 100755 --- a/install_scripts/preremove.sh +++ b/install_scripts/preremove.sh @@ -1,15 +1,43 @@ #!/bin/bash -USER_HOME=$(getent passwd $SUDO_USER | cut -d: -f6) -rm -rf "$USER_HOME/.config/automathemely" -rm -rf "/etc/xdg/autostart/automathemely.desktop" - pkill -f "autothscheduler.py" -# Export some vars required to make some systemd user commands work -export XDG_RUNTIME_DIR="/run/user/${SUDO_UID}" -export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" -sudo -E -u $SUDO_USER systemctl --user --global stop automathemely.timer -sudo systemctl --user --global disable automathemely.timer -sudo -E -u $SUDO_USER systemctl --user --global daemon-reload +local_install=false +## CHECK FOR ROOT PRIVILEGES +if ((${EUID:-0} || "$(id -u)")); then + + if [[ -z "$SUDO_USER" ]]; then + echo "SUDO_USER variable not found, trying to find user manually..." + SUDO_USER=$(logname) + SUDO_UID=$(id -u ${SUDO_USER}) + + if [[ -z "$SUDO_USER" ]]; then + echo "Could not find SUDO_USER, trying to remove local installation..." + local_install=true + fi + + fi + +else + echo "Root privileges not detected, trying to remove local installation..." + local_install=true + +fi + + +if [[ "$local_install" = true ]]; then + systemctl --user stop automathemely.timer + systemctl --user disable automathemely.timer + systemctl --user daemon-reload + +else + # Export some vars required to make some systemd user commands work + export XDG_RUNTIME_DIR="/run/user/${SUDO_UID}" + export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" + + sudo -E -u ${SUDO_USER} systemctl --user --global stop automathemely.timer + sudo systemctl --global disable automathemely.timer + sudo -E -u ${SUDO_USER} systemctl --user --global daemon-reload + +fi