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

export RELEASE=1
export LANG="en_US.UTF-8"

if [ -f /etc/os-release ]; then
    . /etc/os-release
fi

# IPv6 issues in docker containers:
# https://rvm.io/rvm/security#ipv6-issues
case ${ID}-${VERSION_ID} in
    ubuntu-18*)
    gpg2 --list-keys
    echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf
    ;;
esac

# Install ruby-2.4.0 to build Logstash
curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
curl -sSL https://get.rvm.io | bash -s stable
if ((UID == 0))
then
  [ -s /usr/local/rvm/scripts/rvm ] && source /usr/local/rvm/scripts/rvm
else
  [ -s $HOME/.rvm/scripts/rvm ] && source $HOME/.rvm/scripts/rvm
fi

# BIGTOP-3394
# Disable unnecessary autolibs installation
# for rvm fail to "try_sudo" to install autolibs
# dependencies under user:'jenkins'
rvm autolibs disable
rvm install ruby-2.4.0 && rvm use 2.4.0

# Specify childprocess to 3.0.0. The latest release 4.0.0 is not compatible with 5.4.1.
sed -i "s/^.*childprocess.*$/gem \"childprocess\"\, \"3\.0\.0\"/" Gemfile

rake bootstrap
rake plugin:install-default
rake artifact:tar

# Remove ruby-2.4.0 to make build-environment consistent
rvm remove ruby-2.4.0
