# CheckBox mini CI mailer.
#
# This waits for a "checkbox-sru-finished" event and then
# uses curl to post one of two possible log files
# to a configurable CGI URL. The CGI can do whatever it
# wants, such as emailing the report.
#
# An /etc/default/plainbox-ci-mailer config file
# with the SUBMIT_CGI variable defined is expected.

description     "CheckBox SRU mini CI mailer"

start on checkbox-sru-finished

task

env CHECKBOX_UPSTART_LOG=/var/log/upstart/checkbox-sru.log
env CHECKBOX_DESKTOP_LOG=/tmp/checkbox.log

pre-start script
    [ -f /etc/default/plainbox-ci-mailer ] || exit 1
    [ -x /usr/bin/curl ] || exit 1
end script

script
    . /etc/default/plainbox-ci-mailer
    [ -z "$SUBMIT_CGI" ] && exit 1
    RELEASE=$(lsb_release -ds)
    if [ -f $CHECKBOX_UPSTART_LOG ]; then
        MESSAGE=$CHECKBOX_UPSTART_LOG
        SUBJECT="CheckBox NG CI testing run for $RELEASE server"
   elif [ -f $CHECKBOX_DESKTOP_LOG ]; then
        MESSAGE=$CHECKBOX_DESKTOP_LOG
        SUBJECT="CheckBox NG CI testing run for $RELEASE desktop"
    else
        MESSAGE="Something failed and CheckBoxNG didn't even start."
        SUBJECT="FAILED CheckBoxNG CI testing run for $RELEASE"
    fi
    IP=$(ip addr show dev eth0 |grep "inet " |cut -f 6 -d " ")
    HOST=$(hostname)
    SUBJECT="$SUBJECT - $HOST $IP"
    if [ -f "$MESSAGE" ] ; then
	dpkg --list "checkbox*" "plainbox*" >> $MESSAGE
        curl -F subject="$SUBJECT" -F plainbox_output=@$MESSAGE $SUBMIT_CGI
    else
        curl -F subject="$SUBJECT" -F plainbox_output="$MESSAGE" $SUBMIT_CGI
    fi
end script
