env_check.sh 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env bash
  2. # Lightweight environment check for PDF skill.
  3. # Exit 0 = all CORE deps OK, exit 1 = missing CORE dependency.
  4. # Playwright/Chromium (Creative/HTML pipeline) and Tectonic (Academic/LaTeX) are
  5. # OPTIONAL — reported as INFO, never cause a non-zero exit. Install them on demand
  6. # (with the user's confirmation) only when a route actually needs them.
  7. # Also resolves and exports PDF_SKILL_DIR and FONT_DIR.
  8. # Usage: source env_check.sh (preferred, exports vars to caller)
  9. # or: bash env_check.sh [--quiet]
  10. QUIET=false; [ "${1:-}" = "--quiet" ] && QUIET=true
  11. FAIL=0
  12. check() { local desc="$1"; shift; if ! "$@" &>/dev/null; then $QUIET || echo "MISSING (core): $desc"; FAIL=1; fi; }
  13. optional() { local desc="$1"; shift; if "$@" &>/dev/null; then $QUIET || echo "optional OK: $desc"; else $QUIET || echo "optional MISSING: $desc (install on demand)"; fi; }
  14. # LibreOffice is install-on-demand like the others, but it is NOT substitutable: when an
  15. # Office→PDF route needs it and it is absent, the model must explain + warn + install, never
  16. # silently switch to the user's local Office/WPS.
  17. 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 route needs it you MUST install it, not substitute it. FORBIDDEN: using Word/WPS/Keynote/Pages instead; 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; }
  18. # ── Resolve PDF_SKILL_DIR & FONT_DIR ──
  19. _ENV_CHECK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
  20. PDF_SKILL_DIR="$(cd "$_ENV_CHECK_DIR/.." && pwd)"
  21. export PDF_SKILL_DIR
  22. if [ "$(uname -s)" = "Darwin" ]; then
  23. FONT_DIR="${HOME}/Library/Fonts"
  24. else
  25. FONT_DIR="/usr/share/fonts"
  26. fi
  27. export FONT_DIR
  28. # ── CORE (required): Python + ReportLab/pypdf toolchain + CJK font ──
  29. check "python3" command -v python3
  30. check "pikepdf" python3 -c "import pikepdf"
  31. check "pdfplumber" python3 -c "import pdfplumber"
  32. check "pypdf" python3 -c "import pypdf"
  33. check "reportlab" python3 -c "import reportlab"
  34. check "PyMuPDF" python3 -c "import fitz"
  35. # Font check: verify an embeddable CJK font is available (SimHei/Noto/WenQuanYi/Songti…)
  36. if command -v fc-list &>/dev/null; then
  37. fc-list :lang=zh 2>/dev/null | grep -qi "noto\|simhei\|simsun\|songti\|heiti\|wenquanyi\|yahei\|kai" \
  38. || { $QUIET || echo "MISSING (core): CJK font"; FAIL=1; }
  39. fi
  40. # ── OPTIONAL: Creative/HTML pipeline (Node + Playwright + Chromium) ──
  41. optional "node" command -v node
  42. optional "playwright (npm)" node -e "require('playwright')"
  43. # ── OPTIONAL: Academic/LaTeX pipeline (Tectonic) ──
  44. if [ -x "$PDF_SKILL_DIR/scripts/tectonic" ] || command -v tectonic &>/dev/null; then
  45. $QUIET || echo "optional OK: tectonic"
  46. else
  47. $QUIET || echo "optional MISSING: tectonic (LaTeX/Academic; install on demand)"
  48. fi
  49. # ── ON-DEMAND but NOT substitutable: Office→PDF (LibreOffice/soffice) ──
  50. required_on_demand "libreoffice (soffice)" command -v soffice
  51. $QUIET || echo "PDF_SKILL_DIR=$PDF_SKILL_DIR"
  52. $QUIET || echo "FONT_DIR=$FONT_DIR"
  53. return $FAIL 2>/dev/null || exit $FAIL