#!/bin/bash
# For each mutant: run the 22 contract tests on a mutated copy, and the
# differential harness (mutated native copy) on six zones.
R="$1"; cd "$R" || exit 1
MZ="UTC America/New_York Europe/London Australia/Lord_Howe America/Santiago Asia/Kuala_Lumpur"
mkdir -p mut
one() {
  id="$1"; plat="$2"; W="mut/$id"; rm -rf "$W"; mkdir -p "$W"
  cp -R contract_tree "$W/tree"
  python3 mutants.py . apply "$id" "$W/tree"
  ( cd "$W/tree" && "${FLUTTER:-flutter}" test test/widget_parity_test.dart test/android_widget_remote_views_test.dart test/ios_widget_source_test.dart > ../contract.txt 2>&1; echo "exit=$?" >> ../contract.txt )
  if [ "$plat" = kotlin ]; then
    mkdir -p "$W/kotlin"; cp kotlin/Rotation.kt kotlin/json-20180813.jar "$W/kotlin/"
    python3 mutants.py . harness "$id" "$W/kotlin"
    ( cd "$W/kotlin" && kotlinc Rotation.kt -cp json-20180813.jar -include-runtime -d rotation.jar > build.txt 2>&1 )
    for z in $MZ; do f=$(echo $z | tr / _); TZ=$z java -cp "$W/kotlin/rotation.jar:kotlin/json-20180813.jar" RotationKt out/dart_$f.json "$W/kotlin_$f.json" > /dev/null 2>&1; done
  else
    mkdir -p "$W/swift"; cp swift/Rotation.swift "$W/swift/"
    python3 mutants.py . harness "$id" "$W/swift"
    ( cd "$W/swift" && swiftc -O -parse-as-library Rotation.swift -o rotation > build.txt 2>&1 )
    for z in $MZ; do f=$(echo $z | tr / _); TZ=$z "$W/swift/rotation" out/dart_$f.json "$W/swift_$f.json" > /dev/null 2>&1; done
  fi
  echo "mutant $id done"
}
for x in $(python3 mutants.py . list | awk '{print $1":"$2}'); do one "${x%%:*}" "${x##*:}" & while [ "$(jobs -r | wc -l)" -ge 4 ]; do sleep 2; done; done
wait
echo ALL MUTANTS DONE
