#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --input <файл или папка> [--outdir <папка>] [--force]" } input="" outdir="pdf" force=0 while [[ $# -gt 0 ]]; do case "$1" in --input) input="${2:-}"; shift 2 ;; --outdir) outdir="${2:-}"; shift 2 ;; --force) force=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$input" || ! -e "$input" ]]; then echo "Укажите существующий файл или папку через --input" >&2 exit 2 fi find_soffice() { if [[ -n "${SOFFICE_BIN:-}" && -x "${SOFFICE_BIN}" ]]; then printf '%s\n' "$SOFFICE_BIN" return 0 fi local candidate for candidate in soffice libreoffice; do if command -v "$candidate" >/dev/null 2>&1; then command -v "$candidate" return 0 fi done for candidate in "/Applications/LibreOffice.app/Contents/MacOS/soffice" "/usr/lib/libreoffice/program/soffice"; do if [[ -x "$candidate" ]]; then printf '%s\n' "$candidate" return 0 fi done return 1 } soffice_bin="$(find_soffice || true)" if [[ -z "$soffice_bin" ]]; then echo "LibreOffice не найден. Установите soffice и повторите запуск." >&2 exit 2 fi files=() if [[ -f "$input" ]]; then files+=("$input") elif [[ -d "$input" ]]; then while IFS= read -r -d '' file; do files+=("$file") done < <(find "$input" -type f \( -iname '*.pptx' -o -iname '*.ppt' -o -iname '*.odp' \) -print0) else echo "Путь не является файлом или папкой: $input" >&2 exit 2 fi if [[ ${#files[@]} -eq 0 ]]; then echo "Презентации не найдены" >&2 exit 1 fi mkdir -p "$outdir" converted=0 failed=0 for source in "${files[@]}"; do name="$(basename "${source%.*}")" target="$outdir/$name.pdf" if [[ -e "$target" && "$force" -eq 0 ]]; then echo "Пропуск, уже существует: $target" continue fi "$soffice_bin" --headless --convert-to pdf --outdir "$outdir" "$source" >/dev/null 2>&1 || true if [[ -f "$target" ]]; then echo "Готово: $target" converted=$((converted + 1)) else echo "Не удалось конвертировать: $source" >&2 failed=$((failed + 1)) fi done echo "Сконвертировано файлов: $converted, ошибок: $failed" [[ "$failed" -eq 0 ]]