#!/bin/sh
# Run the cclsh regression checks in a fresh CCL image.
set -e
cd "$(dirname "$0")/.."

if [ -z "${CCLSH_QUICKLISP_SETUP:-}" ]; then
    CCLSH_QUICKLISP_SETUP="$HOME/quicklisp/setup.lisp"
    export CCLSH_QUICKLISP_SETUP
fi

if [ -z "${ASDF_OUTPUT_TRANSLATIONS:-}" ]; then
    ASDF_OUTPUT_TRANSLATIONS="$PWD/:${TMPDIR:-/tmp}/cclsh-fasl/"
    export ASDF_OUTPUT_TRANSLATIONS
fi

ccl_command=${CCLSH_CCL:-ccl}
if ! command -v "$ccl_command" >/dev/null 2>&1; then
    echo "cclsh check: CCL executable not found: $ccl_command" >&2
    exit 1
fi
set -- -n -b
if [ -n "${CCLSH_CCL_IMAGE:-}" ]; then
    set -- -I "$CCLSH_CCL_IMAGE" "$@"
fi
# The unit suite exercises job-control state without being an interactive
# shell. Keep its terminal snapshot empty even when the caller owns a TTY;
# interactive terminal behavior is covered by scripts/integration-check.
"$ccl_command" "$@" \
    -e '(setf ccl:*default-file-character-encoding* :utf-8)' \
    -e '(require :asdf)' \
    -e \
    '(handler-case
         (progn
           (let ((setup
                   (ccl::with-utf-8-cstr
                       (encoded-name "CCLSH_QUICKLISP_SETUP")
                     (let ((value
                             (ccl:external-call "getenv"
                                                :address encoded-name
                                                :address)))
                       (unless (ccl:%null-ptr-p value)
                         (pathname (ccl::%get-utf-8-cstring value)))))))
             (unless (and setup (probe-file setup))
               (error "Quicklisp setup is unavailable: ~a" setup))
             (load setup :verbose nil :external-format :utf-8))
            (let* ((root (truename "./"))
                   (specifications
                     (quote
                      (("cl-colorist" "CCLSH_CL_COLORIST_SOURCE"
                        "../cl-colorist/" "cl-colorist.asd")
                       ("colorlisp" "CCLSH_COLORLISP_SOURCE"
                        "../colorlisp/" "colorlisp.asd")
                       ("colordiff" "CCLSH_COLORDIFF_SOURCE"
                        "../colordiff/" "colordiff.asd")
                       ("structlisp" "CCLSH_STRUCTLISP_SOURCE"
                        "../structlisp/" "structlisp.asd")
                       ("clinedi" "CCLSH_CLINEDI_SOURCE"
                        "../clinedi/" "clinedi.asd"))))
                   (systems
                     (mapcar
                      (lambda (specification)
                        (destructuring-bind
                            (name environment default-directory asd-name)
                            specification
                          (let* ((override (uiop:getenv environment))
                                 (directory
                                   (if (and override (plusp (length override)))
                                       (uiop:ensure-directory-pathname override)
                                       (merge-pathnames default-directory root)))
                                 (asd (truename
                                       (merge-pathnames asd-name directory))))
                            (list name asd))))
                      specifications)))
              (asdf:initialize-source-registry
               `(:source-registry
                 (:directory ,root)
                 ,@(mapcar
                    (lambda (system)
                      `(:directory
                        ,(uiop:pathname-directory-pathname (second system))))
                    systems)
                 :ignore-inherited-configuration))
              (dolist (system systems)
                (destructuring-bind (name asd) system
                  (asdf:clear-system name)
                  (asdf:load-asd asd)
                  (let ((selected
                          (truename
                           (asdf:system-source-file (asdf:find-system name)))))
                    (unless (equal asd selected)
                      (error "ASDF selected ~a from ~a instead of ~a"
                             name selected asd)))
                  (asdf:load-system name :force t))))
            (dolist (system (quote ("cl-colorist" "colorlisp" "colordiff"
                                     "structlisp" "clinedi")))
              (asdf:test-system system))
            (load "tests/check.lisp" :external-format :utf-8))
       (serious-condition (condition)
         (format *error-output* "cclsh check loader failed: ~a~%" condition)
         (ccl:quit 1)))' </dev/null

tests/argument-boundary.sh
tests/build-state-rollback.sh
tests/install.sh
tests/system-install.sh
