#!/bin/sh
#
# $FreeBSD: src/etc/rc.d/motd,v 1.9.10.1.4.1 2009/04/15 03:14:26 kensmith Exp $
#

# PROVIDE: nanobsd-motd
# REQUIRE: mountcritremote
# BEFORE:  LOGIN

. /etc/rc.subr

name="nanobsd_motd"
rcvar="update_nanobsd_motd"
start_cmd="motd_start"
stop_cmd=":"

PERMS="644"

motd_start()
{
	#	Update kernel info in /etc/motd
	#	Must be done *before* interactive logins are possible
	#	to prevent possible race conditions.
	#
	echo -n 'Updating motd'
	if [ ! -f /etc/motd ]; then
		install -c -o root -g wheel -m ${PERMS} /dev/null /etc/motd
	fi

	if [ ! -w /etc/motd ]; then
		echo ' ... /etc/motd is not writable, update failed.'
		return
	fi

	T=`mktemp -t motd`
	uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T}
	awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T}

	cmp -s $T /etc/motd || {
                # XXX: Hack quick and dirty combine of /etc/rc.d/motd and /tools/savesshkeys
		trap "umount /cfg" 1 2 15 EXIT
		mount /cfg
		cp $T /cfg/motd
		chmod ${PERMS} /etc/motd
		umount /cfg
		trap 1 2 15 EXIT

		trap "mount -uro noatime /" 1 2 15 EXIT
		mount -uwo noatime /
		cp $T /etc/motd
		chmod ${PERMS} /etc/motd
                mount -uro noatime /
		trap 1 2 15 EXIT
	}
	rm -f $T

	echo .
}

load_rc_config $name
run_rc_command "$1"
