#!/bin/sh
set -e
if [ "$1" = configure ]; then
    if ! getent passwd sticky-notes >/dev/null; then
        useradd --system --user-group --home-dir /var/lib/sticky-notes-server \
            --no-create-home --shell /usr/sbin/nologin sticky-notes
    fi
    install -d -m 700 -o sticky-notes -g sticky-notes /var/lib/sticky-notes-server
    if [ -d /run/systemd/system ]; then
        systemctl daemon-reload >/dev/null || true
        # Restart an already enabled server so it runs the new version.
        if systemctl is-active --quiet sticky-notes-server; then
            systemctl restart sticky-notes-server || true
        fi
    fi
    if ! systemctl is-enabled --quiet sticky-notes-server 2>/dev/null; then
        echo 'Sticky Notes sync server installed but not started. To run it now and at every boot:'
        echo '  sudo systemctl enable --now sticky-notes-server'
        echo 'Then get a pairing code with: sudo sticky-notes-server pair'
    fi
fi
exit 0
