#!/usr/bin/env 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.
#
shopt -s nocasematch

function help {
  echo " "
  echo "usage: ${0}"
  echo "    -v/--version=<version>   The version of the metron release. [Required]"
  echo "    -c/--candidate=<RC#>      Defines the Release Candidate. [Required]"
  echo "    -b/--bro=<bro version>   The version of the bro kafka plugin. [Optional]"
  echo "    -h/--help                Usage information."
  echo " "
  echo "example: "
  echo "    metron-rc-check --version=0.4.2 --candidate=RC2 --bro=0.1.0"
  echo " "
}

APACHE_REPO="https://dist.apache.org/repos/dist/"
METRON_DIST=${APACHE_REPO}"dev/metron/"
METRON_KEYS=${APACHE_REPO}"release/metron/KEYS"
# print help, if the user just runs this without any args
if [ "$#" -eq 0 ]; then
    help
    exit 1
fi

# handle command line options
for i in "$@"; do
  case $i in
    #
    # VERSION: The release version of Metron to validate.
    #
    #
    -v=*|--version=*)
    VERSION="${i#*=}"
    shift # past argument=value
    ;;

    #
    # RC: Defines the RC# to use
    #
    #   -c=RC2
    #   --candidate=RC2
    #
    -c=*|--candidate=*)
    CANDIDATE="${i#*=}"
    shift # past argument=value
    ;;

    #
    # END: Defines the last commit to inspect
    #
    #   -b=0.1.0
    #   --bro=0.1.0
    #
    -b=*|--bro=*)
    BRO="${i#*=}"
    shift # past argument=value
    ;;

    #
    # -h/--help
    #
    -h|--help)
    help
    exit 0
    shift # past argument with no value
    ;;

    #
    # Unknown option
    #
    *)
    UNKNOWN_OPTION="${i#*=}"
    echo "Error: unknown option: $UNKNOWN_OPTION"
    help
    ;;
  esac
done

# validation
if [ -z "$VERSION" ]; then
	echo "Missing -v/--version is is required"
	exit 1
fi
if [[ "$VERSION" =~ ^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2} ]]; then
  METRON_VERSION="$VERSION"
else
  echo "[ERROR] "$VERSION" may not be a valid version number"
  exit 1
fi

if [ -z "$CANDIDATE" ]; then
	echo "Missing -c/--candidate which is required"
	exit 1
fi

if [[ "$CANDIDATE" =~ ^RC[0-9]+ ]]; then
  RC=$(echo "$CANDIDATE" | tr '[:upper:]' '[:lower:]')
  UPPER_RC=$(echo "$CANDIDATE" | tr '[:lower:]' '[:upper:]')
elif [[ "$CANDIDATE" =~ ^[0-9]+ ]]; then
  RC=rc"$CANDIDATE"
  UPPER_RC=RC"$CANDIDATE"
else
  echo "[ERROR] invalid RC, valid is RC# or just #"
  exit 1
fi

# validating the bro plugin is not required
if [ -n "$BRO" ]; then
  if [[ "$BRO" =~ ^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2} ]]; then
    BRO_VERSION="$BRO"
  else
    echo "[ERROR] $BRO may not be a valid version number"
    exit 1
  fi
fi

echo "Metron Version $METRON_VERSION"
echo "Release Candidate $RC"

if [ -n "$BRO" ]; then
  echo "Bro Plugin Version $BRO_VERSION"
fi

METRON_RC_DIST="$METRON_DIST$METRON_VERSION-$UPPER_RC"
echo "Metron RC Distribution Root is $METRON_RC_DIST"

# working directory
WORK="$HOME/tmp/metron-$METRON_VERSION-$RC"

# handle tilde expansion
WORK="${WORK/#\~/$HOME}"

# warn the user if the working directory exists
if [ -d "$WORK" ]; then
  echo "[ERROR] Directory $WORK exists, please rename it and start over"
  exit 1
fi

if [ ! -d "$WORK" ]; then
  mkdir -p "$WORK"
fi
echo "Working directory $WORK"

METRON_ASSEMBLY="$METRON_RC_DIST/apache-metron_$METRON_VERSION-$RC.tar.gz"
METRON_ASSEMBLY_SIG="$METRON_ASSEMBLY.asc"


echo "Downloading $METRON_KEYS"
if ! wget -P "$WORK" "$METRON_KEYS" ; then
  echo "[ERROR] Failed to download $METRON_KEYS"
  exit 1
fi

echo "Downloading $METRON_ASSEMBLY"
if ! wget -P "$WORK" "$METRON_ASSEMBLY" ; then
  echo "[ERROR] Failed to download $METRON_ASSEMBLY"
  exit 1
fi

echo "Downloading $METRON_ASSEMBLY_SIG"
if ! wget -P "$WORK" "$METRON_ASSEMBLY_SIG" ; then
  echo "[ERROR] Failed to download $METRON_ASSEMBLY_SIG"
  exit 1
fi

if [ -n "$BRO" ]; then

  METRON_KAFKA_BRO_ASSEMBLY="$METRON_RC_DIST/apache-metron-bro-plugin-kafka_$BRO_VERSION-$RC.tar.gz"
  METRON_KAFKA_BRO_ASSEMBLY_ASC="$METRON_KAFKA_BRO_ASSEMBLY.asc"

  echo "Downloading $METRON_KAFKA_BRO_ASSEMBLY"
  if ! wget -P "$WORK" "$METRON_KAFKA_BRO_ASSEMBLY" ; then
    echo "[ERROR] Failed to download $METRON_KAFKA_BRO_ASSEMBLY"
    exit 1
  fi

  echo "Downloading $METRON_KAFKA_BRO_ASSEMBLY_ASC"
  if ! wget -P "$WORK" "$METRON_KAFKA_BRO_ASSEMBLY_ASC" ; then
    echo "[ERROR] Failed to download $METRON_KAFKA_BRO_ASSEMBLY_ASC"
    exit 1
  fi
fi

cd "$WORK" || exit 1
echo "importing metron keys"

if ! gpg --import KEYS ; then
  echo "[ERROR] failed to import KEYS"
  exit 1
fi

echo "Verifying Metron Assembly"
if ! gpg --verify ./"apache-metron_$METRON_VERSION-$RC.tar.gz.asc" "apache-metron_$METRON_VERSION-$RC.tar.gz" ; then
  echo "[ERROR] failed to verify Metron Assembly"
  exit 1
fi

if [ -n "$BRO" ]; then

  echo "Verifying Bro Kafka Plugin Assembly"
  if ! gpg --verify ./"apache-metron-bro-plugin-kafka_$BRO_VERSION-$RC.tar.gz.asc" "apache-metron-bro-plugin-kafka_$BRO_VERSION-$RC.tar.gz" ; then
    echo "[ERROR] failed to verify Bro Kafka Plugin Assembly"
    exit 1
  fi

  if ! tar -xzf "apache-metron-bro-plugin-kafka_$BRO_VERSION-$RC.tar.gz" ; then
    echo "[ERROR] failed to unpack  Bro Kafka Plugin Assembly"
    exit 1
  fi

fi

echo "Unpacking Assemblies"
if ! tar -xzf "apache-metron_$METRON_VERSION-$RC.tar.gz" ; then
  echo "[ERROR] failed to unpack Metron Assembly"
  exit 1
fi

echo ""
echo ""
read -p "  run test suite [install, unit tests, integration tests, ui tests, licenses, rpm build]? [yN] " -n 1 -r
echo
DID_BUILD=0
if [[ $REPLY =~ ^[Yy]$ ]]; then
  cd "apache-metron_${METRON_VERSION}-$RC" || exit 1
  if ! mvn -q -T 2C -DskipTests clean install  ; then
    echo "[ERROR] failed to mvn install metron"
    exit 1
  fi
  if ! mvn -q -T 2C surefire:test@unit-tests ; then
    echo "[ERROR] failed unit tests"
    exit 1
  fi
  if ! mvn -q surefire:test@integration-tests ; then
    echo "[ERROR] failed integration tests"
    exit 1
  fi
  if ! mvn -q test --projects metron-interface/metron-config  ; then
    echo "[ERROR] failed metron-config tests"
    exit 1
  fi
  dev-utilities/build-utils/verify_licenses.sh | tee ../build-lic.log
  cd metron-deployment || exit 1
  if ! mvn -q package -DskipTests -P build-rpms ; then
    echo "[ERROR] failed to build rpm"
    exit 1
  fi
  cd .. || exit 1
  DID_BUILD=1
fi

#ask if build test vagrant METRON

# run tests?
echo ""
echo ""
read -p "  run vagrant full_dev? [yN] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
  cd "$WORK/apache-metron_${METRON_VERSION}-$RC/metron-deployment/development/centos6" || exit 1
  if [[ ${DID_BUILD} -ne 1 ]]; then
    vagrant up
  else
    vagrant --ansible-skip-tags="build,sensors,quick-dev" up
  fi
fi
