#!/bin/sh

set -e

if [ "$TRAVIS" = "true" ]; then
  echo '$ sudo apt-get install bc' && echo ''
  sudo apt-get install -q -q bc 2> /dev/null > /dev/null
fi

t/sizes index.js | sed 's/^/    /'

echo ""

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

if [ "$TRAVIS" = "true" ] || [ "$MINIFY" = "true" ]; then
  echo "generating coverage"
  t/cover

  echo "submitting coverage to coveralls.io"
  cat coverage/lcov.info | node_modules/.bin/coveralls > /dev/null

  echo ""
  echo "minified"
  echo ""
  default=$(cat index.js | uglifyjs | gzip -c | wc -c)
  lift=$(cat index.js | uglifyjs --lift-vars | gzip -c | wc -c)

  bak=index-$(date +'%FT%T').js
  mv index.js $bak
  if [ $default -lt $lift ]; then
    cat $bak | uglifyjs > index.js
  else
    cat $bak | uglifyjs --lift-vars > index.js
  fi

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

echo ""
