#!/usr/bin/env bash
#
# Licensed 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.
#
#
# Downloads a release candidate and verifies that it passes binary
# verification (signature and checksums) and test suites.
#
set -ex

HERE=$(cd `dirname "${BASH_SOURCE[0]:-$0}"` && pwd)

parquet_svn_dev_dist_url='https://dist.apache.org/repos/dist/dev/parquet'

download_dist_file() {
  curl -f -O ${parquet_svn_dev_dist_url}/$1
}

download_rc_file() {
  download_dist_file apache-parquet-cpp-${verify_version}-rc${verify_rc_num}/$1
}

import_gpg_keys() {
  download_dist_file KEYS
  gpg --import KEYS
}

fetch_archive() {
  local dist_name=$1
  download_rc_file ${dist_name}.tar.gz
  download_rc_file ${dist_name}.tar.gz.asc
  download_rc_file ${dist_name}.tar.gz.md5
  download_rc_file ${dist_name}.tar.gz.sha512
  gpg --verify ${dist_name}.tar.gz.asc ${dist_name}.tar.gz
  gpg --print-md MD5 ${dist_name}.tar.gz | diff - ${dist_name}.tar.gz.md5
  if [ "$(uname)" == "Darwin" ]; then
    shasum -a 512 ${dist_name}.tar.gz | diff - ${dist_name}.tar.gz.sha512
  else
    sha512sum ${dist_name}.tar.gz | diff - ${dist_name}.tar.gz.sha512
  fi
}

run_tests() {
  # Build
  mkdir -p build
  cd build
  cmake ..
  make -j5

  # Test
  export PARQUET_TEST_DATA=$(dirname $(pwd))/data
  make unittest
}

setup_tempdir() {
  cleanup() {
    rm -fr "$TMPDIR"
  }
  trap cleanup EXIT
  TMPDIR=$(mktemp -d -t "$1.XXXXX")
}

case $# in
  2) verify_version="$1"
     verify_rc_num="$2"
     ;;

  *) echo "Usage: $0 VERSION RC_NUM"
     echo "Example to test 1.3.0-rc2: $0 1.3.0 2"
     exit 1
     ;;
esac

setup_tempdir "parquet-cpp-$verify_version"
echo "Working in sandbox $TMPDIR"
cd $TMPDIR

import_gpg_keys

dist_name="apache-parquet-cpp-${verify_version}"
fetch_archive $dist_name
tar xvzf ${dist_name}.tar.gz
cd ${dist_name}
run_tests

echo 'Release candidate looks good!'
exit 0
