#!/bin/bash


#PDFDIR=~/vhosts/an/httpdocs/pub/files/pdf/pdf/
PDFDIR=~/vhosts/anpdf/app/pub/files/pdf
OUTDIR=~/vhosts/anpdf/app/pub/files/pages

cd $PDFDIR

echo $1

if [ -n "$1" ]
then
f=$1.pdf;
else
f=*.pdf;
fi

for file in $f; 
    do 
        name="${file%.pdf}"
        PGEDIR="$OUTDIR/$name"

        if [ ! -d "$PGEDIR" ]; then

            mkdir -p $PGEDIR
            echo "add $name ..."; 
            docker run --name pdftk -it --rm \
                --user $(id -u):$(id -g) \
                -v "$PDFDIR:/workdir/pdf" \
                -v "$OUTDIR:/workdir/pages" \
                -w "/workdir" \
                masterforweb/pdftk pdftk "pdf/$file" burst output "pages/${name}/${name}-%1d.pdf"; 
            echo "$name done";    

        fi
      
done