#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$ROOT_DIR" PKG_NAME="pubscript" PKG_VERSION="0.1.0" ARCH="amd64" DEB_NAME="${PKG_NAME}_${PKG_VERSION}_${ARCH}.deb" STAGE_ROOT="/tmp/opencode/pubscript-deb-stage" STAGE_DIR="${STAGE_ROOT}/${PKG_NAME}_${PKG_VERSION}_${ARCH}" echo "==> [1/6] Building production web bundle (apps/web/dist)..." pnpm --filter @pubscript/web run build echo "==> [2/6] Preparing Debian package staging tree at ${STAGE_DIR}..." rm -rf "$STAGE_DIR" mkdir -p \ "$STAGE_DIR/DEBIAN" \ "$STAGE_DIR/usr/bin" \ "$STAGE_DIR/usr/share/applications" \ "$STAGE_DIR/usr/share/icons/hicolor/scalable/apps" \ "$STAGE_DIR/usr/share/pixmaps" \ "$STAGE_DIR/opt/pubscript/apps/web" \ "$STAGE_DIR/opt/pubscript/server/dist" echo "==> [3/6] Deploying production server dependencies and bundling server entry..." pnpm --filter @pubscript/server --prod deploy --legacy "$STAGE_DIR/opt/pubscript/server-runtime" cp -a "$STAGE_DIR/opt/pubscript/server-runtime/node_modules" "$STAGE_DIR/opt/pubscript/node_modules" rm -rf "$STAGE_DIR/opt/pubscript/server-runtime" cat > "$STAGE_DIR/opt/pubscript/package.json" <<'EOF' { "name": "pubscript-desktop-runtime", "version": "0.1.0", "private": true, "type": "module" } EOF # Bundle server TypeScript + @pubscript/core into ESM while keeping native/CJS deps external ./apps/web/node_modules/.bin/esbuild server/src/index.ts \ --bundle \ --platform=node \ --target=node22 \ --format=esm \ --outfile="$STAGE_DIR/opt/pubscript/server/dist/server.mjs" \ --external:better-sqlite3 \ --external:argon2 \ --external:fastify \ --external:@fastify/cors \ --external:@fastify/cookie \ --external:@fastify/static \ --external:@fastify/websocket \ --external:ws \ --external:ulid \ --external:zod cp -a apps/web/dist "$STAGE_DIR/opt/pubscript/apps/web/dist" echo "==> [4/6] Compiling native GTK3 + WebKit2GTK desktop binary (/usr/bin/pubscript)..." gcc $(pkg-config --cflags gtk+-3.0) \ -O2 -Wall -Wextra \ desktop/src/pubscript-desktop.c \ $(pkg-config --libs gtk+-3.0) -ldl \ -o "$STAGE_DIR/usr/bin/pubscript" chmod 0755 "$STAGE_DIR/usr/bin/pubscript" echo "==> [5/6] Installing icons, .desktop launcher, and DEBIAN metadata..." cp apps/web/public/logo.svg "$STAGE_DIR/usr/share/icons/hicolor/scalable/apps/pubscript.svg" cp apps/web/public/logo.svg "$STAGE_DIR/usr/share/pixmaps/pubscript.svg" cat > "$STAGE_DIR/usr/share/applications/pubscript.desktop" <<'EOF' [Desktop Entry] Version=1.0 Type=Application Name=PubScript GenericName=Novel & Manuscript Writing Studio Comment=Local-first writing studio for novelists with AI co-writing, plot boards, and manuscript export Exec=/usr/bin/pubscript %U Icon=pubscript Terminal=false Categories=Office;WordProcessor;TextEditor;Publishing; Keywords=writing;novel;manuscript;markdown;epub;author;editor; StartupNotify=true StartupWMClass=pubscript EOF INSTALLED_KB="$(du -sk "$STAGE_DIR/usr" "$STAGE_DIR/opt" | awk '{s+=$1} END {print s}')" cat > "$STAGE_DIR/DEBIAN/control" <= 2.34), libgtk-3-0 (>= 3.24), libwebkit2gtk-4.1-0 | libwebkit2gtk-4.0-37, nodejs (>= 18) Maintainer: PubScript Studio Homepage: https://pubscript.local Description: PubScript — Local-First Novel & Manuscript Writing Studio A multi-surface writing studio for novelists featuring an OnlyOffice/Word-grade manuscript editor, granular text-anchored AI co-writing suggestions, autosave revision history, character & world-building sheets, plot beat templates, interactive whiteboard, and deterministic HTML/EPUB/ODT/DOCX/PDF export. EOF cat > "$STAGE_DIR/DEBIAN/postinst" <<'EOF' #!/bin/sh set -e if command -v update-desktop-database >/dev/null 2>&1; then update-desktop-database -q /usr/share/applications || true fi if command -v gtk-update-icon-cache >/dev/null 2>&1; then gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor || true fi exit 0 EOF chmod 0755 "$STAGE_DIR/DEBIAN/postinst" echo "==> Verifying compiled desktop binary & embedded server via --headless-check..." PUBSCRIPT_APP_ROOT="$STAGE_DIR/opt/pubscript" \ PUBSCRIPT_DESKTOP_PORT=18099 \ "$STAGE_DIR/usr/bin/pubscript" --headless-check echo "==> [6/6] Building .deb archive with dpkg-deb..." mkdir -p "$ROOT_DIR/dist" dpkg-deb --root-owner-group --build "$STAGE_DIR" "$ROOT_DIR/dist/${DEB_NAME}" cp -f "$ROOT_DIR/dist/${DEB_NAME}" "$ROOT_DIR/${DEB_NAME}" echo "==> Built Debian package: $ROOT_DIR/dist/${DEB_NAME} (and $ROOT_DIR/${DEB_NAME})" dpkg-deb --info "$ROOT_DIR/dist/${DEB_NAME}"