#!/usr/bin/env bash

# Convenience script for checking constraints locally

set -euo pipefail -x

cd `dirname $0`

MAJOR=${1:-24}
LTS=$(curl -f -s https://www.stackage.org/download/snapshots.json | jq -r '.["lts-'$MAJOR'"]')

# sometimes http fails
if [ -z "$LTS" ]; then
    LTS=$(curl -f -s https://www.stackage.org/download/snapshots.json | jq -r '.["lts-'$MAJOR'"]')
fi

if [ -z "$LTS" ]; then
    echo "failed to read lts minor version 2 times"
    exit 1
else
    echo "$LTS"
fi

export GHCVER=$(sed -n "s/^ghc-version: \"\(.*\)\"/\1/p" "lts-$MAJOR-build-constraints.yaml")

curator update &&
  curator constraints --no-download --target=$LTS &&
  curator snapshot-incomplete --target=$LTS &&
  curator snapshot &&
  stack --resolver ghc-$GHCVER exec curator check-snapshot
