#!/bin/bash

rm -rf crop
mkdir crop

cat geometry.txt | while read fn g r
do
  fn2=`basename $fn`
  width=${g%x*}
  rest=${g#*x}
  height=${rest%%\+*}
  rest=${rest#*\+}
  x=${rest%\+*}
  y=${rest#*\+}
  iw=`identify -format "%w" $fn`
  ih=`identify -format "%h" $fn`
  l1=portrait
  if [ $width -gt $height ]
  then
    l1=landscape
  fi
  l2=portrait
  if [ $iw -gt $ih ]
  then
    l2=landscape
  fi
  echo $fn2	$g $width $height $x $y $iw $ih	$l1 $l2
  if [ $width -gt $iw ]
  then
    echo bad $fn
  fi
  if [ $height -gt $ih ]
  then
    echo bad $fn
  fi
  convert $fn -crop $g +repage -rotate $r crop/$fn2
done

cd crop
convert * ../Technical_Manual_for_Magnetic_Tape_Adapter_Vol_1_Electronic_Adapter_for_Ampex_TM_7_Tape_Unit_Technical_Manual_Vol_I.pdf

