# 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 [[ "svn" = "svn" ]]
  then
    if [[ -n "" ]]
    then
      echo "Illegal argument for svn: branch ''."
      exit 1
    fi
    if [[ -d apache-source ]] && [[ ! -d apache-source/.svn ]]
    then
      rm -rf apache-source
    fi
    if [[ ! -d apache-source ]]
    then
      svn co https://svn.apache.org/repos/asf/hive/trunk 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 [[ "svn" = "git" ]]
  then
     if [[ -z "" ]]
    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 https://svn.apache.org/repos/asf/hive/trunk 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  || git checkout -b  origin/
    git reset --hard origin/
    git merge --ff-only origin/
    #git gc
    date +"%Y-%m-%d %T.%3N"
  else
    echo "Unknown repository type 'svn'"
    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 [[ "maven" == "maven" ]]
  then
    rm -rf /some/working/dir/maven/org/apache/hive
        echo "Compile check on additional Profiles: hadoop-1"
    export ADDITIONAL_PROFILES=hadoop-1
    for i in $(echo $ADDITIONAL_PROFILES | tr "," "\n")
      do
        mvn clean install -DskipTests -P$i;
        if [[ "-d itests" ]]
        then
          cd itests
          mvn clean install -DskipTests -P$i;
        cd ..
        fi
      done
        mvn -B clean install -DskipTests -T 4 -q -Dmaven.repo.local=/some/working/dir/maven $mavenArgs $mavenBuildArgs
    if [[ -d "itests" ]]
    then
      cd itests
      mvn -B clean install -DskipTests -T 4 -q -Dmaven.repo.local=/some/working/dir/maven $mavenArgs $mavenBuildArgs
    fi
  elif [[ "maven" == "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 maven"
     exit 127
   fi
   date +"%Y-%m-%d %T.%3N"
) 2>&1 | tee /some/log/dir/source-prep.txt
exit ${PIPESTATUS[0]}
