#!/bin/sh

SAVE_DIR=`pwd`
MY_DIR=$(cd "$(dirname "$(which "$0")")" && pwd)

RESULTS_DIR="$1"
REV_NUMBER=$2

if [ -f "$SAVE_DIR/dvm-tester.config" ]; then
    . "$SAVE_DIR/dvm-tester.config"
fi

if [ -z "$RECIPIENTS" ]; then
    RECIPIENTS="pritmick@yandex.ru"
fi

PUBL_PATH="$PUBL_BASE_PATH/r${REV_NUMBER}${PUBL_SUFFIX}"
COPY_PATHS="$PUBL_BASE_PATH/latest${PUBL_SUFFIX}"

ssh $PUBL_SERVER "mkdir -p \"$PUBL_PATH\""
scp "$RESULTS_DIR/report/full-report.html" "$PUBL_SERVER:$PUBL_PATH/index.html"
scp "$RESULTS_DIR/report/sources.tgz" "$PUBL_SERVER:$PUBL_PATH/sources.tgz"
for p in $COPY_PATHS; do
    ssh $PUBL_SERVER "rm -rf \"$p\"; cp -r \"$PUBL_PATH\" \"$p\""
done
create_email()
{
    echo "MIME-Version: 1.0"
    echo "Content-type: text/html;charset=UTF-8"
    echo "From: dvm@keldysh.ru"
    echo "To: $RECIPIENTS"
    echo "Subject: DVM tester: Test results for revision $REV_NUMBER"
    echo
    cat "$1"
    echo "."
    echo
}
create_email "$RESULTS_DIR/report/brief-report.html" | ssh $MAIL_SERVER "$SENDMAIL $RECIPIENTS"
