# -----------------------------------------------------------------------------
#
# Package       : rubyzip
# Version       : v2.2.0
# Source repo   : https://github.com/rubyzip/rubyzip.git
# Tested on     : UBI 8.5
# Language      : Ruby
# Travis-Check  : True
# Script License: Apache License, Version 2 or later
# Maintainer    : Siddhesh Ghadi <Siddhesh.Ghadi@ibm.com>
#
# Disclaimer: This script has been tested in root mode on given
# ==========  platform using the mentioned version of the package.
#             It may not work as expected with newer versions of the
#             package and/or distribution. In such case, please
#             contact "Maintainer" of this script.
#
# ----------------------------------------------------------------------------

#set -e 

PACKAGE_NAME=rubyzip
PACKAGE_VERSION=${1:-v2.2.0}
PACKAGE_URL=https://github.com/rubyzip/rubyzip.git

yum install -y git curl make gcc-c++ ruby ruby-devel rubygem-rake rpm-build procps

gem install bundle
gem install bundler:1.17.3
gem install rake
gem install kramdown-parser-gfm
curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
curl -L https://get.rvm.io | bash -s stable
/bin/bash -c "source /etc/profile.d/rvm.sh;"

mkdir -p output

export LC_ALL=C.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

source /etc/profile.d/rvm.sh;
OS_NAME=$(cat /etc/os-release | grep ^PRETTY_NAME | cut -d= -f2)

# Jruby is needed only by few packages like logstash, hence differing setting in PATH till needed

#install previous version of bundler
gem install bundler:1.17.3
gem install kramdown-parser-gfm

function test_with_jruby(){
	echo "Automation using Jruby"
	rvm install jruby
	export PATH=/usr/local/rvm/rubies/jruby-9.2.9.0/bin/:$PATH
	if ! git clone $PACKAGE_URL $PACKAGE_NAME; then
			echo "------------------$PACKAGE_NAME:clone_fails---------------------------------------"
			echo "$PACKAGE_URL $PACKAGE_NAME" 
			echo "$PACKAGE_NAME  |  $PACKAGE_URL |  $PACKAGE_VERSION | $OS_NAME | GitHub | Fail |  Clone_Fails"
			exit 0
	fi

	cd /$PACKAGE_NAME
	git checkout $PACKAGE_VERSION
	if ! bundle install; then
			echo "------------------$PACKAGE_NAME:install_fails-------------------------------------"
		echo "$PACKAGE_URL $PACKAGE_NAME"
		echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub | Fail |  Install_Fails"
		exit 0
	fi
	bundle config set --local disable_checksum_validation false
	cd /$PACKAGE_NAME
	#- Check if script/cibuild file exists, if exists, run the script
	#- else check if .rspec exists, if yes run bundle exec rspec
	#- else check if Rakefile exists, run bundle exec rake
	#- else mark as no tests available
	if test -f "script/cibuild"; then
		chmod u+x script/cibuild
		if ! script/cibuild; then
			echo "------------------$PACKAGE_NAME:install_success_but_test_fails---------------------"
			echo "$PACKAGE_URL $PACKAGE_NAME" 
			echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub | Fail |  Install_success_but_test_Fails" 
			exit 0
		else
			echo "------------------$PACKAGE_NAME:install_&_test_both_success-------------------------"
			echo "$PACKAGE_URL $PACKAGE_NAME"  
			echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub  | Pass |  Both_Install_and_Test_Success" 
			exit 0
		fi
	elif test -f ".rspec"; then
		if ! bundle exec rspec; then
				echo "------------------$PACKAGE_NAME:install_success_but_test_fails---------------------"
				echo "$PACKAGE_URL $PACKAGE_NAME" 
				echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub | Fail |  Install_success_but_test_Fails" 
				exit 0
		else
			echo "------------------$PACKAGE_NAME:install_&_test_both_success-------------------------"
			echo "$PACKAGE_URL $PACKAGE_NAME"  
			echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub  | Pass |  Both_Install_and_Test_Success" 
			exit 0
		fi
	elif test -f "Rakefile"; then
		if ! bundle exec rake; then
			echo "------------------$PACKAGE_NAME:install_success_but_test_fails---------------------"
			echo "$PACKAGE_URL $PACKAGE_NAME" 
			echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub | Fail |  Install_success_but_test_Fails" 
			exit 0
		else
			echo "------------------$PACKAGE_NAME:install_&_test_both_success-------------------------"
			echo "$PACKAGE_URL $PACKAGE_NAME"
			echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub  | Pass |  Both_Install_and_Test_Success" 
			exit 0
		fi
	else
		echo "------------------$PACKAGE_NAME:install_success_&_test_NA-------------------------"
		echo "$PACKAGE_URL $PACKAGE_NAME"
		echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub  | Pass |  Install_Success_and_Test_NA" 
		exit 0
	fi
}

function test_with_ruby(){
	echo "Automation via Ruby version 2.5"
	if ! git clone $PACKAGE_URL $PACKAGE_NAME; then
		echo "------------------$PACKAGE_NAME:clone_fails---------------------------------------"
		echo "$PACKAGE_URL $PACKAGE_NAME" 
		echo "$PACKAGE_NAME  |  $PACKAGE_URL |  $PACKAGE_VERSION | $OS_NAME | GitHub | Fail |  Clone_Fails" 
		exit 0
	fi

	cd /$PACKAGE_NAME
	git checkout $PACKAGE_VERSION
	# some packages have checksum corrupted, hence disabling checksum
	bundle config set --local disable_checksum_validation true
	if ! bundle _1.17.3_  install; then
		if ! bundle install; then
			test_with_jruby
		fi
	fi
	bundle config set --local disable_checksum_validation false

	cd /$PACKAGE_NAME
	#- Check if script/cibuild file exists, if exists, run the script
	#- else check if .rspec exists, if yes run bundle exec rspec
	#- else check if Rakefile exists, run bundle exec rake
	#- else mark as no tests available
	if test -f "script/cibuild"; then
		chmod u+x script/cibuild
		if ! script/cibuild; then
			echo "------------------$PACKAGE_NAME:install_success_but_test_fails---------------------"
			echo "$PACKAGE_URL $PACKAGE_NAME"  
			echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub | Fail |  Install_success_but_test_Fails"
			exit 0
		else
			echo "------------------$PACKAGE_NAME:install_&_test_both_success-------------------------"
			echo "$PACKAGE_URL $PACKAGE_NAME"  
			echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub  | Pass |  Both_Install_and_Test_Success"
			exit 0
		fi
	elif test -f ".rspec"; then
		if ! bundle _1.17.3_ exec rspec; then
			if ! bundle exec rspec; then
				echo "------------------$PACKAGE_NAME:install_success_but_test_fails---------------------"
				echo "$PACKAGE_URL $PACKAGE_NAME" 
				echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub | Fail |  Install_success_but_test_Fails" 
				exit 0
			fi
		else
			echo "------------------$PACKAGE_NAME:install_&_test_both_success-------------------------"
			echo "$PACKAGE_URL $PACKAGE_NAME"  
			echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub  | Pass |  Both_Install_and_Test_Success" 
			exit 0
		fi
	elif test -f "Rakefile"; then
		if ! bundle _1.17.3_ exec rake; then
			if ! bundle exec rake; then
				echo "------------------$PACKAGE_NAME:install_success_but_test_fails---------------------"
				echo "$PACKAGE_URL $PACKAGE_NAME" 
				echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub | Fail |  Install_success_but_test_Fails"
				exit 0
			fi
		else
			echo "------------------$PACKAGE_NAME:install_&_test_both_success-------------------------"
			echo "$PACKAGE_URL $PACKAGE_NAME" 
			echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub  | Pass |  Both_Install_and_Test_Success" 
			exit 0
		fi
	else
		echo "------------------$PACKAGE_NAME:install_success_&_test_NA-------------------------"
		echo "$PACKAGE_URL $PACKAGE_NAME"  
		echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub  | Pass |  Install_Success_and_Test_NA" 
		exit 0
	fi
}

# Try building packages with normal ruby compiler as its needed to build native packages, if it does not work fallback to jruby compiler
test_with_ruby