#!/usr/bin/env bash

# This script takes as its first argument the path to a checkout of FStar; it
# then proceeds to rebuild the OCaml snapshot of FStar only if the [.last_fstar]
# file in the current directory is not the same as the HEAD revision of FStar.
# The script updates [.last_fstar] after that.

set -e

LAST_FSTAR=$(cd $1 && git rev-parse HEAD)
if [ -f .last_fstar ]; then
  CURRENT_FSTAR=$(cat .last_fstar)
else
  CURRENT_FSTAR="n/a"
fi

echo "Most recent FStar version available: $LAST_FSTAR (currently: $CURRENT_FSTAR)"

if [ $CURRENT_FSTAR != $LAST_FSTAR ]; then
  make -C $1/src/ocaml-output clean
  make -C $1/src/ocaml-output
fi

echo $LAST_FSTAR > .last_fstar
