#!/usr/bin/env bash
set -euo pipefail

if [[ "$#" -ne 1 ]]; then
  printf 'usage: %s <shell-directory>\n' "$0" >&2
  exit 64
fi

target="$1"
if [[ ! -d "$target" ]]; then
  printf 'shell directory not found: %s\n' "$target" >&2
  exit 1
fi

expected_paths='README.md
config
config/bashrc.dev-lab
data
data/access.log
notes
notes/shell-notes.md
output
output/summary.txt
scripts
scripts/report.sh'
actual_paths="$(
  cd "$target"
  find . -mindepth 1 -print | sed 's#^\./##' | LC_ALL=C sort
)"
if [[ "$actual_paths" != "$expected_paths" ]]; then
  printf 'shell tree must contain exactly the six documented files: %s\n' "$target" >&2
  printf 'received entries:\n%s\n' "$actual_paths" >&2
  exit 1
fi

expected_files=(
  'README.md'
  'config/bashrc.dev-lab'
  'data/access.log'
  'notes/shell-notes.md'
  'output/summary.txt'
  'scripts/report.sh'
)
for relative_path in "${expected_files[@]}"; do
  if [[ ! -f "$target/$relative_path" || -L "$target/$relative_path" ]]; then
    printf 'shell checkpoint must include a regular file: %s\n' "$target/$relative_path" >&2
    exit 1
  fi
done

failures=0
fail() {
  printf 'shell checkpoint: %s\n' "$1" >&2
  failures=$((failures + 1))
}

has_any_execute_bit() {
  local mode
  mode="$(LC_ALL=C ls -ld "$1")"
  mode="${mode%% *}"
  [[ "$mode" == *[xstST]* ]]
}

[[ -x "$target/scripts/report.sh" ]] || fail "report.sh must be executable: $target/scripts/report.sh"
for relative_path in \
  'README.md' \
  'config/bashrc.dev-lab' \
  'data/access.log' \
  'notes/shell-notes.md' \
  'output/summary.txt'; do
  if has_any_execute_bit "$target/$relative_path"; then
    fail "$relative_path must not be executable: $target/$relative_path"
  fi
done
if ! bash -n "$target/scripts/report.sh"; then
  fail "report.sh has invalid Bash syntax: $target/scripts/report.sh"
fi
if ! bash -n "$target/config/bashrc.dev-lab"; then
  fail "bashrc.dev-lab has invalid Bash syntax: $target/config/bashrc.dev-lab"
fi
if ! cmp -s <(cat <<'EXPECTED'
# dev-lab Shell checkpoint

这是终端与 Shell 板块完成后的权威参考状态。它保留固定访问日志、确定性摘要、隔离 Bash 配置和只会写入明确输出文件的报告脚本。

请在此目录的父目录运行 `check-shell.sh`；checker 只读取目标，不会 source 配置或执行报告脚本。
EXPECTED
) "$target/README.md"; then
  fail "README.md must match the canonical shell checkpoint: $target/README.md"
fi
if ! cmp -s <(cat <<'CONFIG'
export DEVLAB_MODE='practice'

devlab-root() {
  printf '%s\n' "${DEVLAB_DIR:?set DEVLAB_DIR before using devlab-root}"
}

devlab-summary() {
  bash "${DEVLAB_DIR:?set DEVLAB_DIR first}/scripts/report.sh" "$DEVLAB_DIR"
}
CONFIG
) "$target/config/bashrc.dev-lab"; then
  fail "bashrc.dev-lab must match the approved isolated configuration: $target/config/bashrc.dev-lab"
fi

if ! cmp -s <(printf '%s\n' \
  '2026-08-12T09:00:00Z GET / 200' \
  '2026-08-12T09:00:01Z GET /docs/devenv/welcome 200' \
  '2026-08-12T09:00:02Z GET /docs/devenv/module-route 200' \
  '2026-08-12T09:00:03Z GET /missing 404' \
  '2026-08-12T09:00:04Z POST /practice 200' \
  '2026-08-12T09:00:05Z GET /old-link 404') "$target/data/access.log"; then
  fail "access log is incorrect: $target/data/access.log"
fi

if ! cmp -s <(cat <<'EXPECTED'
# Shell 练习笔记

历史记录可能包含参数、路径或令牌；先检查再分享或清理。Tab 补全帮助确认命令和文件名，但不代替阅读实际路径。

本 checkpoint 的 Bash 配置仅用于 `DEVLAB_DIR` 指定的隔离练习目录。
EXPECTED
) "$target/notes/shell-notes.md"; then
  fail "shell notes must match the canonical checkpoint: $target/notes/shell-notes.md"
fi

grep -Fx 'total requests: 6' "$target/output/summary.txt" >/dev/null || fail "summary total is incorrect: $target/output/summary.txt"
grep -Fx 'successful requests: 4' "$target/output/summary.txt" >/dev/null || fail "summary successful count is incorrect: $target/output/summary.txt"
grep -Fx 'not found: 2' "$target/output/summary.txt" >/dev/null || fail "summary not-found count is incorrect: $target/output/summary.txt"
if ! cmp -s <(printf '%s\n' \
  'total requests: 6' \
  'successful requests: 4' \
  'not found: 2') "$target/output/summary.txt"; then
  fail "summary must contain exactly the fixed three lines: $target/output/summary.txt"
fi


if ! cmp -s <(cat <<'EXPECTED'
#!/usr/bin/env bash
set -euo pipefail

[[ "$#" -eq 1 ]] || { printf 'usage: report.sh DEV_LAB_DIR\n' >&2; exit 64; }
devlab_dir="${1:?usage: report.sh DEV_LAB_DIR}"
input="$devlab_dir/data/access.log"
output="$devlab_dir/output/summary.txt"
[[ -f "$input" ]] || { printf 'missing input: %s\n' "$input" >&2; exit 1; }

total="$(wc -l < "$input" | tr -d ' ')"
successful="$(grep -c ' 200$' "$input" || true)"
not_found="$(grep -c ' 404$' "$input" || true)"

{
  printf 'total requests: %s\n' "$total"
  printf 'successful requests: %s\n' "$successful"
  printf 'not found: %s\n' "$not_found"
} > "$output"
EXPECTED
) "$target/scripts/report.sh"; then
  fail "report.sh must match the canonical deterministic report: $target/scripts/report.sh"
fi

if (( failures > 0 )); then
  exit 1
fi

printf 'shell checkpoint: ok\n'
