#!/bin/sh
set -e
# Alpine (ADR-0116) equivalent of wake-postinstall.sh. Alpine's base image
# has no useradd/groupadd; adduser/addgroup are the busybox-native
# equivalents.

grep -q '^wake:' /etc/group 2>/dev/null || addgroup -S wake
id -u wake >/dev/null 2>&1 || adduser -S -D -H -s /sbin/nologin -h /var/db/wake -G wake wake

mkdir -p /var/db/wake/edge /var/db/wake/collector /run/wake
chown -R wake:wake /var/db/wake /run/wake

# Same entity-dump directory as wake-postinstall.sh (mmwake, rsyslog root).
mkdir -p /var/log/wake
chmod 0755 /var/log/wake

# ADR-0077: the live .thrt dir (mmenrich/mmwake CTI db + GeoIP mmdbs). Owned
# by wake so waked's asset-pipeline CTI fold and the GeoIP downloader (both
# running as `wake`) can write into it without root.
install -d -o wake -g wake -m 0750 /var/db/wake

# Restart only currently-running units — the OpenRC equivalent of systemd's
# try-restart (fresh installs no-op here since nothing is running yet).
# wakeweb ships disabled by default, so it naturally stays skipped too.
for _svc in waked-edge waked-collector wakeweb; do
    rc-service "${_svc}" status >/dev/null 2>&1 && rc-service "${_svc}" restart >/dev/null 2>&1 || true
done
