#!/bin/bash
set -uo pipefail

# Finishes setting a classroom machine up: the things that need the
# network and the apt lock, and so cannot run from the package postinst.
#
# Run from ekklesia-classroom-provision.service at every boot. Each step
# checks whether it is already done and returns immediately if so, so a
# provisioned machine costs nothing, and a machine that was offline the
# first time gets another go at the next boot.
#
# Nothing here is allowed to fail the boot. A classroom with no DVD
# codecs is still a working classroom.

log() {
    echo "[classroom-provision] $1"
}

log "starting"

chrome_ok=true

if /usr/local/bin/classroom-chrome-setup; then
    :
else
    chrome_ok=false
    log "WARNING: Google Chrome is not installed yet."
    log "WARNING: Provisioning will retry automatically."
fi

# DVD codecs are only fetched for rooms that actually have a drive.
if /usr/local/bin/classroom-dvd-css-setup --if-enabled; then
    :
else
    log "WARNING: commercial DVD support is not set up. Unencrypted discs"
    log "WARNING: still play. Retrying at the next boot, or run:"
    log "WARNING: sudo classroom-dvd-css-setup"
fi

log "finished"

# Provisioning is best-effort by design.
# Chrome is required for a usable classroom. Returning failure here does
# not block boot; systemd retries this oneshot service in the background.
if [[ "$chrome_ok" != true ]]; then
    exit 1
fi

exit 0