setup.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #!/usr/bin/env bash
  2. # docx Environment Setup & Initialization Script
  3. # Focus: macOS full-fidelity DOCX environment with no sudo requirement.
  4. # Other platforms keep best-effort package-manager installs.
  5. set -euo pipefail
  6. export DOTNET_CLI_UI_LANGUAGE=en
  7. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  8. PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
  9. DOTNET_DIR="$SCRIPT_DIR/dotnet"
  10. LOG_FILE="$PROJECT_DIR/.setup.log"
  11. DOTNET_REQUIRED_MAJOR=9
  12. RED='\033[0;31m'
  13. GREEN='\033[0;32m'
  14. YELLOW='\033[1;33m'
  15. BLUE='\033[0;34m'
  16. NC='\033[0m'
  17. log() { echo -e "${GREEN}[OK]${NC} $*"; }
  18. warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
  19. fail() { echo -e "${RED}[FAIL]${NC} $*"; }
  20. info() { echo -e "${BLUE}[INFO]${NC} $*"; }
  21. step() { echo -e "\n${BLUE}=== $* ===${NC}"; }
  22. OS="unknown"
  23. PKG_MGR="unknown"
  24. ARCH="$(uname -m)"
  25. BREW_BIN=""
  26. BREW_PREFIX=""
  27. path_prepend_now() {
  28. case ":${PATH}:" in
  29. *":$1:"*) ;;
  30. *) export PATH="$1:$PATH" ;;
  31. esac
  32. }
  33. ensure_shell_export() {
  34. local file="$1"
  35. local line="$2"
  36. touch "$file"
  37. grep -Fqx "$line" "$file" 2>/dev/null || printf '%s\n' "$line" >> "$file"
  38. }
  39. persist_macos_paths() {
  40. [ "$OS" = "macos" ] || return 0
  41. mkdir -p "$HOME/.local/bin"
  42. path_prepend_now "$HOME/.dotnet"
  43. path_prepend_now "$HOME/.local/bin"
  44. if [ -n "$BREW_PREFIX" ]; then
  45. path_prepend_now "$BREW_PREFIX/bin"
  46. path_prepend_now "$BREW_PREFIX/sbin"
  47. fi
  48. local lines=(
  49. 'export PATH="$HOME/.dotnet:$PATH"'
  50. 'export PATH="$HOME/.local/bin:$PATH"'
  51. )
  52. if [ -n "$BREW_PREFIX" ]; then
  53. lines+=("export PATH=\"$BREW_PREFIX/bin:$BREW_PREFIX/sbin:\$PATH\"")
  54. fi
  55. for rc in "$HOME/.zprofile" "$HOME/.zshrc"; do
  56. for line in "${lines[@]}"; do
  57. ensure_shell_export "$rc" "$line"
  58. done
  59. done
  60. }
  61. resolve_brew() {
  62. if [ -n "$BREW_BIN" ] && [ -x "$BREW_BIN" ]; then
  63. return 0
  64. fi
  65. if command -v brew >/dev/null 2>&1; then
  66. BREW_BIN="$(command -v brew)"
  67. BREW_PREFIX="$($BREW_BIN --prefix)"
  68. return 0
  69. fi
  70. if [ -x /opt/homebrew/bin/brew ]; then
  71. BREW_BIN=/opt/homebrew/bin/brew
  72. BREW_PREFIX=/opt/homebrew
  73. path_prepend_now /opt/homebrew/bin
  74. path_prepend_now /opt/homebrew/sbin
  75. return 0
  76. fi
  77. if [ -x /usr/local/bin/brew ]; then
  78. BREW_BIN=/usr/local/bin/brew
  79. BREW_PREFIX=/usr/local
  80. path_prepend_now /usr/local/bin
  81. path_prepend_now /usr/local/sbin
  82. return 0
  83. fi
  84. return 1
  85. }
  86. ensure_brew_for_macos() {
  87. if resolve_brew; then
  88. log "Homebrew available: $BREW_BIN"
  89. persist_macos_paths
  90. return 0
  91. fi
  92. fail "Homebrew is required on macOS to install pandoc, poppler, python3, and LibreOffice without sudo."
  93. fail "Install Homebrew first, then re-run this script: https://brew.sh"
  94. return 1
  95. }
  96. resolve_soffice_path() {
  97. local candidates=(
  98. "$(command -v soffice 2>/dev/null || true)"
  99. "$HOME/.local/bin/soffice"
  100. "$HOME/Applications/LibreOffice.app/Contents/MacOS/soffice"
  101. "/Applications/LibreOffice.app/Contents/MacOS/soffice"
  102. )
  103. local p
  104. for p in "${candidates[@]}"; do
  105. [ -n "$p" ] || continue
  106. [ -x "$p" ] || continue
  107. printf '%s\n' "$p"
  108. return 0
  109. done
  110. return 1
  111. }
  112. symlink_soffice_user_bin() {
  113. local soffice_path="$1"
  114. mkdir -p "$HOME/.local/bin"
  115. ln -sfn "$soffice_path" "$HOME/.local/bin/soffice"
  116. path_prepend_now "$HOME/.local/bin"
  117. persist_macos_paths
  118. }
  119. detect_platform() {
  120. case "$(uname -s)" in
  121. Darwin)
  122. OS="macos"
  123. PKG_MGR="brew"
  124. resolve_brew || true
  125. ;;
  126. Linux)
  127. OS="linux"
  128. if [ -f /etc/os-release ]; then
  129. . /etc/os-release
  130. case "${ID:-}" in
  131. ubuntu|debian|linuxmint|pop) PKG_MGR="apt" ;;
  132. fedora|rhel|centos|rocky|alma) PKG_MGR="dnf" ;;
  133. arch|manjaro|endeavouros) PKG_MGR="pacman" ;;
  134. opensuse*|sles) PKG_MGR="zypper" ;;
  135. alpine) PKG_MGR="apk" ;;
  136. *) PKG_MGR="unknown" ;;
  137. esac
  138. fi
  139. grep -qi microsoft /proc/version 2>/dev/null && OS="wsl"
  140. ;;
  141. MINGW*|MSYS*|CYGWIN*)
  142. OS="windows-git-bash"
  143. PKG_MGR="none"
  144. cat <<'EOF' >&2
  145. [FAIL] You are running setup.sh under git-bash / MSYS / Cygwin on Windows.
  146. This script will NOT install anything here — it cannot reach winget/choco/scoop
  147. reliably from a POSIX-emulation shell, and any installs would land in the
  148. wrong PATH for the rest of the toolchain (dotnet build needs a Windows-native
  149. UTF-8 console, soffice.exe needs to be discoverable to PowerShell, etc.).
  150. Run the PowerShell setup instead, in an elevated or normal PowerShell window:
  151. powershell -ExecutionPolicy Bypass -File scripts\setup.ps1
  152. # or, if pwsh 7+ is installed
  153. pwsh -ExecutionPolicy Bypass -File scripts\setup.ps1
  154. Then verify with:
  155. powershell -ExecutionPolicy Bypass -File scripts\env_check.ps1 -Level Read
  156. EOF
  157. exit 2
  158. ;;
  159. esac
  160. echo "Platform: $OS ($ARCH), Package Manager: $PKG_MGR"
  161. }
  162. install_dotnet() {
  163. step "Checking .NET SDK"
  164. if command -v dotnet >/dev/null 2>&1; then
  165. local ver major
  166. ver="$(dotnet --version 2>/dev/null || echo 0)"
  167. major="${ver%%.*}"
  168. if [ "$major" -ge "$DOTNET_REQUIRED_MAJOR" ] 2>/dev/null; then
  169. log "dotnet $ver already installed (>= $DOTNET_REQUIRED_MAJOR.0 OK)"
  170. return 0
  171. fi
  172. warn "dotnet $ver found but < $DOTNET_REQUIRED_MAJOR.0, upgrading..."
  173. fi
  174. if [ "$OS" = "macos" ]; then
  175. info "Installing .NET SDK via dotnet-install script to ~/.dotnet (no sudo)..."
  176. curl -fsSL "https://dot.net/v1/dotnet-install.sh" -o /tmp/dotnet-install.sh
  177. chmod +x /tmp/dotnet-install.sh
  178. /tmp/dotnet-install.sh --channel "$DOTNET_REQUIRED_MAJOR.0" --install-dir "$HOME/.dotnet"
  179. persist_macos_paths
  180. else
  181. info "Installing .NET SDK..."
  182. case "$PKG_MGR" in
  183. apt)
  184. curl -fsSL "https://dot.net/v1/dotnet-install.sh" -o /tmp/dotnet-install.sh
  185. chmod +x /tmp/dotnet-install.sh
  186. /tmp/dotnet-install.sh --channel "$DOTNET_REQUIRED_MAJOR.0" --install-dir "$HOME/.dotnet"
  187. export PATH="$HOME/.dotnet:$PATH"
  188. ;;
  189. dnf) sudo dnf install -y dotnet-sdk-$DOTNET_REQUIRED_MAJOR.0 ;;
  190. pacman) sudo pacman -S --noconfirm dotnet-sdk ;;
  191. zypper) sudo zypper install -y dotnet-sdk-$DOTNET_REQUIRED_MAJOR.0 ;;
  192. apk) apk add --no-cache dotnet8-sdk ;;
  193. *)
  194. fail "Could not auto-install dotnet on this platform. Install manually from https://dotnet.microsoft.com/download"
  195. return 1
  196. ;;
  197. esac
  198. fi
  199. if command -v dotnet >/dev/null 2>&1; then
  200. log "dotnet $(dotnet --version) installed"
  201. else
  202. fail "dotnet installation failed"
  203. return 1
  204. fi
  205. }
  206. install_python3() {
  207. step "Checking python3"
  208. if command -v python3 >/dev/null 2>&1; then
  209. log "python3 $(python3 --version 2>/dev/null | awk '{print $2}') already installed"
  210. return 0
  211. fi
  212. info "Installing python3..."
  213. case "$OS:$PKG_MGR" in
  214. macos:brew)
  215. ensure_brew_for_macos
  216. "$BREW_BIN" install python
  217. persist_macos_paths
  218. ;;
  219. linux:apt|wsl:apt) sudo apt-get update -qq && sudo apt-get install -y -qq python3 ;;
  220. linux:dnf|wsl:dnf) sudo dnf install -y python3 ;;
  221. linux:pacman|wsl:pacman) sudo pacman -S --noconfirm python ;;
  222. linux:zypper|wsl:zypper) sudo zypper install -y python3 ;;
  223. linux:apk|wsl:apk) apk add --no-cache python3 ;;
  224. *)
  225. fail "python3 is required and could not be auto-installed"
  226. return 1
  227. ;;
  228. esac
  229. command -v python3 >/dev/null 2>&1 || { fail "python3 installation failed"; return 1; }
  230. log "python3 installed"
  231. }
  232. install_pandoc() {
  233. step "Checking pandoc"
  234. if command -v pandoc >/dev/null 2>&1; then
  235. log "pandoc $(pandoc --version | head -1 | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?') already installed"
  236. return 0
  237. fi
  238. info "Installing pandoc..."
  239. case "$OS:$PKG_MGR" in
  240. macos:brew)
  241. ensure_brew_for_macos
  242. "$BREW_BIN" install pandoc
  243. persist_macos_paths
  244. ;;
  245. linux:apt|wsl:apt) sudo apt-get update -qq && sudo apt-get install -y -qq pandoc ;;
  246. linux:dnf|wsl:dnf) sudo dnf install -y pandoc ;;
  247. linux:pacman|wsl:pacman) sudo pacman -S --noconfirm pandoc ;;
  248. linux:zypper|wsl:zypper) sudo zypper install -y pandoc ;;
  249. linux:apk|wsl:apk) apk add --no-cache pandoc ;;
  250. *)
  251. fail "pandoc is required and could not be auto-installed"
  252. return 1
  253. ;;
  254. esac
  255. command -v pandoc >/dev/null 2>&1 || { fail "pandoc installation failed"; return 1; }
  256. log "pandoc installed"
  257. }
  258. install_poppler() {
  259. step "Checking pdftoppm (poppler)"
  260. if command -v pdftoppm >/dev/null 2>&1; then
  261. log "pdftoppm already installed"
  262. return 0
  263. fi
  264. info "Installing poppler..."
  265. case "$OS:$PKG_MGR" in
  266. macos:brew)
  267. ensure_brew_for_macos
  268. "$BREW_BIN" install poppler
  269. persist_macos_paths
  270. ;;
  271. linux:apt|wsl:apt) sudo apt-get update -qq && sudo apt-get install -y -qq poppler-utils ;;
  272. linux:dnf|wsl:dnf) sudo dnf install -y poppler-utils ;;
  273. linux:pacman|wsl:pacman) sudo pacman -S --noconfirm poppler ;;
  274. linux:zypper|wsl:zypper) sudo zypper install -y poppler-tools ;;
  275. linux:apk|wsl:apk) apk add --no-cache poppler-utils ;;
  276. *)
  277. fail "pdftoppm is required and could not be auto-installed"
  278. return 1
  279. ;;
  280. esac
  281. command -v pdftoppm >/dev/null 2>&1 || { fail "pdftoppm installation failed"; return 1; }
  282. log "pdftoppm installed"
  283. }
  284. install_soffice() {
  285. step "Checking LibreOffice/soffice"
  286. if local_path="$(resolve_soffice_path 2>/dev/null)"; then
  287. log "soffice available at $local_path"
  288. [ "$OS" = "macos" ] && symlink_soffice_user_bin "$local_path"
  289. return 0
  290. fi
  291. info "Installing LibreOffice..."
  292. case "$OS:$PKG_MGR" in
  293. macos:brew)
  294. ensure_brew_for_macos
  295. mkdir -p "$HOME/Applications"
  296. HOMEBREW_CASK_OPTS="--appdir=$HOME/Applications" "$BREW_BIN" install --cask libreoffice
  297. ;;
  298. linux:apt|wsl:apt) sudo apt-get update -qq && sudo apt-get install -y -qq libreoffice-core ;;
  299. linux:dnf|wsl:dnf) sudo dnf install -y libreoffice-core ;;
  300. linux:pacman|wsl:pacman) sudo pacman -S --noconfirm libreoffice-still ;;
  301. linux:zypper|wsl:zypper) sudo zypper install -y libreoffice ;;
  302. linux:apk|wsl:apk) apk add --no-cache libreoffice ;;
  303. *)
  304. fail "LibreOffice is required and could not be auto-installed"
  305. return 1
  306. ;;
  307. esac
  308. local local_path
  309. local_path="$(resolve_soffice_path 2>/dev/null || true)"
  310. if [ -z "$local_path" ]; then
  311. fail "LibreOffice installation finished but soffice is still not discoverable"
  312. return 1
  313. fi
  314. [ "$OS" = "macos" ] && symlink_soffice_user_bin "$local_path"
  315. log "soffice installed at $local_path"
  316. }
  317. install_zip_tools() {
  318. step "Checking zip/unzip"
  319. local need_zip=false need_unzip=false
  320. command -v zip >/dev/null 2>&1 || need_zip=true
  321. command -v unzip >/dev/null 2>&1 || need_unzip=true
  322. if ! $need_zip && ! $need_unzip; then
  323. log "zip/unzip already installed"
  324. return 0
  325. fi
  326. info "Installing zip/unzip..."
  327. case "$PKG_MGR" in
  328. brew) ensure_brew_for_macos; "$BREW_BIN" install zip unzip ;;
  329. apt) sudo apt-get update -qq && sudo apt-get install -y -qq zip unzip ;;
  330. dnf) sudo dnf install -y zip unzip ;;
  331. pacman) sudo pacman -S --noconfirm zip unzip ;;
  332. zypper) sudo zypper install -y zip unzip ;;
  333. apk) apk add --no-cache zip unzip ;;
  334. *) warn "Could not auto-install zip/unzip" ;;
  335. esac
  336. }
  337. check_locale() {
  338. step "Checking locale and encoding"
  339. local current_lang="${LANG:-not set}"
  340. if echo "$current_lang" | grep -qi 'utf-8\|utf8'; then
  341. log "Locale supports UTF-8: LANG=$current_lang"
  342. else
  343. warn "Locale may not support UTF-8: LANG=$current_lang"
  344. warn "CJK processing needs UTF-8. Set: export LANG=en_US.UTF-8"
  345. fi
  346. }
  347. check_nuget_config() {
  348. step "Checking NuGet configuration"
  349. if dotnet nuget list source 2>/dev/null | grep -q 'nuget.org'; then
  350. log "nuget.org source is configured"
  351. else
  352. warn "nuget.org not in sources. Adding..."
  353. dotnet nuget add source "https://api.nuget.org/v3/index.json" --name "nuget.org" >/dev/null 2>&1 || true
  354. fi
  355. }
  356. fix_permissions() {
  357. step "Setting script permissions"
  358. local scripts=(
  359. "$SCRIPT_DIR/env_check.sh"
  360. "$SCRIPT_DIR/docx_preview.sh"
  361. "$SCRIPT_DIR/doc_to_docx.sh"
  362. "$SCRIPT_DIR/setup.sh"
  363. )
  364. local s
  365. for s in "${scripts[@]}"; do
  366. [ -f "$s" ] || continue
  367. chmod +x "$s"
  368. done
  369. log "script permissions updated"
  370. }
  371. build_project() {
  372. step "Building docx .NET project"
  373. [ -d "$DOTNET_DIR" ] || { fail "Dotnet project directory not found: $DOTNET_DIR"; return 1; }
  374. cd "$DOTNET_DIR"
  375. info "Restoring NuGet packages..."
  376. if ! dotnet restore --verbosity quiet 2>>"$LOG_FILE"; then
  377. fail "NuGet restore failed. Check $LOG_FILE for details."
  378. return 1
  379. fi
  380. log "NuGet packages restored"
  381. info "Building project..."
  382. if ! dotnet build --verbosity quiet --no-restore 2>>"$LOG_FILE"; then
  383. fail "Build failed. Check $LOG_FILE for details."
  384. return 1
  385. fi
  386. log "Project built successfully"
  387. cd "$PROJECT_DIR"
  388. }
  389. verify_installation() {
  390. step "Verification Test"
  391. local test_output="/tmp/docx-setup-test-$$.docx"
  392. info "Creating a test document..."
  393. if cd "$DOTNET_DIR" && dotnet run --project MiniMaxAIDocx.Cli -- create --type report --output "$test_output" --title "Setup Test" 2>>"$LOG_FILE"; then
  394. log "Test document created: $test_output"
  395. rm -f "$test_output"
  396. log "Test passed — docx is ready to use"
  397. else
  398. fail "Test document creation failed. Check $LOG_FILE for details."
  399. return 1
  400. fi
  401. cd "$PROJECT_DIR"
  402. }
  403. check_fonts() {
  404. step "Checking fonts"
  405. if [ "$OS" = "macos" ]; then
  406. log "macOS built-in CJK fonts available"
  407. if [ -d "/Applications/Microsoft Word.app" ] || [ -d "/Applications/Microsoft Office" ]; then
  408. log "Microsoft Office fonts available"
  409. else
  410. warn "Microsoft Office not installed — Calibri/Cambria may be missing"
  411. fi
  412. else
  413. info "Font check is informational on non-macOS platforms"
  414. fi
  415. }
  416. print_summary() {
  417. step "Setup Complete"
  418. echo ""
  419. echo " Environment: $OS ($ARCH)"
  420. echo " dotnet: $(dotnet --version 2>/dev/null || echo 'NOT FOUND')"
  421. echo " python3: $(python3 --version 2>/dev/null | awk '{print $2}' || echo 'NOT FOUND')"
  422. echo " pandoc: $(pandoc --version 2>/dev/null | head -1 | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' || echo 'NOT FOUND')"
  423. echo " pdftoppm: $(command -v pdftoppm 2>/dev/null || echo 'NOT FOUND')"
  424. echo " soffice: $(resolve_soffice_path 2>/dev/null || echo 'NOT FOUND')"
  425. echo " Project: $DOTNET_DIR"
  426. echo ""
  427. echo " Mandatory preflight: bash $SCRIPT_DIR/env_check.sh"
  428. echo " Log file: $LOG_FILE"
  429. }
  430. main() {
  431. echo "============================================"
  432. echo " docx Setup & Initialization"
  433. echo " $(date '+%Y-%m-%d %H:%M:%S')"
  434. echo "============================================"
  435. : > "$LOG_FILE"
  436. detect_platform
  437. local skip_verify=false
  438. local skip_optional=false
  439. for arg in "$@"; do
  440. case "$arg" in
  441. --minimal) skip_optional=true ;;
  442. --skip-verify) skip_verify=true ;;
  443. --help|-h)
  444. echo "Usage: setup.sh [options]"
  445. echo " --minimal Skip font check only (runtime deps are still installed)"
  446. echo " --skip-verify Skip the final create-document verification"
  447. exit 0
  448. ;;
  449. esac
  450. done
  451. [ "$OS" = "macos" ] && ensure_brew_for_macos
  452. install_dotnet
  453. install_python3
  454. install_pandoc
  455. install_poppler
  456. install_soffice
  457. install_zip_tools
  458. check_locale
  459. check_nuget_config
  460. fix_permissions
  461. build_project
  462. if ! $skip_optional; then
  463. check_fonts
  464. fi
  465. if ! $skip_verify; then
  466. verify_installation
  467. fi
  468. print_summary
  469. }
  470. main "$@"