#/bin/sh

#  pdfpagecounter v0.1
#
#  Usage: pdfpagecounter FILE
#  Copyright 2008 Michele La Monaca

# If present use tag /N
pages=`./grep -a -m1 "\/N \([0-9]\+\)" "$1" | sed -n "s/.*\/N \([0-9]\+\).*/\1/p"`

# Otherwise use the greatest /Count tag
if ! [ "$pages" -gt 0 ] &> /dev/null ; then
        pages=0
        pages=$(
        strings "$1" | ./grep -a "\/Count \([0-9]\+\)" | (
        while read -r line
        do
                num=`echo $line | sed -n "s/.*\/Count \([0-9]\+\).*/\1/p"`
                [ $num -gt $pages ] && pages=$num
        done; echo $pages))
fi
sleep 3
echo $pages

