#!/bin/sh
#
#  Licensed to the Apache Software Foundation (ASF) under one or more
#  contributor license agreements.  See the NOTICE file distributed with
#  this work for additional information regarding copyright ownership.
#  The ASF licenses this file to You under the Apache License, Version 2.0
#  (the "License"); you may not use this file except in compliance with
#  the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

# --------------------------------------------------------------------
# $Rev: 631018 $ $Date: 2008-02-25 17:13:03 -0500 (Mon, 25 Feb 2008) $
# --------------------------------------------------------------------

# -----------------------------------------------------------------------------

samplesLog=samples-$ts.log

SAMPLES_BRANCH=$1

MAIL_FILE=$TEST_DIR/$BRANCH_DIR/mail.txt
MAIL_LOG=$TEST_DIR/$BRANCH_DIR/mail.sample.log

function samples_send_mail() {
   subject=$1
   mailto=$2

   mail_ts=`date +%Y%m%d%H%M%S`
   remote_mail_file=${REMOTE_HOME}/${mail_ts}.samples.log
   scp $MAIL_LOG ${REMOTE_ACCOUNT}@${REMOTE_MACHINE}:$remote_mail_file
   ssh ${REMOTE_ACCOUNT}@${REMOTE_MACHINE} "mail -s '$subject' $mailto < $remote_mail_file; rm -f $remote_mail_file"
}

echo " " >> $MAIL_FILE
echo "Samples: $SAMPLES_BRANCH" >> $MAIL_FILE
echo "=================================" >> $MAIL_FILE
echo "Log: $BINARY_URL/$BRANCH_DIR/$dir/$samplesLog" >> $MAIL_FILE
echo " " >> $MAIL_FILE

cd $TEST_DIR
rm -rf samples 

svn co https://svn.apache.org/repos/asf/geronimo/samples/$SAMPLES_BRANCH samples
if [ $? -ne 0 ]
then
  echo "SVN checkout failed."
  subject="[Samples $SAMPLES_BRANCH]: SVN checkout failed"
  echo "SVN checkout failed." > $MAIL_LOG

  samples_send_mail "$subject" "$ADMIN_ADDR"
  exit
fi

cd samples
samplesrev=`svn info | grep Revision | awk -F: '{print $2}'`
samplesrev=${samplesrev#* }

echo "Samples at $samplesrev" >> $samplesLog
echo " " >> $samplesLog

mvn -e -B >> $samplesLog
scp $samplesLog  ${REMOTE_ACCOUNT}@${REMOTE_MACHINE}:$bin_dir
ssh ${REMOTE_ACCOUNT}@${REMOTE_MACHINE} "chmod -R g+w $bin_dir/$samplesLog"

grep "BUILD SUCCESSFUL" $samplesLog
if [ $? -eq 0 ]
then

  echo "Build status: OK" >> $MAIL_FILE

else

  echo "Build status: FAILED" >> $MAIL_FILE

fi

echo " " >> $MAIL_FILE
