#!/bin/bash 
# like pnmpad, but understands a size to pad to. Only works on a pipe.
# Hacked up by Nelson Minar <nelson@monkey.org> 2001-11-21
T=`tempfile -s .pnmpadtosize.$$`

newWidth=$1
newHeight=$2

cat > $T

fileinfo=`pnmfile $T`
width=`echo $fileinfo | awk '{ print $4 }'`
height=`echo $fileinfo | awk '{ print $6 }'`

if [ $newWidth -lt $width -o $newHeight -lt $height ]; then
   echo 'New dimensions must be larger than old' 1>&2
   rm $T
   exit 1;
fi

widthPad=$[newWidth-width]
left=$[widthPad/2]
right=$[widthPad-left]
heightPad=$[newHeight-height]
top=$[heightPad/2]
bottom=$[heightPad-top]

cmd="pnmpad -white -l$left -r$right -t$top -b$bottom"
echo $cmd 1>&2
cat $T | $cmd
rm $T
exit 0
