#!/usr/bin/env sh # One-time installer: builds the binary and ensures it's on your PATH via ~/.local/bin. set -euo pipefail ROOT="$(cd -- "$(dirname "$0")/.." && pwd)" BIN_DIR="$ROOT/bin" BIN="$BIN_DIR/img2pdf" GOFLAGS_DEFAULT="-tags=wayland" CGO_CFLAGS_DEFAULT="-D_GNU_SOURCE" # Simple spinner for long-running steps (cycles 10 dots inline). run_with_spinner() { label="$1"; shift printf "%s" "$label" ( count=0 while :; do printf "." count=$(( (count + 1) % 10 )) if [ "$count" -eq 0 ]; then printf "\r%s" "$label" fi sleep 0.35 done ) & spid=$! "$@"; status=$? kill "$spid" 2>/dev/null printf "\n" return $status } export GOCACHE="${GOCACHE:-$ROOT/.cache/go-build}" export GOMODCACHE="${GOMODCACHE:-$ROOT/.cache/go-mod}" export GOFLAGS="${GOFLAGS:-$GOFLAGS_DEFAULT}" export CGO_CFLAGS="${CGO_CFLAGS:-$CGO_CFLAGS_DEFAULT}" export CGO_CXXFLAGS="${CGO_CXXFLAGS:-$CGO_CFLAGS}" mkdir -p "$GOCACHE" "$GOMODCACHE" "$BIN_DIR" patch_glfw_wayland() { # Drop the redundant _GNU_SOURCE define in GLFW's Wayland file to avoid macro redefinition warnings. local wl_file="$GOMODCACHE/github.com/go-gl/glfw/v3.3/glfw@v0.0.0-20221017161538-93cebf72946b/glfw/src/wl_window.c" if [ -f "$wl_file" ] && grep -q '^#define _GNU_SOURCE$' "$wl_file"; then chmod u+w "$wl_file" tmp="$(mktemp)" sed '/^#define _GNU_SOURCE$/d' "$wl_file" >"$tmp" && mv "$tmp" "$wl_file" fi } # Ensure dependencies are present (generates go.sum). If this fails, likely due to blocked network. ensure_deps() { if [ ! -f "$ROOT/go.sum" ]; then run_with_spinner "Resolving dependencies" env GOCACHE="$GOCACHE" GOMODCACHE="$GOMODCACHE" go mod tidy || { echo "Failed to resolve dependencies (network or permissions issue). Please run:" echo " GOMODCACHE=\"$GOMODCACHE\" GOCACHE=\"$GOCACHE\" go mod tidy" exit 1 } fi # Prefetch modules; if it fails (offline), warn but continue to build from cache. if ! run_with_spinner "Downloading modules" env GOCACHE="$GOCACHE" GOMODCACHE="$GOMODCACHE" go mod download 2>/dev/null; then echo "Warning: could not download modules (maybe offline). Continuing with existing cache." fi } cd "$ROOT" ensure_deps patch_glfw_wayland if ! run_with_spinner "Building img2pdf" env GOCACHE="$GOCACHE" GOMODCACHE="$GOMODCACHE" GOFLAGS="$GOFLAGS" go build -o "$BIN" ./cmd/img2pdf; then echo "Build failed. See errors above." exit 1 fi echo "Built: $BIN" echo "Use: ./img2pdf (from repo root)"