#!/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 -ex

. `dirname $0`/bigtop.bom

usage() {
    echo "
usage: $0 <options>
  Required not-so-options:
     --qfs-version=VERSION         version of qfs we are installing
     --python=PATH                 path to python

  Optional options:
     --python3=PATH                path to python 3
  "
    exit 1
}

OPTS=$(getopt \
    -n $0 \
    -o '' \
    -l 'qfs-version:' \
    -l 'python:' \
    -l 'python3:' \
    -- "$@")

if [ $? != 0 ] ; then
    usage
fi

eval set -- "$OPTS"
while true ; do
    case "$1" in
        --qfs-version)
            QFS_VERSION=$2 ; shift 2
            ;;
        --python)
            PYTHON_PATH=$2 ; shift 2
            ;;
        --python3)
            PYTHON3_PATH=$2 ; shift 2
            ;;
        --)
            shift ; break
            ;;
        *)
            echo "Unknown option: $1"
            usage
            exit 1
            ;;
    esac
done

for var in QFS_VERSION PYTHON_PATH ; do
    if [ -z "$(eval "echo \$$var")" ]; then
        echo Missing required param: $var
        usage
    fi
done

# bigtop installs maven on its own and not through the package manager
export PATH=/usr/local/maven/bin:$PATH
command -v mvn > /dev/null 2>&1 || {
    echo >&2 "Maven is required but not found in PATH. Check your installation."
    exit 1
}

PYTHON3_PATH=${PYTHON3_PATH:-}

# Workaround for BIGTOP-2710 
sed -ie '/which/!s#mvn #mvn -Duser.home=${HOME} #g' src/java/javabuild.sh

# Build and install QFS into the build/release directory
make

# Build QFS Java library JAR and Hadoop plugin JAR.
# NOTE(fsareshwala): we have src/java/javabuild.sh to do this for us, but that
# script does some git magic to determine the release numbers and hashes from
# git tags. However, in bigtop, we have an entirely different set of git objects
# and will get the wrong tag name and release hash if we use javabuild.sh as it
# currently is. Until qfs can update its build process upstream, we replicate
# what javabuild.sh would do.
get_hadoop_qfs_profile() {
    myversion="`echo "$HADOOP_VERSION" | cut -d. -f 1-2`"
    myversionmaj="`echo "$HADOOP_VERSION" | cut -d. -f 1`"

    if [ x"$myversion" = x"1.0"  -o  x"$myversion" = x"1.1" ]; then
        echo "hadoop_branch1_profile"
    elif [ x"$myversion" = x"0.23" ]; then
        echo "hadoop_trunk_profile"
    elif [  x"$myversionmaj" = x"2" ]; then
        echo "hadoop_trunk_profile,hadoop_trunk_profile_2"
    else
        echo "Unsupported Hadoop release version."
        exit 1
    fi
}

HADOOP_QFS_PROFILE=$(get_hadoop_qfs_profile)
cd src/java
mvn -Duser.home=${HOME} -Dqfs.release.version=${QFS_VERSION} --projects qfs-access package
mvn -P $HADOOP_QFS_PROFILE -Duser.home=${HOME} -Dqfs.release.version=${QFS_VERSION} \
    -Dhadoop.release.version=${HADOOP_VERSION} -Dtest.build.data=/tmp \
    package
cd ../..

# Build QFS Python libraries.
cd build/release
$PYTHON_PATH ../../src/cc/access/kfs_setup.py build
if [ ! -z "$PYTHON3_PATH" ]; then
    $PYTHON3_PATH ../../src/cc/access/kfs_setup.py build
fi
cd ..
