#!/bin/bash

cd "$(dirname "$0")/.."

function checkout_at() {
  repo=$1; url=$2; sha=$3
  if [ ! -d "$repo" ]; then
    git clone "https://github.com/$url" "$repo"
  fi

  pushd "$repo"
  git fetch && git reset --hard "$sha"
  popd
}

checkout_at "examples/elm-spa-example" "rtfeldman/elm-spa-example" "c8c3201ec0488f17c1245e1fd2293ba5bc0748d5"
checkout_at "examples/elm-browser" "elm/browser" "1d28cd625b3ce07be6dfad51660bea6de2c905f2"
checkout_at "examples/elm-bytes" "elm/bytes" "2bce2aeda4ef18c3dcccd84084647d22a7af36a6"
checkout_at "examples/elm-core" "elm/core" "22eefd207e7a63daab215ae497f683ff2319c2ca"
checkout_at "examples/elm-file" "elm/file" "e4ca3864c93a5e766e24ed6916174753567b2f59"
checkout_at "examples/elm-html" "elm/html" "94c079007f8a7ed282d5b53f4a49101dd0b6cf99"
checkout_at "examples/elm-http" "elm/http" "81b6fdc67d8e5fb25644fd79e6b0edbe2e14e474"
checkout_at "examples/elm-json" "elm/json" "af344039e8c014b06ed0f73ac3ffd22c60d30876"
checkout_at "examples/elm-parser" "elm/parser" "7506b07eaa93a93d13b508b948c016105b0953c8"
checkout_at "examples/elm-project-metadata-utils" "elm/project-metadata-utils" "b075be15046e151e36a79950412c4cb703605aeb"
checkout_at "examples/elm-random" "elm/random" "ecf97bb43f0d5cd75243428f69f45323957bda25"
checkout_at "examples/elm-regex" "elm/regex" "8810c41fb17ddf89165665489be213f44070bc4a"
checkout_at "examples/elm-svg" "elm/svg" "08bd432990862bab5b840654dd437fbb2e6176e7"
checkout_at "examples/elm-time" "elm/time" "dc3b75b7366e59b99962706f7bf064d3634a4bba"
checkout_at "examples/elm-url" "elm/url" "8602f4b48653ca93b6991fe2c6a764523cd1d462"
checkout_at "examples/elm-virtual-dom" "elm/virtual-dom" "5a5bcf48720bc7d53461b3cd42a9f19f119c5503"
checkout_at "examples/elm-ui" "mdgriffith/elm-ui" "7aa0e015499d4975a4dda02c2629ee21ec18eac3"
checkout_at "examples/elm-markup" "mdgriffith/elm-markup" "e4b88aa04c7e9bafd02b5436c311cf07e255dab1"
checkout_at "examples/elm-visualization" "gampleman/elm-visualization" "68631a3dbf840d30bc642e605a49ccd8040504f6"


skipped_files=()
all_examples=$(find examples -name '*.elm')
known_failures=$(cat script/known-failures.txt)
examples_to_parse=$(
  for example in $all_examples; do
    if [[ ! $known_failures == *$example* ]]; then
      echo $example
    else
      skipped_files+=($example)
    fi
  done
)

start=`date +%s.%N`
tree_sitter_report=$(echo $examples_to_parse | xargs -n 1000 npx tree-sitter parse -q)
end=`date +%s.%N`

ret_code=$?

echo -e "-----------------------------------------------------------------\n$tree_sitter_report \n -----------------------------------------------------------------\n"

errors=$( echo "$tree_sitter_report" | sed '/^\s*$/d' | wc -l )

skipped=$( echo ${#skipped_files[@]} )
tried_to_parse=$( echo "$examples_to_parse" | wc -w )
parsed=$(bc -l <<< "$tried_to_parse-$errors")
total=$((tried_to_parse+skipped))
percent=$(bc -l <<< "100*$parsed/$total")
runtime=$( echo "$end - $start" | bc -l )

printf "Successfully parsed %d of %d files (%.2f%%)\nSkipped: %d Failed: %d\nTook: %s\n" $parsed $total $percent $skipped $errors $runtime

exit $ret_code
