env_check.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #!/usr/bin/env bash
  2. # docx strict environment check
  3. # This script is authoritative for whether the skill may run.
  4. # Supports --level read|render|full (default: full).
  5. set -euo pipefail
  6. # --- Parse --level argument ---
  7. LEVEL="full"
  8. while [[ $# -gt 0 ]]; do
  9. case "$1" in
  10. --level)
  11. shift
  12. LEVEL="${1:-}"
  13. shift
  14. ;;
  15. *)
  16. echo "Unknown argument: $1" >&2
  17. exit 2
  18. ;;
  19. esac
  20. done
  21. case "$LEVEL" in
  22. read|render|full) ;;
  23. *)
  24. echo "Invalid --level value: '$LEVEL'. Must be one of: read, render, full" >&2
  25. exit 2
  26. ;;
  27. esac
  28. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  29. PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
  30. DOTNET_DIR="$SCRIPT_DIR/dotnet"
  31. DOTNET_REQUIRED_MAJOR=9
  32. export DOTNET_CLI_UI_LANGUAGE=en
  33. resolve_soffice_path() {
  34. local candidates=(
  35. "$(command -v soffice 2>/dev/null || true)"
  36. "$HOME/.local/bin/soffice"
  37. "$HOME/Applications/LibreOffice.app/Contents/MacOS/soffice"
  38. "/Applications/LibreOffice.app/Contents/MacOS/soffice"
  39. )
  40. local p
  41. for p in "${candidates[@]}"; do
  42. [ -n "$p" ] || continue
  43. [ -x "$p" ] || continue
  44. printf '%s\n' "$p"
  45. return 0
  46. done
  47. return 1
  48. }
  49. OS="unknown"
  50. case "$(uname -s)" in
  51. Darwin) OS="macos" ;;
  52. Linux) OS="linux"; grep -qi microsoft /proc/version 2>/dev/null && OS="wsl" ;;
  53. MINGW*|MSYS*|CYGWIN*) OS="windows-shell" ;;
  54. esac
  55. echo "=== docx Environment Check (level: $LEVEL) ==="
  56. echo ""
  57. STATUS="READY"
  58. # Failure buckets — populated by check_fail, rendered at the end so the
  59. # remediation section can speak to the user in their language without
  60. # repeating "go install python3" five times.
  61. MISSING_TOOLS="" # python3, unzip, soffice, pdftoppm, dotnet, pandoc, zip
  62. LOCALE_BROKEN=0
  63. LOCALE_HEALED=0 # set when locale self-heal succeeds; READY path still emits
  64. # a short advisory so the "see hint below" promise on the OK
  65. # line is actually fulfilled (and the user gets the persist
  66. # commands without having to fail the gate first).
  67. LOCALE_HEALED_FROM_LANG=""
  68. LOCALE_HEALED_FROM_CHARMAP=""
  69. LOCALE_HEALED_TO=""
  70. PERMS_BROKEN=0
  71. PROJECT_BROKEN=0
  72. check_ok() {
  73. printf "[OK] %-14s %s\n" "$1" "$2"
  74. }
  75. check_fail() {
  76. printf "[FAIL] %-14s %s\n" "$1" "$2"
  77. STATUS="NOT READY"
  78. }
  79. # Same as check_fail but also tag the failure bucket so the end-of-run
  80. # remediation section can be specific. We keep check_fail around because
  81. # daemon-side parsers may scan for "[FAIL]" prefixes verbatim.
  82. mark_missing_tool() {
  83. MISSING_TOOLS="$MISSING_TOOLS $1"
  84. }
  85. # --- read-level checks (always run) ---
  86. if ! command -v python3 >/dev/null 2>&1; then
  87. check_fail "python3" "not found"
  88. mark_missing_tool "python3"
  89. else
  90. check_ok "python3" "$(python3 --version 2>/dev/null | awk '{print $2}')"
  91. fi
  92. if ! command -v unzip >/dev/null 2>&1; then
  93. check_fail "unzip" "not found"
  94. mark_missing_tool "unzip"
  95. else
  96. check_ok "unzip" "available"
  97. fi
  98. # Locale check (3-step, with self-heal):
  99. # 1. $LANG explicitly carries utf-8? -> OK
  100. # 2. `locale charmap` reports UTF-8? -> OK (LANG empty is fine on macOS GUI shells)
  101. # 3. Try hot-patching LC_ALL=C.UTF-8 for the rest of this run and re-check.
  102. # This rescues subprocess contexts (skill spawners, daemon workers) where
  103. # $LANG is stripped but a UTF-8 locale is installed.
  104. # Otherwise -> fail with an actionable remediation in the summary block.
  105. current_lang="${LANG:-}"
  106. if [ -n "$current_lang" ] && echo "$current_lang" | grep -qi 'utf-8\|utf8'; then
  107. check_ok "locale" "$current_lang"
  108. else
  109. charmap="$(locale charmap 2>/dev/null || echo "unknown")"
  110. if echo "$charmap" | grep -qi 'utf-8\|utf8'; then
  111. check_ok "locale" "charmap=$charmap (LANG empty/non-UTF-8, but system charmap is UTF-8)"
  112. else
  113. healed=0
  114. for candidate in C.UTF-8 en_US.UTF-8; do
  115. # Use a subshell + locale charmap so we don't depend on `locale -a`
  116. # (which is slow on macOS and absent on minimal Linux images).
  117. if [ "$(LC_ALL="$candidate" locale charmap 2>/dev/null)" = "UTF-8" ]; then
  118. export LANG="$candidate"
  119. export LC_ALL="$candidate"
  120. check_ok "locale" "self-healed to $candidate (was LANG='$current_lang', charmap='$charmap'; see advisory below to persist)"
  121. healed=1
  122. LOCALE_HEALED=1
  123. LOCALE_HEALED_FROM_LANG="$current_lang"
  124. LOCALE_HEALED_FROM_CHARMAP="$charmap"
  125. LOCALE_HEALED_TO="$candidate"
  126. break
  127. fi
  128. done
  129. if [ "$healed" -eq 0 ]; then
  130. check_fail "locale" "LANG='$current_lang', charmap='$charmap' — neither is UTF-8 and self-heal failed"
  131. LOCALE_BROKEN=1
  132. fi
  133. fi
  134. fi
  135. perm_issues=0
  136. for s in "$SCRIPT_DIR"/*.sh; do
  137. if [ -f "$s" ] && [ ! -x "$s" ]; then
  138. perm_issues=$((perm_issues + 1))
  139. fi
  140. done
  141. if [ "$perm_issues" -eq 0 ]; then
  142. check_ok "permissions" "all scripts executable"
  143. else
  144. check_fail "permissions" "$perm_issues script(s) not executable"
  145. PERMS_BROKEN=1
  146. fi
  147. # --- render-level checks (render + full) ---
  148. if [[ "$LEVEL" == "render" || "$LEVEL" == "full" ]]; then
  149. SOFFICE_PATH="$(resolve_soffice_path 2>/dev/null || true)"
  150. if [ -z "$SOFFICE_PATH" ]; then
  151. check_fail "soffice" "not found"
  152. mark_missing_tool "soffice"
  153. else
  154. check_ok "soffice" "$SOFFICE_PATH"
  155. fi
  156. if ! command -v pdftoppm >/dev/null 2>&1; then
  157. check_fail "pdftoppm" "not found"
  158. mark_missing_tool "pdftoppm"
  159. else
  160. check_ok "pdftoppm" "available"
  161. fi
  162. fi
  163. # --- full-level checks (full only) ---
  164. if [[ "$LEVEL" == "full" ]]; then
  165. if ! command -v dotnet >/dev/null 2>&1; then
  166. check_fail "dotnet" "not found"
  167. mark_missing_tool "dotnet"
  168. else
  169. ver="$(dotnet --version 2>/dev/null || echo 0.0.0)"
  170. major="${ver%%.*}"
  171. if [ "$major" -ge "$DOTNET_REQUIRED_MAJOR" ] 2>/dev/null; then
  172. check_ok "dotnet" "$ver (>= $DOTNET_REQUIRED_MAJOR.0)"
  173. else
  174. check_fail "dotnet" "$ver (requires >= $DOTNET_REQUIRED_MAJOR.0)"
  175. mark_missing_tool "dotnet"
  176. fi
  177. fi
  178. if ! command -v pandoc >/dev/null 2>&1; then
  179. check_fail "pandoc" "not found"
  180. mark_missing_tool "pandoc"
  181. else
  182. check_ok "pandoc" "$(pandoc --version 2>/dev/null | head -1 | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' || echo '?')"
  183. fi
  184. if ! command -v zip >/dev/null 2>&1; then
  185. check_fail "zip" "not found"
  186. mark_missing_tool "zip"
  187. else
  188. check_ok "zip" "available"
  189. fi
  190. if [ ! -d "$DOTNET_DIR" ]; then
  191. check_fail "project" "directory not found: $DOTNET_DIR"
  192. PROJECT_BROKEN=1
  193. else
  194. if [ -f "$DOTNET_DIR/MiniMaxAIDocx.Cli/bin/Debug/net10.0/MiniMaxAIDocx.Cli.dll" ] || \
  195. [ -f "$DOTNET_DIR/MiniMaxAIDocx.Cli/bin/Debug/net9.0/MiniMaxAIDocx.Cli.dll" ] || \
  196. [ -f "$DOTNET_DIR/MiniMaxAIDocx.Cli/bin/Debug/net8.0/MiniMaxAIDocx.Cli.dll" ]; then
  197. check_ok "project" "built"
  198. else
  199. if dotnet restore "$DOTNET_DIR" --verbosity quiet >/dev/null 2>&1 && \
  200. dotnet build "$DOTNET_DIR" --verbosity quiet --no-restore >/dev/null 2>&1; then
  201. check_ok "project" "restore+build succeeded"
  202. else
  203. check_fail "project" "restore/build failed"
  204. PROJECT_BROKEN=1
  205. fi
  206. fi
  207. fi
  208. fi
  209. echo ""
  210. if [ "$STATUS" = "READY" ]; then
  211. echo "Status: READY"
  212. # Advisory — fulfils the "see advisory below to persist" promise on the
  213. # self-heal OK line. Not a failure (we keep exit 0 and READY status), just a
  214. # nudge so the next subprocess does not have to self-heal again.
  215. if [ "$LOCALE_HEALED" -eq 1 ]; then
  216. echo ""
  217. echo "----- Advisory -----"
  218. echo ""
  219. echo "[locale] Self-healed to '$LOCALE_HEALED_TO' for this run (was LANG='$LOCALE_HEALED_FROM_LANG', charmap='$LOCALE_HEALED_FROM_CHARMAP')."
  220. echo " Skill scripts in THIS shell will keep working — no further action needed."
  221. echo " But every NEW subprocess starts from scratch and must self-heal again."
  222. echo " To make UTF-8 sticky for the Mavis daemon and its workers:"
  223. if [ "$OS" = "macos" ]; then
  224. echo " launchctl setenv LANG en_US.UTF-8"
  225. echo " launchctl setenv LC_ALL en_US.UTF-8"
  226. echo " # then restart the daemon so it inherits the new env"
  227. elif [ "$OS" = "linux" ] || [ "$OS" = "wsl" ]; then
  228. echo " # daemon launched via systemd: add to your unit file's [Service] section:"
  229. echo " Environment=LANG=en_US.UTF-8"
  230. echo " Environment=LC_ALL=en_US.UTF-8"
  231. echo " # daemon launched from shell: export the two vars in the parent shell"
  232. echo " # before starting the daemon (NOT in ~/.zshrc — see below)."
  233. fi
  234. echo " Note: editing ~/.zshrc does NOT help — Mavis subprocesses are non-interactive"
  235. echo " and never source it. Use the launchctl / systemd / parent-shell paths above."
  236. fi
  237. else
  238. echo "Status: NOT READY"
  239. echo ""
  240. echo "----- Remediation -----"
  241. # Locale block — most common subprocess/skill-spawner pitfall, surface first.
  242. if [ "$LOCALE_BROKEN" -eq 1 ]; then
  243. echo ""
  244. echo "[locale] No UTF-8 locale is exposed to this process."
  245. echo " Caller (one-shot, no system changes):"
  246. echo " LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 bash $0 --level $LEVEL"
  247. if [ "$OS" = "macos" ]; then
  248. echo " macOS (persist for GUI apps + Mavis daemon; survives reboot once set in launchd):"
  249. echo " launchctl setenv LANG en_US.UTF-8"
  250. echo " launchctl setenv LC_ALL en_US.UTF-8"
  251. echo " Then restart the daemon so it inherits the new env."
  252. elif [ "$OS" = "linux" ] || [ "$OS" = "wsl" ]; then
  253. echo " Linux/WSL (persist for interactive shells):"
  254. echo " echo 'export LANG=en_US.UTF-8' >> ~/.bashrc"
  255. echo " echo 'export LC_ALL=en_US.UTF-8' >> ~/.bashrc"
  256. echo " If your distro has no en_US.UTF-8, install glibc-locale-source / glibc-langpack-en"
  257. echo " or add 'en_US.UTF-8 UTF-8' to /etc/locale.gen and run 'sudo locale-gen'."
  258. fi
  259. echo " Note: editing ~/.zshrc does NOT help — Mavis subprocesses are non-interactive"
  260. echo " and never source it. Use launchctl (macOS) or the caller-prefix approach."
  261. fi
  262. # Permissions block — cheap to fix, surface before tool installs.
  263. if [ "$PERMS_BROKEN" -eq 1 ]; then
  264. echo ""
  265. echo "[permissions] Some scripts are not executable. Fix:"
  266. echo " chmod +x $SCRIPT_DIR/*.sh"
  267. fi
  268. # Missing tools block — show one composite install hint instead of one per tool.
  269. if [ -n "$MISSING_TOOLS" ]; then
  270. # Squash leading space + dedup.
  271. tools="$(echo "$MISSING_TOOLS" | tr ' ' '\n' | awk 'NF' | sort -u | tr '\n' ' ')"
  272. echo ""
  273. echo "[missing tools] $tools"
  274. if [ "$OS" = "macos" ]; then
  275. echo " Recommended: bash $SCRIPT_DIR/setup.sh"
  276. echo " This installs everything via Homebrew without sudo."
  277. elif [ "$OS" = "linux" ] || [ "$OS" = "wsl" ]; then
  278. # apt is the most common; pacman/dnf users will know what to do.
  279. echo " Debian/Ubuntu: sudo apt-get update && sudo apt-get install -y \\"
  280. echo " python3 unzip zip libreoffice poppler-utils pandoc"
  281. echo " dotnet: see https://learn.microsoft.com/dotnet/core/install/linux"
  282. echo " Or, if package manager is unavailable: bash $SCRIPT_DIR/setup.sh"
  283. else
  284. echo " Run: bash $SCRIPT_DIR/setup.sh"
  285. fi
  286. fi
  287. # dotnet project build failure — usually means dotnet SDK is too old or
  288. # NuGet feed is blocked. Not solvable by re-running setup blindly.
  289. if [ "$PROJECT_BROKEN" -eq 1 ]; then
  290. echo ""
  291. echo "[dotnet project] build/restore failed. To diagnose:"
  292. echo " dotnet restore $DOTNET_DIR --verbosity normal"
  293. echo " dotnet build $DOTNET_DIR --verbosity normal --no-restore"
  294. echo " Common causes: dotnet SDK < $DOTNET_REQUIRED_MAJOR.0, blocked NuGet feed,"
  295. echo " or a clobbered intermediate build. Try 'rm -rf $DOTNET_DIR/**/{bin,obj}' then rebuild."
  296. fi
  297. # Level-specific tail.
  298. echo ""
  299. if [[ "$LEVEL" == "read" ]]; then
  300. echo "The read-level gate requires: python3, unzip, UTF-8 locale, executable scripts."
  301. echo "Re-check with: bash $0 --level read"
  302. elif [[ "$LEVEL" == "render" ]]; then
  303. echo "The render-level gate requires read-level items plus: soffice, pdftoppm."
  304. echo "Re-check with: bash $0 --level render"
  305. else
  306. echo "The full-level gate requires render-level items plus: dotnet>=$DOTNET_REQUIRED_MAJOR, pandoc, zip, built dotnet project."
  307. echo "Re-check with: bash $0 --level full"
  308. fi
  309. exit 1
  310. fi