#!/bin/sh

# stop blowing compiled stuff
rm -rf builds/out-simp
rm -rf package.json
rm -rf package-lock.json

echo {} > package.json
mkdir -p builds/out-simp

possible=6
ran=0

#bin/cljsc test >out/core-test.js
if ! bin/cljsc src/test/cljs  "{:optimizations :simple
                                :static-fns true
                                :output-dir \"builds/out-simp\"
                                :cache-analysis true
                                :output-wrapper true
                                :verbose true
                                :compiler-stats true
                                :npm-deps {:lodash \"4.17.4\"}
                                :closure-warnings {:non-standard-jsdoc :off :global-this :off}
                                :install-deps true
                                :language-out :es5
                                :foreign-libs [{:file \"src/test/cljs/calculator_global.js\"
                                                :provides [\"calculator\"]
                                                :global-exports {calculator Calculator}}
                                               {:file \"src/test/cljs/es6_dep.js\"
                                                :module-type :es6
                                                :provides [\"es6_calc\"]}
                                               {:file \"src/test/cljs/calculator.js\"
                                                :module-type :commonjs
                                                :provides [\"calculator\"]}
                                               {:file \"src/test/cljs/es6_default_hello.js\"
                                                :provides [\"es6_default_hello\"]
                                                :module-type :es6}]}" > builds/out-simp/core-simple-test.js; then
  >&2 echo ClojureScript compilation failed
  exit 1
fi;

if [ "$V8_HOME" = "" ]; then
  echo "V8_HOME not set, skipping V8 tests"
else
  echo "Testing with V8"
  "${V8_HOME}/d8" builds/out-simp/core-simple-test.js
  ran=$[ran+1]
fi

if [ "$SPIDERMONKEY_HOME" = "" ]; then
  echo "SPIDERMONKEY_HOME not set, skipping SpiderMonkey tests"
else
  echo "Testing with SpiderMonkey"
  ${SPIDERMONKEY_HOME}/js -f builds/out-simp/core-simple-test.js
  ran=$[ran+1]
fi

if ! hash jsc 2>/dev/null; then
 echo "jsc not on path, skipping JavaScriptCore tests"
else
 echo "Testing with JavaScriptCore"
 jsc -f builds/out-simp/core-simple-test.js
 ran=$[ran+1]
fi

if [ "$NASHORN_HOME" = "" ]; then
  echo "NASHORN_HOME not set, skipping Nashorn tests"
else
  echo "Testing with Nashorn"
  "${NASHORN_HOME}/jjs" builds/out-simp/core-simple-test.js
  ran=$[ran+1]
fi

if [ "$CHAKRACORE_HOME" = "" ]; then
  echo "CHAKRACORE_HOME not set, skipping ChakraCore tests"
else
  echo "Testing with ChakraCore"
  "${CHAKRACORE_HOME}/ch" builds/out-simp/core-simple-test.js
  ran=$[ran+1]
fi

if [ "$GRAALVM_HOME" = "" ]; then
  echo "GRAALVM_HOME not set, skipping GraalVM tests"
else
  echo "Testing with GraalVM"
  "${GRAALVM_HOME}/js" builds/out-simp/core-simple-test.js
  ran=$[ran+1]
fi

echo "Tested with $ran out of $possible possible js targets"
