Skip to content

Commit

Permalink
try to fix SUDO_USER dilemma & add support for non-root installation
Browse files Browse the repository at this point in the history
  • Loading branch information
C2N14 committed Jun 10, 2019
1 parent 704bfa6 commit 3f6f997
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 54 deletions.
7 changes: 7 additions & 0 deletions automathemely/lib/installation_files/autostart.desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[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
Name[en_US]=autostart.desktop
8 changes: 8 additions & 0 deletions automathemely/lib/installation_files/sun-times.service
Original file line number Diff line number Diff line change
@@ -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 <PACKDIR>/bin/systemd-trigger.sh "/.config/automathemely/sun_times" "/usr/bin/env python3 <PACKDIR>/autoth_tools/updsuntimes.py"
9 changes: 9 additions & 0 deletions automathemely/lib/installation_files/sun-times.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Update automathemely sun times daily

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target
108 changes: 64 additions & 44 deletions install_scripts/postinst.sh
Original file line number Diff line number Diff line change
@@ -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" <<EOF > /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" <<EOF > /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>|$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>|$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" <<EOF > /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

48 changes: 38 additions & 10 deletions install_scripts/preremove.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3f6f997

Please sign in to comment.