#!/bin/sh
#
# $NetBSD: motd,v 1.5 2000/09/19 13:04:38 lukem Exp $
# $FreeBSD: src/etc/rc.d/motd,v 1.6 2003/06/30 22:06:26 mtm Exp $
#

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

. /etc/rc.subr

name="motd"
rcvar="update_motd"
start_cmd="motd_start"
stop_cmd=":"

motd_start()
{
	#	Update kernel info in /etc/motd
	#	Must be done *before* interactive logins are possible
	#	to prevent possible race conditions.
	#
	local target="/etc/motd"
	local old new exp temp

	echo "Updating motd."
	if [ ! -f "${target}" ]; then
		install -o root -g wheel -m 0644 /dev/null ${target}
	fi

	old=$(awk '
	NR == 1 {
		if ($1 == "DragonFly") { print } else { exit 1 }
	}' < ${target})
	if [ $? -ne 0 ]; then
		return  # custom motd
	fi

	exp='s@([^#]*) #(.* [1-2][0-9]{3}).*/([^/ ]+) *$@\1 (\3) #\2@'
	new=$(uname -v | sed -E -e "${exp}")
	if [ "${old}" != "${new}" ]; then
		temp=$(mktemp -t motd)
		printf '%s\n' "${new}" > ${temp}
		tail -n +2 ${target} >> ${temp}
		cat ${temp} > ${target}
		rm -f ${temp}
	fi
}

load_rc_config $name
run_rc_command "$1"
