#!/sbin/openrc-run
# Runs once, on the first boot of an installed machine, and then never again:
# it sets up the database, writes Asterisk's configuration, creates the first
# administrator and starts everything. The result is in /root/nuxpbx-welcome.

description="Turns a freshly installed machine into a phone system, once"

depend() {
	need net localmount
	before nuxpbx asterisk
}

start() {
	if [ -e /etc/nuxpbx/installed ]; then
		return 0
	fi
	# Running from the ISO: nothing is on a disk yet, and a database set up
	# in memory would vanish with the next reboot. nuxpbx-setup is the way.
	if grep -qE '^[^ ]+ / (tmpfs|rootfs|overlay) ' /proc/mounts; then
		einfo "running from the live media, nothing to set up; run nuxpbx-setup to install"
		return 0
	fi
	ebegin "Setting up the phone system for the first time"
	/usr/sbin/nuxpbx-firstboot >>/var/log/nuxpbx-firstboot.log 2>&1
	eend $? "failed, see /var/log/nuxpbx-firstboot.log"
}
