#!/bin/bash
# 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.

set -x
date +"%Y-%m-%d %T.%3N"
umask 0022
echo $$
ps -e -o pid,pgrp,user,args
ps x -o  "%p %r %y %x %c "
chmod -R u+w /some/log/dir
rm -rf /some/log/dir
# makes /some/log/dir and /some/log/dir/tmp
mkdir -p /some/log/dir/tmp
if [[ -n "${javaHomeForTests}" ]]
then
  export JAVA_HOME=$javaHomeForTests
  export PATH=$JAVA_HOME/bin/:$PATH
elif [[ -n "/usr/java/jdk1.7" ]]
then
  export JAVA_HOME=/usr/java/jdk1.7
  export PATH=$JAVA_HOME/bin/:$PATH
fi
export ANT_OPTS="-Xmx1g -XX:MaxPermSize=256m -Djava.io.tmpdir=/some/log/dir/tmp -Dhttp.proxyHost=somehost -Dhttp.proxyPort=3128"
export MAVEN_OPTS="-Xmx256m -Djava.io.tmpdir=/some/log/dir/tmp ${mavenEnvOpts}"
export HADOOP_ROOT_LOGGER=INFO,console
export HADOOP_OPTS="-Dhive.log.dir=/some/log/dir -Dhive.query.id=hadoop -Djava.io.tmpdir=/some/log/dir/tmp"
cd /some/local/dir/instance-1/apache-source || exit 1
date +"%Y-%m-%d %T.%3N"
echo "Pre test cleanup"
if [[ -s batch.pid ]]
then
  while read pid
  do
    if kill -9 -$pid 2>/dev/null
    then
      echo "Killed process group $pid"
      sleep 1
    fi
  done < batch.pid
fi
echo "$$" > batch.pid
find ./ -name 'TEST-*.xml' -delete
date +"%Y-%m-%d %T.%3N"
echo "Pre test cleanup done"
ret=0
if [[ "maven" == "maven" ]]
then
  if [[ -z "$testModule" ]]
  then
    testModule=./
  fi
  pushd $testModule
  timeout 40m mvn -B test -Dmaven.repo.local=/some/local/dir/instance-1/maven \
    -Dant=arg1 $mavenTestArgs -Dtest=arg1 1>/some/log/dir/maven-test.txt 2>&1 </dev/null &
  date +"%Y-%m-%d %T.%3N"

  pid=$!

  popd
elif [[ "maven" == "ant" ]]
then
  timeout 40m ant testonly -Dtest.junit.output.format=xml \
    -Divy.default.ivy.user.dir=/some/local/dir/instance-1/ivy \
    -Divy.default.always.check.exact.revision=false \
    -Divy.cache.ttl.default=eternal -Divy.checkmodified=false \
    -Dmvn.local.repo=/some/local/dir/instance-1/maven \
    -Dant=arg1 -DgrammarBuild.notRequired=true -Dskip.javadoc=true -Dtest=arg1 1>/some/log/dir/ant-test.txt 2>&1 </dev/null &

  pid=$!

else
  echo "Unknown build tool maven"
  exit 127
fi
echo $pid >> batch.pid
wait $pid
ret=$?
date +"%Y-%m-%d %T.%3N"
mkdir /some/log/dir/logs
find ./ -type f -name 'TEST-*.xml' | \
xargs -I {} sh -c 'f=TEST-batch-1-$(basename {}); test -f /some/log/dir/$f && f=$f-$(uuidgen); mv {} /some/log/dir/$f'
find ./ -type f -name hive.log -o -name spark.log -o -name derby.log | \
  xargs -I {} sh -c 'f=$(basename {}); test -f /some/log/dir/logs/$f && f=$f-$(uuidgen); mv {} /some/log/dir/logs/$f'
find ./ -path "*/spark/work" | \
  xargs -I {} sh -c 'mv {} /some/log/dir/logs/spark-log'
find ./ -type f -name 'syslog*' | \
  xargs -I {} sh -c 'mkdir -p /some/log/dir/logs/syslogs; mv {} /some/log/dir/logs/syslogs'
date +"%Y-%m-%d %T.%3N"

if [[ -f /some/log/dir/.log ]]
then
  mv /some/log/dir/.log /some/log/dir/logs/dot.log
fi
exit $ret
