#!/bin/bash

if ! { ruby --version | grep -q '^ruby 1.9.3'; }
then
  echo "Zone verification tests fail without Ruby 1.9.3." >&2
fi

# On Travis CI we need to run a little setup, print a few diagnostics.
if [ "$TRAVIS" = "true" ]; then
  # We need a Ruby 1.9.3 for to get a `strftime` that understands `%::z`. Print
  # out the Ruby version so we can quickly see if we have a bad ruby.
  ruby -v
  which ruby

  # Install `bc` so we can get our minification report.
  sudo apt-get -q install bc >/dev/null 2>&1 && echo "apt-get install bc"

  # Run the minification report.
  echo ""
  (cd src && ../util/sizes timezone.js rfc822.js)
  echo ""
fi

ls -la build/timezone/index.js

echo ""

(proof run t/*/*.t.* t/*/*/*.t.* | tee .proof.out | proof progress) || (proof errors < .proof.out) || exit 1

# On Travis CI we rerun all the tests against the minified source just in case.
if [ "$TRAVIS" = "true" ] || [ "$MINIFY" = "true" ]; then
  default=$(cat src/timezone.js | uglifyjs | gzip -c | wc -c)
  lift=$(cat src/timezone.js | uglifyjs --lift-vars | gzip -c | wc -c)

  if [ $default -lt $lift ]; then
    cat src/timezone.js | uglifyjs > build/timezone/index.js
  else
    cat src/timezone.js | uglifyjs --lift-vars > build/timezone/index.js
  fi

  ls -la build/timezone/index.js

  echo ""

  (proof run t/*/*.t.* t/locale/*/*.t.* t/zones/*/*.t.js | tee .proof.out | proof progress) || (proof errors < .proof.out) || exit 1
fi
