#!/bin/sh -
# $Id: mgp2pdf,v 1.3 2003/12/29 07:43:50 sra Exp $
#
# Hack to generate PDF files from MagicPoint presentations.
#
# For simple presentations, you don't need this script at all, there's
# a much simpler way that usually generates much smaller PDF files:
#
#    mgp2ps input.mgp | ps2pdf - output.pdf
#
# Unfortunately, due to limitations in the current version of mgp2ps,
# this simple method may not produce satisfactory results.  When the
# simple method fails, you need this script, or something like it.
#
# The basic idea here is to use MagicPoint itself to render each slide
# into a pixmap, then combine the resulting pixmaps into a PDF file.
# The resulting PDF is huge, because it's all raster graphics, but it
# has everything rendered correctly.
#
# This uses the X Virtual Frame Buffer server, the NetPBM toolkit, and
# GhostScript.  It's a kludge.  Feel free to do better, then send me
# what you did so I can use it too.
#
# This program is hereby explictly placed in the public domain as
# Beer-Ware.  If we meet some day and you think this program is worth
# it, you can buy me a beer.  Your mileage may vary.  We decline
# responsibilities, all shapes, all sizes, all colors.  If this
# program breaks, you get to keep both pieces.

# Rendering geometry.  792x612 produces something with the 22:17
# aspect ratio of North American Letter paper in landscape mode.
# 1024x768 or 640x480 produces something with the 4:3 aspect ratio of
# your average monitor (although not my Sony PictureBook!).
#
#xres=792  yres=612
#xres=640  yres=480
#xres=1024 yres=768

: ${xres=1024} ${yres=768}

# Screen depth (changing this may crash MagicPoint, be careful)
: ${depth=16}

# Filenames
input="${1?"usage: $0 input.mgp [output.pdf]"}"
output="${2-"${input%.mgp}.pdf"}"

# Find a free X display number.  You may have to hack this for your
# operating system.  If you know that, say, display 1 is always free
# on your machine, you can  just replace this entire mess with
newdisplay=':0'
#
#: ${newdisplay="$(netstat -f inet -an | sort | awk '
#              BEGIN {FS="[. \t]+"}
#	      $1 !~ /^tcp/ || $8 != "LISTEN" {next}
#	      {this = $5 - 6000}
#	      this == display && this < 100 {display++}
#	      END {print ":" display}')"}

# No user servicable parts past this point.

# Always mount a scratch monkey
tmp="tmp.$$"
trap "rm -rf '$tmp'" 0 1 2 3 13 15
mkdir "$tmp"

# Run MagicPoint under Xvfb to render the presentation to PNG.
# The sleep and xwininfo are voodo (remove them at your peril).
# Xvfb also tends to whine a bit, so gag it unless you're debugging.
#
echo "Rendering MGP => PNG at ${xres}x${yres}..."
Xvfb "$newdisplay" -screen 0 "${xres}x${yres}x${depth}" 2>/dev/null &
xvfb="$!"
sleep 1
DISPLAY="$newdisplay" xwininfo -root >/dev/null 2>&1
DISPLAY="$newdisplay" mgp -d -D "$tmp" -E png "$input"
kill "$xvfb"

# We now have a temporary directory containing PNG renderings of our
# slides, along with a bunch of stuff we don't care about.   Convert
# the PNGs to Postscript, then to PDF.  It'd be nice to include the
# thumbnails, but I don't know any easy way to do that.
#
echo 'Converting PNG => PNM => PS => PDF...'
find "$tmp" -name '*.png' ! -name '*.idx.*' | sort |
while read i; do pngtopnm "$i" | pnmtops -nocenter -noturn -dpi 72 -equalpixels; done |
ps2pdf13 "-g${xres}0x${yres}0" - "$tmp/output.pdf"

# That's it.
mv -f "$tmp/output.pdf" "$output"
echo "Wrote ${output}"

