#!/bin/sh
set -ev
FORCE="${1}"
OLD="$(mhs --numeric-version || echo none)"
( git clone https://github.com/augustss/MicroHs.git || (cd MicroHs && git stash && git pull && (git stash pop || true)) )
( git clone https://github.com/augustss/mtl.git || (cd mtl && git stash && git pull && (git stash pop || true)) )
( git clone https://github.com/haskell/containers.git || (cd containers && git stash && git pull && (git stash pop || true)) )
( git clone https://code.mathr.co.uk/godwit.git || (cd godwit && git stash && git pull && (git stash pop || true)) )
NEW="$(grep '^version:' MicroHs/MicroHs.cabal | sed -e 's/^version: *//')"
if [ "x$OLD" != "x$NEW" -o "x$FORCE" = "x--force" ]
then
  rm -rf "${HOME}/.mcabal/mhs-$OLD"
  ( cd MicroHs && make clean && make install )
  PATH="${HOME}/.mcabal/bin:${PATH}"
  ( mcabal install transformers )
  ( cd mtl && mcabal install )
  ( cd containers/containers && mcabal install )
  ( cd godwit && make HC=mhs clean && make HC=mhs -j "$(nproc)" )
fi
