#!/bin/bash

set -e

bionic_meets_gem_requirement() {
    local gem="$1"
    # The gems listed below do not match what ruby upstream expects.
    # * power_assert: upstream expects version >= 1.1.1. Bionic has version 0.3.0-1
    #   but actually it contains 0.2.7, look at the gemspec:
    #   /usr/share/rubygems-integration/all/specifications/power_assert-0.2.7.gemspec
    # * test-unit: upstream expects version >= 3.2.7. Bionic has version 3.2.5-1.
    # * xmlrpc: upstream expects version >= 0.3.0. This package is not available in
    #   Bionic, just from Focal onwards.
    # Also see LP #1903905.
    local gems_not_expected_to_work=(power_assert test-unit xmlrpc)

    for gem_skipped in "${gems_not_expected_to_work[@]}"; do
        if [[ "${gem}" == "${gem_skipped}" ]]; then
	    echo "${gem} in Bionic does not match what ruby upstream expects for this version."
	    return 1
	fi
    done
    return 0
}

rc=0
while read gem version repository; do
  bionic_meets_gem_requirement "${gem}" || continue

  if ruby2.5 -e "gem '${gem}', '>= ${version}'"; then
    echo "${gem} (>= ${version}) OK"
  else
    rc=$(($rc + 1))
  fi
done < gems/bundled_gems

exit $rc
