#! /bin/bash
#
#Author: Fabrice Régnier (email: regnier.fab@free.fr)
#Date: 16/11/2017
#Subject: rechercher un motif a l'interieur des fichiers *.pdf, *.doc, *.odt, ... (grace au script uvgrep.sh et aux boites de dialogue yad)
#pour qui: pour l'ami Yves

#je me base sur :
# Frontend for find(1)
# Author: Victor Ananjevsky <ananasik@gmail.com>, 2015-2017
# et sur :
# uvgrep (UniVersal Grep), version 1.0.1 (2016/07/08)
# (c) Hans-Georg Esser, EasyLinux, hgesser@easylinux.de

# Licensed under the GPL, version 3 (see LICENSE)

default_motif="article"
default_directory="/home/fabricer/perso/ecole/"

export find_cmd='@bash -c "run_find %1 %2 %3"'

export fpipe=$(mktemp -u --tmpdir find.XXXXXXXX)
mkfifo "$fpipe"

trap "rm -f $fpipe" EXIT

fkey=$(($RANDOM * $$))

function run_find
{
	#réinitialise le flux
	echo -e '\f' >> "$fpipe"

	extensions="*.pdf *.odt *.ods *.odp *.docx *.xlsx *.pptx *.doc *.xls *.ppt *.sh *.txt"
	cd $2
 	/usr/bin/uvgrep -in $1 $extensions >> "$fpipe"
#  	if [ -s $fpipe ] 
#  	then
# 		echo "--> fin de la recherche" >> "$fpipe"
#  	else
# 		echo "--> aucun résultat" >> "$fpipe"
#  	fi
}
export -f run_find

function open_file
{
    xdg-open "$1" &> /dev/null
}
export -f open_file

exec 3<> $fpipe

yad --plug="$fkey" --tabnum=1 --form --field="Motif" $default_motif \
    --field="Répertoire:dir" $default_directory \
    --field="gtk-find:fbtn" "$find_cmd" &

yad --plug="$fkey" --tabnum=2 --list --no-markup --dclick-action="bash -c 'open_file %s'" \
    --text "Résultats:" --column="Nom" --search-column=1 --expand-column=1 <&3 &

yad --paned --key="$fkey" --button="gtk-close:1" --width=700 --height=500 \
    --title="Rechercher dans les fichiers pdf/doc/xls/odt/txt/..." --window-icon="find"

exec 3>&-
