#!/bin/sh
#
# $FreeBSD$
#

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

. /etc/rc.subr

name="motd"
rcvar="update_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.
	#
	check_startmsgs && 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

	# Begin Isilon

	# On initial boot generate a basic Isilon motd file as a template.
	# The template is then managed by MCP so any updates to it will update
	# /etc/motd appropriately.
	if [ ! -f /etc/mcp/templates/motd ]; then
		# if we don't have a template, we shouldn't have /etc/motd either
		# bug 164876
		install -c -o root -g wheel -m ${PERMS} /dev/null /etc/motd
		TT=`mktemp -t motdtemplate`
		uname -s -r | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${TT}
		if [ -f /etc/ifs/isi_manufacturing_only ]; then
		    cat <<EOF >>${TT}
********************************************************************************
This node is running a manufacturing build of OneFS.
Before you can form a cluster, you must reimage this node with a generally
available version of OneFS 8.1. After reimaging this node, the standard OneFS
join process proceeds to create the cluster.  You can download OneFS from EMC
Online Support at https://support.emc.com/downloads/15209_Isilon-OneFS
********************************************************************************
EOF
		fi
		cp $TT /etc/mcp/templates/motd
		chown root:wheel /etc/mcp/templates/motd
		chmod 640 /etc/mcp/templates/motd

		rm -f $TT
	fi

	T=`mktemp -t motd`
	cat /etc/mcp/templates/motd > ${T}

	# append any custom MOTD message after the Isilon MOTD template.
	if [ -f /etc/mcp/override/motd ]; then
	    echo >> ${T}
	    cat /etc/mcp/override/motd >> ${T}
	fi

	if false; then	# Original FreeBSD lines.
	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}
	fi

	# End Isilon

	cmp -s $T /etc/motd || {
		cp $T /etc/motd
		chmod ${PERMS} /etc/motd
	}
	rm -f $T

	check_startmsgs && echo '.'
}

load_rc_config $name
run_rc_command "$1"
