#!/usr/bin/sh
file=original54.pdf;
newfile=$(basename $file .pdf)-2.pdf;
tmpfile=$(mktemp --suffix=.pdf);
blankfile=$(mktemp --suffix=.pdf);
cp -f $file $newfile;
numberofpages=`pdftk $file dump_data | grep "NumberOfPages" | sed 's:.*\([0-9][0-9*]\).*:\1:'`;
echo "" | ps2pdf -sPAPERSIZE=a4 - $blankfile;
while (( $numberofpages % 4 != 0 ));
do 
  ((numberofpages++));
  pdftk A=$newfile B=$blankfile cat A B output $tmpfile;
  cp -f $tmpfile $newfile;
done;
neworder=`
for ((  a=1, b=3, c=4, d=2 ;
        a <=numberofpages ;
        ((a+=4)), ((b+=4)), ((c+=4)), ((d+=4))
    ));
do
echo -n "$a $b $c $d ";
done`;
pdftk $tmpfile cat $neworder output $newfile;
