#!/bin/bash
# Fail if tracked shell scripts contain CR (CRLF) — breaks bash on Ubuntu.
# Usage: bash deploy/check_shell_eol.sh
# Run from repo root (or set NEXUS_DEPLOY_DIR).

set -euo pipefail

_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
_REPO_ROOT="$(cd "${_SCRIPT_DIR}/.." && pwd)"
ROOT="${NEXUS_DEPLOY_DIR:-${_REPO_ROOT}}"

cd "${ROOT}"

if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  echo "check_shell_eol: not a git repo at ${ROOT}" >&2
  exit 1
fi

BAD=0
while IFS= read -r -d '' f; do
  if grep -q $'\r' "${f}" 2>/dev/null; then
    echo "CRLF/CR detected: ${f}" >&2
    BAD=1
  fi
done < <(git ls-files -z '*.sh')

if [ "${BAD}" -ne 0 ]; then
  echo "Fix: git add --renormalize '*.sh' or run dos2unix on listed files." >&2
  exit 1
fi

echo "check_shell_eol: OK (all tracked *.sh are LF-only)"
