#!/bin/sh

set -eu

root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd -- "$root"

if [ ! -r "$root/.qlot/setup.lisp" ]; then
  printf '%s\n' 'structlisp dependencies are missing; run ./script/bootstrap.' >&2
  exit 1
fi

tests=0
case " $* " in
  *" --fix "*) ;;
  *)
    sbcl --noinform --non-interactive \
      --load "$root/.qlot/setup.lisp" \
      --eval '(require :asdf)' \
      --eval '(asdf:load-asd (truename "structlisp.asd"))' \
      --eval '(asdf:test-system "structlisp")' || tests=$?
    ;;
esac

source=0
sbcl --script "$root/script/check-source.lisp" "$@" || source=$?

if [ "$tests" -ne 0 ]; then
  exit "$tests"
fi
exit "$source"
