#!/bin/bash
# -*- ENCODING: UTF-8 -*-

# fif
#
# Search within files.
#
# Copyright (c) 2013-2026: Alexis Puente Montiel   < pica (a) picalibre.org >
#
# Licensed according to GNU AGPL version 3.0.
#
# It is libre/free software; you can use, redistribute and/or modify it according to the terms of GNU AGPL as published by GNU, version 3.0, 19 November 2007.
#
# It is distributed in the hope that it will be useful, but without any warranty. Read GNU AGPL version 3.0 for additional details.
#
# A copy of GNU AGPL version 3.0 is available at /usr/share/doc/<software-package-name>/agpl-3.0.txt (additionally on Internet as text at https://www.gnu.org/licenses/agpl-3.0.txt and as HTML at https://www.gnu.org/licenses/agpl-3.0-standalone.html ).
#
# Note: Additionally to the official e-mails, picalibre.org is strictly the only official site for this software project, please consider using it to download, report bugs and contribute.
#
# Depends: bash, coreutils, findutils, gawk | mawk | original-awk, grep
# Suggests: djvulibre-bin, ghostscript, pdfgrep | mupdf-tools | poppler-utils, taggrepper, unoconv


### SCRIPT VARIABLES ########################################

CNAME="fif"
VERSION="1.3.1"
TITLE="'fif'"

# Translations
if [ "$LANG" = "" ] ; then export $(cat /etc/default/locale | grep -a 'LANG=') ; fi
TEXTDOMAIN=picainfo
TEXTDOMAINDIR=/usr/share/locale/

# Write errors to log
ERRORLOG="$HOME/.${CNAME}.log"
if [ -e "$ERRORLOG" ] ; then
	mv -f $ERRORLOG ${ERRORLOG}.ant
fi
if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi

for i in /etc/pica-global.dist /etc/pica-global.orig /etc/pica-global /etc/pica-global.local ~/.pica-global ~/pica-global ; do
	if [ -f "$i" ] ; then
	cat "$i"
	source "$i"
	source <(cat $i | sed -e "s/=\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)/=Y/g" -e "s/=\(No\|no\|n\)/=N/g" -e "s/=\"\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)\"/=Y/g" -e "s/=\"\(No\|no\|n\)\"/=N/g")
	fi
done
if [ "$DEBUG" = "Y" ] ; then
	set -xv
	DEBUG="Y"
else
	ERRORLOG="/tmp/.${CNAME}_$(id -nu).log"
	if [ -e "$ERRORLOG" ] ; then mv -f $ERRORLOG ${ERRORLOG}.ant ; fi
	if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi
fi
if [ "$DEBUG" = "" ] ; then DEBUG="N" ; fi
if [ "$DEBUG" != "N" ] ; then
exec > >(tee -a "$ERRORLOG") 2>&1
echo "$0" "$*" >> "$ERRORLOG"
#else
#exec 2>>"$ERRORLOG"
fi

# Description:
BDESCRIP=$"Search within files."
LDESCRIP=$"$TITLE is a tool to search within files."

# Documentation:
docu_info () {
echo "$CNAME ($VERSION) - $BDESCRIP"
echo 
echo $"Usage:" $CNAME [$"OPTIONS"] $"TEXT" [$"PATH"] [$"DEPTH"]
echo 
echo $"Options:"
echo -e "$ODESCRIP"
echo 
echo $"'man $CNAME' for more information."
echo 
}
ODESCRIP=" -x""\t"$"Show help documentation."

while getopts x OPTION ; do
	case $OPTION in
		x )   docu_info ; exit 0 ;;
	esac
done


### SCRIPT ########################################

X="$1"
if [ "$X" = "" ] ; then
	docu_info
	exit 1
fi
FPATH="$2"
if [ "$FPATH" = "" ] ; then
	FPATH="."
fi
DEPTH="$3"
if [ "$DEPTH" != "" ] ; then
	FDEPTH="-maxdepth $DEPTH"
fi
GREPV=grep
GREPOP="-aEi"

find "$FPATH" $FDEPTH -type f | while read i ; do
	FT=$(echo "$i" | awk -F '.' '{print $NF}')
	case $FT in
		pdf|PDF)
			if [ "$(command -v pdfgrep)" != "" ] ; then
				pdfgrep -iHn "$X" "$i"
			elif [ "$(command -v mutool)" != "" ] ; then
				printf "$i"'\t'"$(mutool draw -F txt "$i" 2>/dev/null | $GREPV $GREPOP "$X")"'\n' | $GREPV $GREPOP "$X"
			elif [ "$(command -v pdftotext)" != "" ] ; then
				printf "$i"'\t'"$(pdftotext "$i" - | $GREPV $GREPOP "$X")"'\n' | $GREPV $GREPOP "$X"  # pdftotext -layout
			else
				$GREPV -inHaE "$X" "$i"
			fi
		;;
		ps|PS)
			if [ "$(command -v ps2txt)" != "" ] ; then
				printf "$i"'\t'"$(ps2txt "$i" | $GREPV $GREPOP "$X")"'\n' | $GREPV $GREPOP "$X"
			else
				$GREPV -inHaE "$X" "$i"
			fi
		;;
		djvu|DJVU|djv|DJV)
			if [ "$(command -v djvutxt)" != "" ] ; then
				printf "$i"'\t'"$(djvutxt "$i" | $GREPV $GREPOP "$X")"'\n' | $GREPV $GREPOP "$X"
			else
				$GREPV -inHaE "$X" "$i"
			fi
		;;
		odt|ODT|doc|DOC|docx|DOCX|rtf|RTF|ods|ODS|xls|XLS|xlsx|XLSX|odp|ODP|pps|PPS|ppsx|PPSX|ppt|PPT|pptx|PPTX)
			if [ "$(command -v unoconv)" != "" ] ; then
				printf "$i"'\t'"$(unoconv -f txt --stdout "$i" | $GREPV $GREPOP "$X")"'\n' | $GREPV $GREPOP "$X"
			else
				$GREPV -inHaE "$X" "$i"
			fi
		;;
		mp3|MP3|ogg|OGG|ogv|OGV|fla|FLA|flac|FLAC)
			if [ "$(command -v taggrepper)" != "" ] ; then
				taggrepper --any-tag="$X" "$i"
			else
				$GREPV -inHaE "$X" "$i"
			fi
		;;
		*)
			$GREPV -inHE "$X" "$i"
		;;
	esac
done

exit "$?"
