#!/bin/sh
set -e

# =============================================================================
# epc670-first-boot-setup
# Enables the notifier timer globally for all users.
# On a live install this is called directly by postinst.
# On an image build this runs as a one-shot service on first boot.
# =============================================================================

FIRST_BOOT_DONE="/var/lib/epc670-tofhat-utils/first-boot.done"
ENABLE_AUTORESOLUTION="/var/lib/epc670-tofhat-utils/autoresolution.enable"
TOF_STUDIO_SERVICE="epc-tof-studio.service"

log() {
  echo "epc670-first-boot-setup: $*"
  logger -t epc670-first-boot-setup "$*" || true
}

# -----------------------------------------------------------------------------
# Enable notifier timer globally for all users
# -----------------------------------------------------------------------------
log "Enabling epc670-notifier.timer globally"
systemctl --global enable epc670-notifier.timer || \
  log "Warning: could not enable epc670-notifier.timer globally"

# -----------------------------------------------------------------------------
# Disable autoresolution initially
# -----------------------------------------------------------------------------
if [ -f "$ENABLE_AUTORESOLUTION" ]; then
  epc670-enable-autoresolution
else
  epc670-disable-autoresolution
fi

log "Enabling and starting $TOF_STUDIO_SERVICE"
systemctl enable --now "$TOF_STUDIO_SERVICE" || \
  log "Warning: could not enable or start $TOF_STUDIO_SERVICE"
# -----------------------------------------------------------------------------
# Hint
# -----------------------------------------------------------------------------
log "Setup complete."
log "To set up the epc670 desktop environment for a user, run:"
log "  sudo epc670_setup_desktop [username]"

# -----------------------------------------------------------------------------
# Mark done and self-disable (only meaningful in the image-build path)
# -----------------------------------------------------------------------------
mkdir -p "$(dirname "$FIRST_BOOT_DONE")"
touch "$FIRST_BOOT_DONE"
systemctl disable epc670-first-boot-setup.service 2>/dev/null || true