#!/bin/bash 

SCRIPT=$(readlink -f "$0")
SCRIPT_PATH=$(dirname "$SCRIPT")
. $SCRIPT_PATH/ROOTS.sh


firstPage=$1
lastPage=$2
direction=$3
source=$4
workingDirectory=$5

if ls ${workingDirectory}separated-not-rotated*.pdf >/dev/null 2>&1
then
	rm ${workingDirectory}separated-not-rotated*.pdf
fi

if ls ${workingDirectory}separated-page*.pdf >/dev/null 2>&1
then
	rm ${workingDirectory}separated-page*.pdf
fi

pdfseparate -f $firstPage -l $lastPage $source ${workingDirectory}separated%d-not-rotated.pdf 

currentPage=$firstPage

while [ $currentPage -le $lastPage ]
do
  command2="$COMMAND_PREFIX pdftk ${workingDirectory}separated${currentPage}-not-rotated.pdf cat 1${direction} output ${workingDirectory}separated-page${currentPage}.pdf"
  $command2
  currentPage=`expr $currentPage + 1`
done

exit ${?}

