post_install() {
	post_upgrade
}

post_upgrade() {
	echo "==> Ensuring KVMD users and groups ..."
	systemd-sysusers /usr/lib/sysusers.d/kvmd.conf

	# https://github.com/systemd/systemd/issues/13522
	for user in `grep '^u ' /usr/lib/sysusers.d/kvmd.conf | awk '{print $2}'`; do
		usermod --expiredate= $user >/dev/null
	done

	chown kvmd:kvmd /etc/kvmd/htpasswd
	chown kvmd-ipmi:kvmd-ipmi /etc/kvmd/ipmipasswd
	chown kvmd-vnc:kvmd-vnc /etc/kvmd/vncpasswd
	chmod 600 /etc/kvmd/*passwd

	chown kvmd /var/lib/kvmd/msd || true

	if [ ! -e /etc/kvmd/nginx/ssl/server.crt ]; then
		echo "==> Generating KVMD-Nginx certificate ..."
		kvmd-gencert --do-the-thing
	fi

	if [ ! -e /etc/kvmd/vnc/ssl/server.crt ]; then
		echo "==> Generating KVMD-VNC certificate ..."
		kvmd-gencert --do-the-thing --vnc
	fi

	for target in nginx vnc; do
		chown root:root /etc/kvmd/$target/ssl
		chown root:kvmd-$target /etc/kvmd/$target/ssl/*
		chmod 440 /etc/kvmd/$target/ssl/server.key
		chmod 444 /etc/kvmd/$target/ssl/server.crt
	done

	echo "==> Patching configs ..."
	[ ! -f /boot/config.txt ] || sed -i -e 's/^dtoverlay=pi3-disable-bt$/dtoverlay=disable-bt/g' /boot/config.txt
	[ ! -f /boot/config.txt ] || sed -i -e 's/^dtoverlay=dwc2$/dtoverlay=dwc2,dr_mode=peripheral/g' /boot/config.txt
	[ ! -f /etc/conf.d/rngd ] || (echo 'RNGD_OPTS="-o /dev/random -r /dev/hwrng -x jitter -x pkcs11 -x rtlsdr"' > /etc/conf.d/rngd)
}
