#!/usr/bin/env bash #=== HELP START === # install-pandoc.sh – Installs the Pandoc binary # # Usage: # sudo ./install-pandoc.sh [options] [] # # Options: # -a, --arch Target architecture (default: auto-detected) # -d, --dir Installation directory (default: /usr/local/bin) # -n, --dry-run Download and verify, but DO NOT install # -h, --help Show this help message # # Arguments: # Pandoc version to install (default: latest) # # Behaviour: # • If pandoc is already in PATH and no version was requested, the script exits. # • If a specific version was requested, the installed version is only upgraded if it differs. #=== HELP END === set -euo pipefail REPO="jgm/pandoc" # ─────────────────────────────────────────────── # Defaults INSTALL_DIR_DEFAULT="/usr/local/bin" INSTALL_DIR="$INSTALL_DIR_DEFAULT" ARCH_INPUT="" VERSION="latest" DRY_RUN=false USER_VERSION_SPECIFIED=false # ─────────────────────────────────────────────── # 0 Help function show_help() { sed -n '/^#=== HELP START ===/,/^#=== HELP END ===/ { /^#=== HELP START ===/d /^#=== HELP END ===/d s/^#// p }' "$0" } # 1 Parse options POSITIONAL=() while [[ $# -gt 0 ]]; do case "$1" in -h|--help) show_help; exit 0 ;; -n|--dry-run) DRY_RUN=true; shift ;; -a|--arch) [[ $# -lt 2 ]] && { echo "❌ Option $1 requires an argument"; exit 1; } ARCH_INPUT="$2"; shift 2 ;; -d|--dir) [[ $# -lt 2 ]] && { echo "❌ Option $1 requires an argument"; exit 1; } INSTALL_DIR="$2"; shift 2 ;; *) POSITIONAL+=("$1"); shift ;; esac done if [[ ${#POSITIONAL[@]} -gt 0 ]]; then VERSION="${POSITIONAL[0]}" USER_VERSION_SPECIFIED=true fi # 2 Detect / normalize architecture detect_arch() { case "$(uname -m)" in x86_64) echo "linux-amd64" ;; aarch64|arm64) echo "linux-arm64" ;; *) echo "unsupported" ;; esac } ARCH_TAG="" if [[ -n "$ARCH_INPUT" ]]; then case "$ARCH_INPUT" in amd64|linux-amd64) ARCH_TAG="linux-amd64" ;; arm64|aarch64|linux-arm64) ARCH_TAG="linux-arm64" ;; *) echo "❌ Unknown architecture: $ARCH_INPUT"; exit 1 ;; esac else ARCH_TAG=$(detect_arch) if [[ "$ARCH_TAG" == "unsupported" ]]; then echo "❌ Automatic architecture detection failed. Please use -a." exit 1 fi fi # 3 Pre-check existing installation if command -v pandoc >/dev/null; then INSTALLED_PATH=$(command -v pandoc) INSTALLED_VER=$(pandoc -v | head -n1 | awk '{print $2}') if ! $USER_VERSION_SPECIFIED; then echo "✅ pandoc already present at ${INSTALLED_PATH} (version ${INSTALLED_VER}) – nothing to do." exit 0 else if [[ "$INSTALLED_VER" == "$VERSION" ]]; then echo "✅ pandoc ${INSTALLED_VER} already installed – requested version matches." exit 0 else echo "ℹ️ pandoc ${INSTALLED_VER} installed, upgrading to ${VERSION}..." fi fi fi # 4 Check required tools need() { command -v "$1" >/dev/null || { echo "$1 is missing"; exit 1; }; } need curl; need tar; need grep; need sed; need awk; need jq # 5 Fetch release info if [[ "$VERSION" == "latest" ]]; then API_URL="https://api.github.com/repos/${REPO}/releases/latest" else API_URL="https://api.github.com/repos/${REPO}/releases/tags/${VERSION}" fi echo "🔍 Fetching release info (${API_URL})…" JSON=$(curl -fsSL "$API_URL") || { echo "❌ Failed to fetch release info"; exit 1; } VERSION=$(jq -r '.tag_name' <<< "$JSON") || { echo "❌ Could not extract version"; exit 1; } ASSET_URL=$(jq -r '.assets[]?.browser_download_url' <<< "$JSON" | grep -E "pandoc-.*-${ARCH_TAG}\.tar\.gz$" | head -n1) [[ -z "$ASSET_URL" ]] && { echo "❌ Matching asset not found for architecture ${ARCH_TAG}"; exit 1; } ASSET_FILE=$(basename "$ASSET_URL") echo "📦 Downloading pandoc ${VERSION} (${ASSET_FILE}) …" TMP=$(mktemp -d) curl -#L -o "${TMP}/${ASSET_FILE}" "$ASSET_URL" # 6 Extract tar -C "$TMP" -xzf "${TMP}/${ASSET_FILE}" # 7 Locate binary BIN_PATH=$(find "$TMP" -type f -name pandoc -perm -u+x | head -n1) [[ -z "$BIN_PATH" ]] && { echo "❌ Binary not found"; exit 1; } # 8 Install / Dry-run if $DRY_RUN; then echo "🧪 Dry-run: verified pandoc binary at ${BIN_PATH}" echo "ℹ️ Would install to: ${INSTALL_DIR}/pandoc (sudo install -m755 …)" echo "✅ pandoc $("$BIN_PATH" -v | head -n1) would be installed successfully" else sudo install -m755 "$BIN_PATH" "${INSTALL_DIR}/pandoc" echo "✅ pandoc $(pandoc -v | head -n1) installed in ${INSTALL_DIR}" fi