#!/bin/bash
#
# MyPDFgrep
#
#
# Author: D-M-Wilhelm <meisssw01 at gmail.com>
##########################################################
# Copyright (C) 2024-2025
# D.M.-Wilhelm <meisssw01 at gmail.com> 
#
##########################################################
#
#define some variables
Encoding=UTF-8
TITLE="MyPDFsearch"
VERSION="0.1.2"
ICON="/usr/share/icons/hicolor/128x128/apps/mypdfsearch.png"
#
# Create cache path 
if ! test -d $HOME/.cache/mypdfsearch  ; then
mkdir -p $HOME/.cache/mypdfsearch 
fi 
CCH="$HOME/.cache/mypdfsearch"
#
# 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
Bei keine Eingabe werden die letzten Suchergebnisse angezeigt.
\n<b>Was 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="Ergebnisse für"
WND7="Öffne eine angewählte PDF Datei."
WND8="Zeigen"
WND9="Starte eine neue Suche."
WND10="Suchen"
WND11="Schließen"
WND12="Speichert aktuelle Suche."
WND13="Speichern"
WND14="Öffne eine gespeicherte Suche."
WND15="Öffnen"
WND16="Pfad"
WND17="Vorhanden, Überschreiben?"
else
WND1="<b>Search PDF files</b>.
If you do not enter anything, the last search results will be displayed.
\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="Results for"
WND7="Open a selected PDF file."
WND8="Show"
WND9="Start a new search."
WND10="Search"
WND11="Exit"
WND12="Save current search."
WND13="Save"
WND14="Opoen a saved search."
WND15="Open"
WND16="Path"
WND17="Existing, overwrite?"
fi
# LOCALE_END
#
usage ()
#
{
FILES=$(yad --entry --title "$TITLE $VERSION" \
			--window-icon="$ICON" --image="$ICON"\
			--width=350 \
			--button="gtk-ok:0" \
			--text "$WND1") 
	if [ "$FILES" = "" ]; then								             # add entry
		#exit 0          
		case $1 in														 # no entry to open pdfsearch without search function
		-d | --dump) echo "$MYPDFSEARCH" ;;
		esac 												 
	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
			
		XDGOPEN=$(notify-send -i mypdfsearch -t 0 "$WND3 $FILES" | pdfgrep -rinpm1 "$FILES" "$FIND" > $CCH/default.txt)
		
fi

MAIN_CHOOSER='
<window title="'$TITLE-$VERSION'" icon-name="mypdfsearch" resizable="true">
	<vbox border-width="10">
		<chooser>
			<variable>CHOOSER</variable>
			<default>'$CCH'</default>
			<width>600</width>
			<height>400</height>
		</chooser>
		<hbox>
			<button>
				<variable>MAINCHOOSER</variable>
				<label>'$WND8'</label>
				<action>cat $CHOOSER > '$CCH'/default.txt</action>
				<action type="refresh">SELECT</action>
				<action  type="closewindow">MAINCHOOSER</action>
			</button>	
			<button>
				<label>'$WND11'</label>
				<action  type="closewindow">MAINCHOOSER</action>
			</button>
		</hbox>
	</vbox>
</window>
'
export MAIN_CHOOSER 

MYPDFSEARCH='
<window window_position="1" title="'$TITLE-$VERSION'" icon-name="mypdfsearch" 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" selection-mode="3">
					<variable>SELECT</variable>
					<label>" '$WND6' '$FILES'"</label>
					<input file>'$CCH'/default.txt</input> 
				</tree>  
			</vbox> 
			
			<hbox>
				<hbox tooltip-text="'$WND14'">
					<button>
						<action type="launch">MAIN_CHOOSER</action>
						<label>'$WND15'</label>
					</button>	
				</hbox>
					
				<vbox tooltip-text="'$WND12'">
					<button>
						<label>'$WND13'</label>
						<action>if [ -f '$CCH'/'$FILES'.txt ]; then yad --title='$TITLE' --window-icon='$ICON' --text="'$WND17'" ; if [ $? = 0 ]; then cp '$CCH'/default.txt '$CCH'/'$FILES'.txt ; fi else  cp '$CCH'/default.txt '$CCH'/'$FILES'.txt ; fi</action>
					</button>
				</vbox>
					
				<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>
						<label>'$WND10'</label>
						<action>exec mypdfsearch &</action>
						<action type="exit">PDFSEARCH</action>
					</button>
				</vbox> 

				<vbox tooltip-text="'$WND11'">
					<button use-stock="true" label="gtk-quit"></button>
				</vbox> 
			</hbox>
		</frame>
	</vbox>
</window>
'

export GUI="`echo "$MYPDFSEARCH" | sed -e 's/##.*//'`" 
gtkdialog -p GUI > /dev/null 2>&1
exit 1 
}

if [ $# -eq 0 ];
then
usage

fi
#
FIND="$1"
FILES=$(yad --entry --title "$TITLE $VERSION" \
			--window-icon="$ICON" --image="$ICON"\
			--width=350 \
			--button="gtk-ok:0" \
			--text "$WND1") 
	if [ "$FILES" = "" ]; then								             # add entry
		#exit 0          
		case $1 in														 # no entry to open pdfsearch without search function
		-d | --dump) echo "$MYPDFSEARCH" ;;
		esac            												 # close app
	else                                                                
		XDGOPEN=$(notify-send -i mypdfsearch -t 0 "$WND3 $FILES" | pdfgrep -rinpm1 "$FILES" "$FIND" > $CCH/default.txt)
		
fi
MAIN_CHOOSER='
<window title="'$TITLE-$VERSION'" icon-name="mypdfsearch" resizable="true">
	<vbox border-width="10">
		<chooser>
			<variable>CHOOSER</variable>
			<default>'$CCH'</default>
			<width>600</width>
			<height>400</height>
		</chooser>
		<hbox>
			<button>
				<variable>MAINCHOOSER</variable>
				<label>'$WND8'</label>
				<action>cat $CHOOSER > '$CCH'/default.txt</action>
				<action type="refresh">SELECT</action>
				<action  type="closewindow">MAINCHOOSER</action>
			</button>	
			<button>
				<label>'$WND11'</label>
				<action  type="closewindow">MAINCHOOSER</action>
			</button>
		</hbox>
	</vbox>
</window>
'
export MAIN_CHOOSER  

MYPDFSEARCH='
<window window_position="1" title="'$TITLE-$VERSION'" icon-name="mypdfsearch" 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" selection-mode="3">
					<variable>SELECT</variable>
					<label>" '$WND6' '$FILES'"</label>
					<input file>'$CCH'/default.txt</input> 
				</tree>  
			</vbox> 
			
			<hbox>
				<hbox tooltip-text="'$WND14'">
					<button>
						<action type="launch">MAIN_CHOOSER</action>
						<label>'$WND15'</label>
					</button>	
				</hbox>
					
				<vbox tooltip-text="'$WND12'">
					<button>
						<label>'$WND13'</label>
						<action>if [ -f '$CCH'/'$FILES'.txt ]; then yad --title='$TITLE' --window-icon='$ICON' --text="'$WND17'" ; if [ $? = 0 ]; then cp '$CCH'/default.txt '$CCH'/'$FILES'.txt ; fi else  cp '$CCH'/default.txt '$CCH'/'$FILES'.txt ; fi</action>
					</button>
				</vbox>
				
				<vbox tooltip-text="'$WND7'">
					<button>
						<label>'$WND8'</label>
						<action>xdg-open "`echo "$SELECT" | cut -d ":" -f1`" &</action>
					</button>
				</vbox>
           
				<vbox tooltip-text="'$WND9'"> 
					<button>
						<label>'$WND10'</label>
						<action>exec mypdfsearch &</action>
						<action type="exit">PDFSEARCH</action>
					</button>
				</vbox> 

				<vbox tooltip-text="'$WND11'">
					<button use-stock="true" label="gtk-quit"></button>
				</vbox> 
			</hbox>
		</frame>
	</vbox>
</window>
'

export GUI="`echo "$MYPDFSEARCH" | sed -e 's/##.*//'`" 
gtkdialog -p GUI  > /dev/null 2>&1

