#!/bin/sh

REPORT_MAIL=false
REPORT_FILE=$(mktemp aptitude-run-mailreport.XXXXXXXXXX)
REPORT_RCPT=root

if [ -z "$REPORT_FILE" ]; then
    echo "ERROR: $0: Couldn't create temporary file in `pwd` using mktemp. Aborting." | \
        mail -s "aptitude-robot report on $(hostname) (ERROR)" $REPORT_RCPT
    exit 1
fi

# TODO: The following contains code very similar to
# xymon-report. Should be factored out.

cat >> $REPORT_FILE << EOF
Aptitude-Robot Report
=====================
EOF

#
# check for remaining upgrades
#
UPGRADES_FILE=$(mktemp aptitude-upgrades.XXXXXXXXX)
if [ -z "$UPGRADES_FILE" ]; then
    COLOR=red
    MSG="${MSG}ERROR: $(basename $0): Can't check for remaining upgrades:
Couldn't create temporary file in `pwd` using mktemp.
"
else
    aptitude search '~U !~ahold' > "$UPGRADES_FILE"
    if [ -s "$UPGRADES_FILE" ]; then
        REPORT_MAIL=true
        cat >> "$REPORT_FILE" << EOF

Remaining Upgrades
------------------
Some upgrades need to be looked into manually

EOF
        cat "$UPGRADES_FILE" >> "$REPORT_FILE"
    fi
    rm "$UPGRADES_FILE"
fi

#
# check for errors in log file
#
LOGFILE="$1"
if [ -f "$LOGFILE" ]; then
    if egrep -q -i '\bError\b|\bErr|\bE:' "$LOGFILE" ; then
        REPORT_MAIL=true
        cat >> "$REPORT_FILE" << EOF

Log File Errors
---------------
Please investigate the errors mentioned in the log file

EOF
    cat "$LOGFILE" >> "$REPORT_FILE"
    fi
else
    REPORT_MAIL=true
    cat >> "$REPORT_FILE" << EOF

Missing Log File
----------------
aptitude-robot produces no log file.  Please investigate.
EOF
fi

if [ $REPORT_MAIL = true ]; then
    mail -s "aptitude-robot report on $(hostname)" $REPORT_RCPT < "$REPORT_FILE"
fi
rm "$REPORT_FILE"
