#!/bin/sh
set -e
UNIT=backgroundsync.service
DROPIN_DIR=/etc/systemd/system/${UNIT}.d
DROPIN=${DROPIN_DIR}/user.conf
POLKIT_RULE=/etc/polkit-1/rules.d/50-backgroundsync.rules

installing_user()
{
    if [ -n "${SUDO_USER}" ] && [ "${SUDO_USER}" != root ]; then
        echo "${SUDO_USER}"
    elif [ -n "${PKEXEC_UID}" ] && [ "${PKEXEC_UID}" != 0 ]; then
        getent passwd "${PKEXEC_UID}" | cut -d: -f1
    fi
}

write_service_user()
{
    mkdir -p "${DROPIN_DIR}"
    printf '[Service]\nUser=%s\nGroup=%s\n' "$1" "$(id -gn "$1")" > "${DROPIN}"
}

service_user()
{
    sed -n 's/^User=//p' "${DROPIN}" | head -n 1
}

# The service user may start and stop the service from the program without an
# administrator password - only this unit, only these verbs.
write_polkit_rule()
{
    [ -d /etc/polkit-1/rules.d ] || return 0
    cat > "${POLKIT_RULE}" <<EOF
// Written by the autofilesync package: the user the service runs as may start
// and stop it from the program without an administrator password.
polkit.addRule(function(action, subject) {
    if (action.id == "org.freedesktop.systemd1.manage-units" &&
        action.lookup("unit") == "${UNIT}" &&
        subject.user == "$1" &&
        ["start", "stop", "restart"].indexOf(action.lookup("verb")) >= 0)
        return polkit.Result.YES;
});
EOF
    chmod 644 "${POLKIT_RULE}"
}

update_service()
{
    [ -d /run/systemd/system ] || return 0
    systemctl daemon-reload
    if [ -z "$1" ] && [ -f "${DROPIN}" ]; then
        systemctl enable "${UNIT}" || true
    fi
    if systemctl -q is-enabled "${UNIT}"; then
        systemctl restart "${UNIT}" || true  # restart, not start: an already running daemon still executes the replaced binary
    fi
}

if [ "$1" = configure ]; then
    if [ ! -f "${DROPIN}" ]; then
        user="$(installing_user)"
        [ -n "${user}" ] && write_service_user "${user}"
    fi
    [ -f "${DROPIN}" ] && write_polkit_rule "$(service_user)"
    update_service "$2"
    if [ ! -f "${DROPIN}" ]; then
        echo "backgroundsync: kein Benutzer fuer den Hintergrunddienst bekannt. Einrichten mit:"
        echo "  sudo mkdir -p ${DROPIN_DIR}"
        echo "  printf '[Service]\\nUser=<benutzer>\\nGroup=<gruppe>\\n' | sudo tee ${DROPIN}"
        echo "  sudo systemctl daemon-reload && sudo systemctl enable --now ${UNIT}"
    fi
fi
exit 0
