#!/bin/bash

. /etc/sysconfig/mythtv-status

if [ x$UPDATEMOTD != xyes ]
then
  exit
fi

# Check if stub exists. If not, assume motd is OK to use as stub
if [ ! -f /etc/motd.stub ]; then
 if [ -f /etc/motd ]; then
  cp -a /etc/motd /etc/motd.stub
 else # No motd exists, create empty stub file
  touch /etc/motd.stub
 fi
fi

# Update motd:
if [ -f /etc/motd.stub ]; then
 # Insert stub
 cp /etc/motd.stub /tmp/motd.new
fi

# Add mythtv status to end of stub
if mythtv-status $ARGS -h $HOST >> /tmp/motd.new
then
  cp /tmp/motd.new /etc/motd # If succesful, replace motd
fi

if [ -f /tmp/motd.new ]; then
 rm /tmp/motd.new
fi
