#!/bin/bash
#
# PDFgrep
#
# Version: 0.0.1 
#
# Author: D-M-Wilhelm <meisssw01 at gmail.com>
##########################################################
# Copyright (C) 2024 D.M.-Wilhelm <meisssw01 at gmail.com> 
#
##########################################################
#
#define some variables
Encoding=UTF-8
TITLE="PDFgrep"
VERSION="0.0.3"
ICON="/usr/share/icons/hicolor/128x128/apps/pdfsearch.png"
#
# Locale
LCL=$(cat /etc/sysconfig/i18n | grep LC_CTYPE | tail -1 | cut -c 10-14)
if [ $LCL = de_DE ]; then
WND1="<b>PDF Dateien dursuchen</b>.\n<b>
\nWas wird gesucht?</b>"
WND2="<b>Wähle ein Verzeichnis.</b>"
WND3="$TITLE sucht nach "
WND4="Wenn keine Dateien angezeigt werden, wurde keine Übereinstimmung gefunden oder die Liste ist zu lang!"
WND5="Wähle eine Datei, die geöffnet werden soll zur Ansicht."
WND6="Dateien vorhanden"
WND7="Öffne eine angewählte PDF Datei."
WND8="Öffnen"
WND9="Starte eine neue suche."
WND10="Suchen"
WND11="Schließe $TITLE!"
else
WND1="<b>Search PDF files</b>.\n<b>
\What are you looking for?</b>"
WND2="<b>Select a directory.</b>"
WND3="$TITLE searches for "
WND4="If no files are displayed, no match was found or the list is too long!"
WND5="Select a file to open for viewing."
WND6="Files present"
WND7="Open a selected PDF file."
WND8="Open"
WND9="Start a new search."
WND10="Search"
WND11="Exit $TITLE!"
fi
# LOCALE_END
#
usage ()
#
{
FILES=$(yad --entry --title "$TITLE $VERSION" \
			--window-icon="$ICON" --image="$ICON"\
			--width=300 \
			--text "$WND1") 
	if [ "$FILES" = "" ]; then								             # add entry
		exit 0            												 # close app
	else                                                                 # choose where to search
	FIND=$(yad --button="gtk-ok:0" --file-selection \
			--title "$TITLE $VERSION" \
			--window-icon="$ICON" \
			--width=800 --height=600 \
			--text "$WND2")
			if [ "$FIND" = "" ]; then								     # add entry
			exit 0 
		    fi       													 # close app
			
ret=$?
		XDGOPEN=$(notify-send -i info -t 0 "$WND3 $FILES" | pdfgrep -rin "$FILES" "$FIND" - | sort -f | while read programm; do echo "<item>$programm</item>"; done)
		
fi

PDFSEARCH='
<window window_position="1" title="'$TITLE-$VERSION'" icon-name="pdfsearch" resizable="true">
	<vbox>
		<frame> 
			<vbox  tooltip-text="'$WND4'">
				<text use-markup="True">
					<label>"'$WND5'"</label>
				</text>  
			</vbox>
			
			<vbox scrollable="true"> 
				<tree rules_hint="true" exported_column="0"  scrollable="true">
					<width>500</width><height>800</height>
					<variable>SELECT</variable>
					<label>'$WND6'</label>
					'$XDGOPEN' 
				</tree>  
			</vbox> 
			
			<hbox>
				<vbox tooltip-text="'$WND7'">
					<button>
						##<input file icon="gtk-find"></input>
						<label>'$WND8'</label>
						<action>xdg-open "`echo "$SELECT" | cut -d ":" -f1`" &</action>
					</button>
				</vbox>
           
				<vbox tooltip-text="'$WND9'"> 
					<button>
						##<input file icon="gtk-find"></input>
						<label>'$WND10'</label>
						<action>exec pdfsearch &</action>
						<action type="exit">PDFSEARCH</action>
					</button>
				</vbox> 

				<vbox tooltip-text="'$WND11'">
					<button cancel></button>
				</vbox> 
			</hbox>
		</frame>
	</vbox>
</window>
'

export GUI="`echo "$PDFSEARCH" | sed -e 's/##.*//'`" 
gtkdialog -p GUI
exit 1 
}

if [ $# -eq 0 ];
then
usage

fi
#
FIND="$1"
FILES=$(yad --entry --title "$TITLE $VERSION" \
			--window-icon="$ICON" --image="$ICON"\
			--width=300 \
			--text "$WND1")
	if [ "$FILES" = "" ]; then								             # add entry
		exit 0            												 # close app
	else                                                                
		XDGOPEN=$(notify-send -i info -t 0 "$WND3 $FILES" | pdfgrep -rin "$FILES" "$FIND" - | sort -f | while read programm; do echo "<item>$programm</item>"; done)
		
fi

PDFSEARCH='
<window window_position="1" title="'$TITLE-$VERSION'" icon-name="pdfsearch" resizable="true">
	<vbox>
		<frame> 
			<vbox  tooltip-text="'$WND4'">
				<text use-markup="True">
					<label>"'$WND5'"</label>
				</text>  
			</vbox>
			
			<vbox  scrollable="true"> 
				<tree rules_hint="true" exported_column="0"  scrollable="true">
					<width>500</width><height>800</height>
					<variable>SELECT</variable>
					<label>'$WND6'</label>
					'$XDGOPEN' 
				</tree>  
			</vbox> 
			
			<hbox>
				<vbox tooltip-text="'$WND7'">
					<button>
						##<input file icon="gtk-find"></input>
						<label>'$WND8'</label>
						<action>xdg-open "`echo "$SELECT" | cut -d ":" -f1`" &</action>
					</button>
				</vbox>
           
				<vbox tooltip-text="'$WND9'"> 
					<button>
						##<input file icon="gtk-find"></input>
						<label>'$WND10'</label>
						<action>exec pdfsearch &</action>
						<action type="exit">PDFSEARCH</action>
					</button>
				</vbox> 

				<vbox tooltip-text="'$WND11'">
					<button cancel></button>
				</vbox> 
			</hbox>
		</frame>
	</vbox>
</window>
'

export GUI="`echo "$PDFSEARCH" | sed -e 's/##.*//'`" 
gtkdialog -p GUI

