#!/bin/sh

# See misc/ruler* for more details
#
# Requires tempfile, netpbm suite

if [ -n "$1" ] ; then
  echo ""
  echo "USAGE: dypbmfixmargins  < pbm-in  > pbm-out    or  | pbm2lmpc"
  echo ""
  echo "Adjusts left and right margins to have a neatly centered print with"
  echo "minimal margins."
  echo ""
  echo "(Actually, input is expected to be vertical, i.e. we adjust top and bottom.)"
  echo ""
  exit 1
fi

TMP1="`tempfile`" || exit 1
TMP2="`tempfile`" || exit 1
TMP3="`tempfile`" || exit 1

(
  set -e   # exit this subshell on any error; still clean up tempfiles

  pbmmake -white 1 7 > "$TMP1"
  pnmcrop -white -top -bottom > "$TMP2"   # reads from stdin
  pbmmake -white 1 122 > "$TMP3"

  pnmcat -white -topbottom -jleft "$TMP1" "$TMP2" "$TMP3"   # writes to stdout
)

rm -f "$TMP1" "$TMP2" "$TMP3"
