#!/bin/bash
set -e

if ! id -u party >/dev/null 2>&1; then
    useradd --system --no-create-home --shell /bin/false party
fi

chown -R party:party /usr/share/party

if [ ! -f /etc/party/environment ]; then
    mkdir -p /etc/party
    cat > /etc/party/environment <<'EOF'
PARTY_DB_DSN=postgres://party:secret@localhost/party?sslmode=disable
SMTP_USERNAME=
SMTP_PASSWORD=
APNS_KEY_PATH=
APNS_KEY_ID=
APNS_TEAM_ID=
APNS_BUNDLE_ID=
EOF
    chmod 640 /etc/party/environment
    chown root:party /etc/party/environment
    echo "NOTE: Edit /etc/party/environment before the service can start."
fi

systemctl daemon-reload
systemctl enable party.service
systemctl restart party.service

systemctl reload caddy 2>/dev/null || true
