# 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 -e
set -x
date +"%Y-%m-%d %T.%3N"
if [[ -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 -Dhttp.proxyHost=somehost -Dhttp.proxyPort=3128"
export MAVEN_OPTS="-Xmx1g ${mavenEnvOpts}"
cd /some/working/dir/
(
  if [[ "true" == "true" ]]
  then
    rm -rf ivy maven
  fi
  mkdir -p maven ivy
  if [[ "git" = "svn" ]]
  then
    if [[ -n "branch-1" ]]
    then
      echo "Illegal argument for svn: branch 'branch-1'."
      exit 1
    fi
    if [[ -d apache-source ]] && [[ ! -d apache-source/.svn ]]
    then
      rm -rf apache-source
    fi
    if [[ ! -d apache-source ]]
    then
      svn co git:///repo1 apache-source
    fi
    cd apache-source
    svn revert -R .
    rm -rf $(svn status --no-ignore | egrep -v '^X|^Performing status on external' | awk '{print $2}')
    svn update
  elif [[ "git" = "git" ]]
  then
     if [[ -z "branch-1" ]]
    then
      echo "Illegal argument for git: branch name is required."
      exit 1
    fi
    if [[ -d apache-source ]] && [[ ! -d apache-source/.git ]]
    then
      rm -rf apache-source
    fi
    if [[ ! -d apache-source ]]
    then
      git clone git:///repo1 apache-source
    fi
    date +"%Y-%m-%d %T.%3N"
    cd apache-source
    git fetch origin
    git reset --hard HEAD && git clean -f -d
    git checkout branch-1 || git checkout -b branch-1 origin/branch-1
    git reset --hard origin/branch-1
    git merge --ff-only origin/branch-1
    #git gc
    date +"%Y-%m-%d %T.%3N"
  else
    echo "Unknown repository type 'git'"
    exit 1
  fi
  patchCommandPath=/some/working/dir/scratch/smart-apply-patch.sh
  patchFilePath=/some/working/dir/scratch/build.patch
  if [[ -f $patchFilePath ]]
  then
    chmod +x $patchCommandPath
    $patchCommandPath $patchFilePath
  fi
  if [[ "${buildTool}" == "maven" ]]
  then
    rm -rf /some/working/dir/maven/org/apache/hive
        mvn -B clean install -DskipTests -T 4 -q -Dmaven.repo.local=/some/working/dir/maven -X -Phadoop-2
    if [[ -d "itests" ]]
    then
      cd itests
      mvn -B clean install -DskipTests -T 4 -q -Dmaven.repo.local=/some/working/dir/maven -X -Phadoop-2
    fi
  elif [[ "${buildTool}" == "ant" ]]
  then
    ant -Dant=arg1 -Divy.default.ivy.user.dir=/some/working/dir/ivy \
      -Dmvn.local.repo=/some/working/dir/maven clean package test \
      -Dtestcase=nothing
   else
     echo "Unknown build tool ${buildTool}"
     exit 127
   fi
   date +"%Y-%m-%d %T.%3N"
) 2>&1 | tee /some/log/dir/source-prep.txt
exit ${PIPESTATUS[0]}
