#! /usr/bin/env bash
#
#  script to test a set of gem files
#
set -o errexit
set -o nounset
set -o pipefail

gems=$*
gem_platform_local=`ruby -e "puts Gem::Platform.local.to_s"`

function remove_all {
  gem uninstall --all --force $1
}

function test_installation {
  gem=$1

  # test installation
  remove_all sqlite3
  gem install $gem
  ruby -r sqlite3 -e 'pp SQLite3::SQLITE_VERSION, SQLite3::SQLITE_LOADED_VERSION'

  if [[ $gem =~ sqlite3-[^-]*\.gem ]] ; then
    # test installation against system libs without mini_portile, linux distro repackagers
    # (note that we don't care about running the gem in this case, just building it)
    # see https://github.com/sparklemotion/sqlite3-ruby/pull/381 for background
    remove_all sqlite3
    remove_all mini_portile2
    gem install --local --force $gem -- --enable-system-libraries
    if gem list -i mini_portile2 ; then
      echo "FAIL: should not have installed mini_portile2"
      exit 1
    fi

    # test installation against system libs
    remove_all sqlite3
    gem install $gem -- --enable-system-libraries
    ruby -r sqlite3 -e 'pp SQLite3::SQLITE_VERSION, SQLite3::SQLITE_LOADED_VERSION'
  fi
}

for gem in $gems ; do
  ./bin/test-gem-file-contents $gem
done

for gem in $gems ; do
  if [[ $gem =~ sqlite3-[^-]+(-${gem_platform_local})?\.gem$ ]] ; then
    test_installation $gem
  fi
done
