#!/usr/bin/env bash

ghcs_nix="https://gitlab.haskell.org/bgamari/ghcs-nix/-/archive/master/ghcs-nix-master.tar.gz"

nix build -f "$ghcs_nix" ghc-8_10_7 -o boot_ghc

run() {
  local ver="$1"
  local drv="ghc-$ver"
  echo "$ver"
  nix build -f "$ghcs_nix" $drv
  (cd ../; rm -r dist-newstyle; cabal build --dry-run hadrian -w bootstrap/result/bin/ghc)
  cp ../dist-newstyle/cache/plan.json "plan-$ver.json"
  cabal run --with-ghc-pkg "$PWD/boot_ghc/bin/ghc-pkg" -w "$PWD/boot_ghc/bin/ghc" -v0 hadrian-bootstrap-gen -- "plan-$ver.json" \
      | nix run "nixpkgs#jq" -- --sort-keys \
      | tee "plan-bootstrap-$ver.json"
}

run_all() {
    run "9_4_1"
    run "9_4_2"
    run "9_4_3"
    run "9_4_4"
    run "9_6_1"
    run "9_6_2"
    run "9_6_3"
    run "9_6_4"
    run "9_6_5"
    run "9_8_1"
    run "9_8_2"
}

if (( $# == 0 )); then
    run_all
else
    run "$1"
fi
