env_check.sh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env bash
  2. # Lightweight environment check for DOCX skill.
  3. # Exit 0 = all OK, exit 1 = missing dependencies.
  4. # Also resolves and exports DOCX_SKILL_DIR and FONT_DIR.
  5. # Usage: source env_check.sh (preferred, exports vars to caller)
  6. # or: bash env_check.sh [--quiet]
  7. QUIET=false; [ "${1:-}" = "--quiet" ] && QUIET=true
  8. FAIL=0
  9. check() { local desc="$1"; shift; if ! "$@" &>/dev/null; then $QUIET || echo "MISSING: $desc"; FAIL=1; fi; }
  10. # LibreOffice is install-on-demand (its absence does not fail this check), but it is NOT
  11. # substitutable: when .doc→.docx / DOCX→PDF / visual verification needs it and it is absent, the
  12. # model must explain + warn + install, never silently switch to the user's local Word/WPS/Pages.
  13. required_on_demand() { local desc="$1"; shift; if "$@" &>/dev/null; then $QUIET || echo "on-demand OK: $desc"; else $QUIET || echo "on-demand MISSING: $desc — if a task needs it you MUST install it, not substitute it. FORBIDDEN: using Word/WPS/Pages instead, or skipping the PDF/visual check; a large download is NOT a reason to skip. REQUIRED: explain the necessity to the user, say the package is large, then INSTALL (preferred source: https://mirrors.tuna.tsinghua.edu.cn/libreoffice/libreoffice/stable/26.8.0/), THEN register it on PATH and re-verify 'soffice --version' (Linux/macOS: ln -sf the binary into /usr/local/bin; Windows: setx the program dir onto the user PATH) — see env_setup/setup.md \"Register the binary on PATH\". FIRST check whether it is already installed but merely off PATH (/opt/libreoffice*/program/soffice, /Applications/LibreOffice.app/Contents/MacOS/soffice) — if so register it instead of reinstalling. No install attempt = no substitute program."; fi; }
  14. # ── Resolve DOCX_SKILL_DIR & FONT_DIR ──
  15. _ENV_CHECK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
  16. DOCX_SKILL_DIR="$(cd "$_ENV_CHECK_DIR/.." && pwd)"
  17. export DOCX_SKILL_DIR
  18. if [ "$(uname -s)" = "Darwin" ]; then
  19. FONT_DIR="${HOME}/Library/Fonts"
  20. else
  21. FONT_DIR="/usr/share/fonts"
  22. fi
  23. export FONT_DIR
  24. check "node" command -v node
  25. check "python3" command -v python3
  26. check "defusedxml" python3 -c "import defusedxml"
  27. # Font check
  28. if command -v fc-list &>/dev/null; then
  29. fc-list :lang=zh 2>/dev/null | grep -qi "noto\|simhei\|wenquanyi" || { $QUIET || echo "MISSING: CJK fonts"; FAIL=1; }
  30. fi
  31. # ── ON-DEMAND but NOT substitutable: .doc→.docx / DOCX→PDF / visual check (LibreOffice/soffice) ──
  32. required_on_demand "libreoffice (soffice)" command -v soffice
  33. $QUIET || echo "DOCX_SKILL_DIR=$DOCX_SKILL_DIR"
  34. $QUIET || echo "FONT_DIR=$FONT_DIR"
  35. return $FAIL 2>/dev/null || exit $FAIL