#!/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:

# Insert stub
cp /etc/motd.stub /tmp/motd.new
# Add mythtv status to end of stub
ret=0
mythtv-status $ARGS -h $HOST >> /tmp/motd.new || ret=$?
if [ $ret -eq 0 -o $ret -eq 1 ]; then
  [ -f /tmp/motd.new ] && mv /tmp/motd.new /etc/motd
fi
