#!/usr/bin/env bash
# Factory artifact runnability.
set -euo pipefail

ARTIFACT="${HOME}/.local/opt/overdeck/harness/current"
fail() { echo "health: $1" >&2; exit 1; }

[[ -e "$ARTIFACT" ]] || fail "artifact missing at $ARTIFACT"
[[ -x "$ARTIFACT/factory/bin/factory" ]] || fail "factory not executable at $ARTIFACT/factory/bin/factory"
bash -n "$ARTIFACT/factory/bin/factory" >/dev/null 2>&1 || fail "factory entrypoint does not parse"

factory_link="${HOME}/.local/bin/factory"
[[ -L "$factory_link" ]] || fail "factory PATH symlink missing at $factory_link"
[[ "$(readlink -f "$factory_link")" == "$(readlink -f "$ARTIFACT/factory/bin/factory")" ]] || fail "factory PATH symlink is stale"
