#!/bin/bash

if [ $# -lt 1 ] ; then
    echo "simple cli tool for search PDFs by content"
    echo "this script don't work in path constaints symlinks (locate feature)"
    echo "and need builded slocate database (and installed locate of course)"
    echo
    echo "usage:"
    echo "   pdfsearch expression"
    echo "      it search from current directory recursively"
    exit 1;
fi

exp=$1
path=`pwd`"/*.pdf"
echo "search \""$exp"\" in \""$path"\""
echo

files=0
sum=0
IFS=`echo -en "\n\b"` # change internal bash separator

for pdffile in `locate "$path"` ; do

    count=`pdftotext "$pdffile" - | grep -c "$exp"`
    if [ $count -gt 0 ] ; then
        echo -e $count" \t\""$pdffile"\""

        ## uncoment these lines, if you would see searched line
        #pdftotext "$pdffile" - | grep --color=always "$exp"
        #echo

        files=$(( $files + 1 ))
        sum=$(( $sum + $count ))
    fi
done

echo
echo "summary found: "$sum" in "$files" files"


