#!/bin/sh -e

###########################################################################
# /usr/sbin/update-motd
#
# A convenient wrapper for running the scripts located in
# /etc/update-motd.d and concatenating their output to the Message of the
# Day file (usually linked to /etc/motd)
#
# Copyright (C) 2008 Canonical Ltd.
#  * Original: Aug 2008 - Dustin Kirkland <kirkland@canonical.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL'.
###########################################################################

NAME="update-motd"
NEW=/var/run/motd.new
SKEL=/etc/motd.tail
REAL=/var/run/motd
DIR=/etc/$NAME.d
DISABLED=/var/lib/$NAME/disabled
LASTRUN=/var/run/$NAME.lastrun
FORCE=0

if [ -f "$NEW" ]; then
	# If /var/run/motd.new exists, another instance is running
	echo "Another update-motd is running ($NEW exists)" 1>&2
	exit 0
fi

usage() {
	echo "Usage:

  update-motd [--disable|--enable|--force]

    --disable - will prevent update-motd from running; useful for
                temporarily disabling automatic updates of /etc/motd
                by the /etc/cron.d/update-motd cronjob.
    --enable  - will allow update-motd to run; useful for enabling
                automatic updates of /etc/motd through the
                /etc/cron.d/update-motd cronjob.
    --force   - will override a disabled update-motd for a single
                update of /etc/motd.
" 1>&2
}

FORCE=0
for arg in $@; do
	case $arg in
		"--disable"|"-d")
			touch "$DISABLED"
			echo "$NAME is now disabled." 1>&2
			# Regenerate motd created at boot by
			# /etc/init.d/bootmisc.sh
			uname -snrvm > $REAL
			[ -f $SKEL ] && cat $SKEL >> $REAL
			exit 0
		;;
		"--enable"|"-e")
			rm -f "$DISABLED"
			echo "$NAME is now enabled." 1>&2
			# Do not exit here, as we want to update the MOTD
			# immediately upon enabling
		;;
		"--force"|"-f")
			FORCE=1
		;;
		*)
			usage
			exit 1
		;;
	esac
done

if [ -f "$DISABLED" ] && [ "$FORCE" != "1" ]; then
	echo "$NAME is currently disabled.
You might try:
  * update-motd --enable
  * update-motd --force" 1>&2
	exit 1
fi

# Make sure we clean up the motd.new lock file if we exit for any reason
trap "rm -f $NEW 2>/dev/null || true" EXIT HUP INT QUIT TERM

# Start with the motd header
# This is identical to the motd-building code used in
#   /etc/init.d/bootmisc.sh
uname -snrvm > $NEW
[ -f $SKEL ] && cat $SKEL >> $NEW

# Run each script, appending to the new motd
run-parts --lsbsysinit $DIR >>$NEW 2>/dev/null

# Move the new motd into place
mv -f $NEW $REAL

# Write out the current timestamp to the lastrun file
echo -n "Last run completed: " > "$LASTRUN"
date >> "$LASTRUN"
