#/bin/bash
# script for rotating all even pages of a document

# enough arguments?
if [ $# -ne 2 ]; then
         echo 1>&2 Usage: pdf-rotate-even input.pdf output.pdf
         exit 127
    fi

# generate a temporary directory
TEMPDIR=/tmp/pdf`date +%N`
mkdir $TEMPDIR

# rotate all even pages, keep the odd ones
pdftk $1 cat 1-endodd output $TEMPDIR/odd.pdf
pdftk $1 cat 1-endevenS output $TEMPDIR/even.pdf

# split the files...
pdftk $TEMPDIR/odd.pdf burst output $TEMPDIR/pg%04d_A.pdf
pdftk $TEMPDIR/even.pdf burst output $TEMPDIR/pg%04d_B.pdf

# ... and recombine them, "_A" and "_B" suffixes leading to the correct order
pdftk $TEMPDIR/pg*.pdf cat output $2

# cleanup
rm -r $TEMPDIR
