#!/bin/bash

PID=$$
if [ "$2" = "-external_call" ]; then #most likely used the -e switch
	export PROGPATH=/usr/local/pfilesearch
	export WORKDIR=$HOME/.pfilesearch/tmp
fi

export TEXTDOMAIN=pfilesearch # for gettext

case "$1" in
-search)
	echo false > $WORKDIR/pfilesearch-finish #used by daemons (ie Pfind) to see when to update their gui.
	[ "$SEARCHPATHS" = "/  ($(gettext 'All files'))" ] && SEARCHPATHS="/"
	if [ "$SEARCHPATHS" = "/" ]; then TMP="$(gettext 'All files')"; else TMP="$SEARCHPATHS"; fi
	#splash
	if [ "$BBBACKEND" != "true" ]; then
		echo -e "$(gettext 'Searching')  <b><span size='"'large'"' color='"'darkred'"'>$SEARCHSTRING</span></b>  $(gettext 'in') $TMP\n\n\n$(gettext 'Note! The first search will be much slower than the next')" > $WORKDIR/pfilesearch-splashtext
		echo 0 > $WORKDIR/pfilesearch-splash #reset progress bar
		$PROGPATH/box_splash &
	fi
	#---
	echo -n > $WORKDIR/pfilesearch-ext
	echo -n > $HOME/.pfilesearch/hits
	#set days fo atime / ctime / mtime
	YEAR=`echo "$DATE_FROM" | cut -d "/" -f 3`
	if [ $YEAR -lt 1970 ]; then ERROR="$(gettext 'Date can not be earlier than 1/1 1970.')"; . $PROGPATH/func -error_dialog; fi
	TODAY=`date +%s`; TODAY=`expr $TODAY / 60 / 60 / 24`
	TMP=`date -d $DATE_FROM +%s`
	TMP2=`expr $TMP / 60 / 60 / 24`
	DAYS_FROM1=`expr $TODAY - $TMP2`
	TMP=`date -d $DATE_TO +%s`
	TMP1=`expr $TMP / 60 / 60 / 24`
	TMP2=`expr $TODAY - $TMP1`
	DAYS_TO1=`expr $TMP2 - 1`
	echo $DATE_TO1
	TIME=""
	[ "$CHKMTIME" = "true" ] && TIME=" -mtime -$DAYS_FROM1 -mtime +$DAYS_TO1"
	[ "$CHKATIME" = "true" ] && TIME=" -atime -$DAYS_FROM1 -atime +$DAYS_TO1"
	[ "$CHKCTIME" = "true" ] && TIME=" -ctime -$DAYS_FROM1 -ctime +$DAYS_TO1"
	#set permission mode
	if [ "$CHK_UX" = "true" ]; then UX=1; else UX=0; fi 
	if [ "$CHK_UW" = "true" ]; then UW=2; else UW=0; fi 
	if [ "$CHK_UR" = "true" ]; then UR=4; else UR=0; fi 
	if [ "$CHK_GX" = "true" ]; then GX=1; else GX=0; fi 
	if [ "$CHK_GW" = "true" ]; then GW=2; else GW=0; fi 
	if [ "$CHK_GR" = "true" ]; then GR=4; else GR=0; fi 
	if [ "$CHK_OX" = "true" ]; then OX=1; else OX=0; fi 
	if [ "$CHK_OW" = "true" ]; then OW=2; else OW=0; fi 
	if [ "$CHK_OR" = "true" ]; then OR=4; else OR=0; fi 
	OWN=`expr $UX + $UW + $UR`
	GRP=`expr $GX + $GW + $GR`
	OTH=`expr $OX + $OW + $OR`
	if [ $OWN != 0 ] || [ $GRP != 0 ] || [ $OTH != 0 ]; then PERM=" -perm $OWN$GRP$OTH"; fi
	#prepare variables to search
	[ "$CHKDOC" = "true" ] && echo -n "$EXTDOC" >> $WORKDIR/pfilesearch-ext
	[ "$CHKPIC" = "true" ] && echo -n "$EXTPIC" >> $WORKDIR/pfilesearch-ext
	[ "$CHKMUSIC" = "true" ] && echo -n "$EXTMUSIC" >> $WORKDIR/pfilesearch-ext
	[ "$CHKVIDEO" = "true" ] && echo -n "$EXTVIDEO" >> $WORKDIR/pfilesearch-ext
#	[ "$CHKMAIL" = "true" ] && echo -n "$EXTMAIL" >> $WORKDIR/pfilesearch-ext
	[ "$CHKZIP" = "true" ] && echo -n "$EXTZIP" >> $WORKDIR/pfilesearch-ext
	[ "$CHKCODE" = "true" ] && echo -n "$EXTCODE" >> $WORKDIR/pfilesearch-ext
	EXT=`cat $WORKDIR/pfilesearch-ext | sed -e 's/ ./\\\|\\\./g' `
	if [ "$CHKEXACT" = "false" ]; then SEARCHSTRING2="*$SEARCHSTRING*"; else SEARCHSTRING2="$SEARCHSTRING"; fi
	if [ "$CHKCASE" = "true" ]; then NAME="-name"; GREPCASE=""; else NAME="-iname"; GREPCASE="-i"; fi #case (in)sensitive
	if [ "$CHKFOLLOW_SYM" = "true" ]; then FOLLOW_SYM="-L"; else FOLLOW_SYM="-P"; fi #follow symlink?
	[ "$EXCLUDE" ] && EXCLUDE1="-path \"$EXCLUDE\" -prune -o "
	FIND3X="false"
	[ "$CHKFILE" = "true" ] && FIND3X="true" #only if type is specified it will run find 3 times
	[ "$CHKDIR" = "true" ] && FIND3X="true"
	[ "$CHKSYMLN" = "true" ] && FIND3X="true"
	if [ "$CHKREC" = "true" ]; then DEPTH=""; else DEPTH="-maxdepth 1"; fi
	if [ "$CHKXDEV" = "true" ]; then XDEV="-xdev"; else XDEV=""; fi
	[ "$P_USER" != "" ] && USER2=" -user $P_USER"
	[ "$SIZE_GT" != "" ] && SIZE_GT2=" -size +$SIZE_GT"
	[ "$SIZE_LT" != "" ] && SIZE_LT2=" -size -$SIZE_LT"
	FINDOPTIONS="$DEPTH$PERM$XDEV$TIME$USER2$SIZE_GT2$SIZE_LT2" #optional parameters to find
	#Start loop with (all) defined searchpaths
	echo "$SEARCHPATHS" | sed -e "s/\[OR\]/\n/g" > $WORKDIR/pfilesearch-SEARCHPATHS
	while read SEARCHPATH; do
		case $SEARCHPATH in './') SEARCHPATH="$CURDIR"; esac
		#let's ensure that searchpath end with a /. Without Puppy troubles with searching in /mnt/home (symlink to base filesytem)
		SEARCHPATH="`echo "${SEARCHPATH}/" | sed 's%//%/%'`"
		#---
		echo -n "find $FOLLOW_SYM \"$SEARCHPATH\" $EXCLUDE1 $FINDOPTIONS $NAME \"$SEARCHSTRING2\" " > $WORKDIR/pfilesearch-execfind #default find
		echo -n "grep $GREPCASE -l '$SEARCHSTRING' "'"$TMP2"'" " > $WORKDIR/pfilesearch-execgrep #build default grep
		#====== Building find-command ======
		#[OR]
		TMP=`echo "$SEARCHSTRING2" | grep -o "\[OR\]"` #Check if [OR] is used
		if [ "$TMP" ]; then
			TMP=`echo "$SEARCHSTRING2" | sed -e "s/\ \[/\[/g" | sed -e "s/\]\ /\]/g" | sed -e "s/\[OR\]/\|/g"` #cut -d syntax
			if [ $CHKEXACT = "false" ]; then TMP=`echo "$TMP" | sed -e 's/|/*|*/g'`; fi
			STR1=`echo "$TMP" | cut -d "|" -f 1`
			FIND1="$FINDOPTIONS $NAME \"$STR1\""
			GREP1=`echo "$STR1"`
			STR2=`echo "$TMP" | cut -d "|" -f 2`
			if [ "$STR2" ]; then
				FIND2="-o $FINDOPTIONS $NAME \"$STR2\""
				GREP2=`echo '|'"$STR2"`
			fi
			STR3=`echo "$TMP" | cut -d "|" -f 3`
			if [ "$STR3" ]; then 
				FIND3="-o $FINDOPTIONS $NAME \"$STR3\""
				GREP3=`echo '|'"$STR3"`
			fi
			STR4=`echo "$TMP" | cut -d "|" -f 4`
			if [ "$STR4" ]; then
				FIND4="-o $FINDOPTIONS $NAME \"$STR4\""
				GREP4=`echo '|'"$STR4"`
			fi
			STR5=`echo "$TMP" | cut -d "|" -f 5`
			if [ "$STR5" ]; then
				FIND5="-o $FINDOPTIONS $NAME \"$STR5\""
				GREP5=`echo '|'"$STR5"`
			fi
			echo -n "find $FOLLOW_SYM \"$SEARCHPATH\" $EXCLUDE1 $FIND1 $FIND2 $FIND3 $FIND4 $FIND5 " > $WORKDIR/pfilesearch-execfind
			echo -n "grep $GREPCASE -l -E \"$GREP1$GREP2$GREP3$GREP4$GREP5\" "'"$TMP2"'" " > $WORKDIR/pfilesearch-execgrep
		fi
		#[AND]
		if [ "`echo "$SEARCHSTRING2" | grep -o "\[AND\]"`" ]; then #Check if [AND] is used
			TMP=`echo "$SEARCHSTRING2" | sed -e "s/\ \[/\[/g" | sed -e "s/\]\ /\]/g" | sed -e "s/\[AND\]/*\|*/g"` #cut -d syntax
			STR1=`echo "$TMP" | cut -d "|" -f 1`
			FIND1="$FINDOPTIONS $NAME \"$STR1\""
			STR2=`echo "$TMP" | cut -d "|" -f 2`
			[ "$STR2" ] && FIND2="-a $FINDOPTIONS $NAME \"$STR2\""
			STR3=`echo "$TMP" | cut -d "|" -f 3`
			[ "$STR3" ] && FIND3="-a $FINDOPTIONS $NAME \"$STR3\""
			STR4=`echo "$TMP" | cut -d "|" -f 4`
			[ "$STR4" ] && FIND4="-a $FINDOPTIONS $NAME \"$STR4\""
			STR5=`echo "$TMP" | cut -d "|" -f 5`
			[ "$STR5" ] && FIND5="-a $FINDOPTIONS $NAME \"$STR5\""
			echo -n "find $FOLLOW_SYM \"$SEARCHPATH\" $EXCLUDE1 $FIND1 $FIND2 $FIND3 $FIND4 $FIND5 " > $WORKDIR/pfilesearch-execfind
		fi
		#====== FIND ======
		if [ "$CHKFILENAME" = "true" ] || [ "$CHKCONTENT" != "true" ]; then
			if [ $FIND3X = "true" ]; then
				if [ $CHKFILE = "true" ]; then
					sed -i -e "s/-o /-o -type f/g" $WORKDIR/pfilesearch-execfind #pushing -type f to command
					sed -i -e "s/-a /-a -type f/g" $WORKDIR/pfilesearch-execfind
					echo -n "-type f " >> $WORKDIR/pfilesearch-execfind
				fi
				if [ $CHKDIR = "true" ]; then
					sed -i -e "s/-o /-o -type d/g" $WORKDIR/pfilesearch-execfind #pushing -type d to command
					sed -i -e "s/-a /-a -type d/g" $WORKDIR/pfilesearch-execfind
					echo -n "-type d " >> $WORKDIR/pfilesearch-execfind
				fi
				if [ $CHKSYMLN = "true" ];	then
					sed -i -e "s/-o /-o -type l/g" $WORKDIR/pfilesearch-execfind #pushing -type l to command
					sed -i -e "s/-a /-a -type l/g" $WORKDIR/pfilesearch-execfind
					echo -n "-type l " >> $WORKDIR/pfilesearch-execfind
				fi
			fi
			chmod 722 $WORKDIR/pfilesearch-execfind	
			$WORKDIR/pfilesearch-execfind >> $HOME/.pfilesearch/hits #Run find command
		fi
		#====== GREP =======
		if [ "$CHKCONTENT" = "true" ]; then
			chmod 722 $WORKDIR/pfilesearch-execgrep
			echo "find $FOLLOW_SYM \"$SEARCHPATH\" $FINDOPTIONS -iname \"*\" -type f" > $WORKDIR/pfilesearch-execfind #build command
			chmod 722 $WORKDIR/pfilesearch-execfind
			$WORKDIR/pfilesearch-execfind > $WORKDIR/pfilesearch-allfiles
			if test -n "$EXT"; then
				grep -i "('$EXT')" $WORKDIR/pfilesearch-allfiles > $WORKDIR/pfilesearch-tmp
				mv -f $WORKDIR/pfilesearch-tmp $WORKDIR/pfilesearch-allfiles
			fi
			#All this to make [AND] allowed to grep 
			TMP=`echo "$SEARCHSTRING" | grep -o "\[AND\]"` #Check if [AND] is used
			if [ "$TMP" ]; then
				echo -e "\c" > $WORKDIR/pfilesearch-tmp4
				STR1=`echo "$STR1" | sed -e "s/*//g"`
				while read I; do
					export TMP2="$I" #export to use in next-line-func
					grep $GREPCASE -l "$STR1" "$TMP2" >> $WORKDIR/pfilesearch-tmp4
				done < $WORKDIR/pfilesearch-allfiles
				cp -f $WORKDIR/pfilesearch-tmp4 $WORKDIR/pfilesearch-allfiles
				if [ "$STR2" ]; then
					echo -e "\c" > $WORKDIR/pfilesearch-tmp4
					STR2=`echo "$STR2" | sed -e "s/*//g"`
					while read I; do
						export TMP2="$I" #export to use in next-line-func
						grep $GREPCASE -l "$STR2" "$TMP2" >> $WORKDIR/pfilesearch-tmp4
					done < $WORKDIR/pfilesearch-allfiles
					cp -f $WORKDIR/pfilesearch-tmp4 $WORKDIR/pfilesearch-allfiles
				fi
				if [ "$STR3" ]; then
					echo -e "\c" > $WORKDIR/pfilesearch-tmp4
					STR3=`echo "$STR3" | sed -e "s/*//g"`
					while read I; do
						export TMP2="$I" #export to use in next-line-func
						grep $GREPCASE -l "$STR3" "$TMP2" >> $WORKDIR/pfilesearch-tmp4
					done < $WORKDIR/pfilesearch-allfiles
					cp -f $WORKDIR/pfilesearch-tmp4 $WORKDIR/pfilesearch-allfiles
				fi
				if [ "$STR4" ]; then
					echo -e "\c" > $WORKDIR/pfilesearch-tmp4
					STR4=`echo "$STR4" | sed -e "s/*//g"`
					while read I; do
						export TMP2="$I" #export to use in next-line-func
						grep $GREPCASE -l "$STR4" "$TMP2" >> $WORKDIR/pfilesearch-tmp4
					done < $WORKDIR/pfilesearch-allfiles
					cp -f $WORKDIR/pfilesearch-tmp4 $WORKDIR/pfilesearch-allfiles
				fi
				if [ "$STR5" ]; then
					echo -e "\c" > $WORKDIR/pfilesearch-tmp4
					STR5=`echo "$STR5" | sed -e "s/*//g"`
					while read I; do
						export TMP2="$I" #export to use in next-line-func
						grep $GREPCASE -l "$STR5" "$TMP2" >> $WORKDIR/pfilesearch-tmp4
					done < $WORKDIR/pfilesearch-allfiles
					cp -f $WORKDIR/pfilesearch-tmp4 $WORKDIR/pfilesearch-allfiles
				fi
				cp $WORKDIR/pfilesearch-allfiles $HOME/.pfilesearch/hits
	#-----
			else
				while read I; do
					export TMP2="$I" #export to use in next-line-func
					$WORKDIR/pfilesearch-execgrep >> $HOME/.pfilesearch/hits
				done < $WORKDIR/pfilesearch-allfiles
			fi
		fi
	done < $WORKDIR/pfilesearch-SEARCHPATHS
	#remove excluded dir from hit-list
	if [ "$EXCLUDE" ]; then
		grep -v "^${EXCLUDE}" $HOME/.pfilesearch/hits > $WORKDIR/pfilesearch-tmp
		cp -f $WORKDIR/pfilesearch-tmp $HOME/.pfilesearch/hits
	fi
	#find only files with choosen extension
	if test -n "$EXT"; then
		cat $HOME/.pfilesearch/hits | grep -i "('$EXT')" > $WORKDIR/pfilesearch-tmp
		cat $WORKDIR/pfilesearch-tmp > $HOME/.pfilesearch/hits
	fi
	#---
	cp -f $HOME/.pfilesearch/hits /tmp/result-pfilesearch > /dev/null 2>&1 #make it compatible with older versions
	echo "$SEARCHPATHS" > $WORKDIR/SEARCHPATHS #store last used
	#update external gui
	echo true > $WORKDIR/pfilesearch-finish
	#---
	if [ ! -s $HOME/.pfilesearch/hits ] && [ "$BBACKEND" != "true" ]; then
		echo 100 > $WORKDIR/pfilesearch-splash #end progressbar.
		ERROR="$(gettext 'No files found!')"
		. $PROGPATH/func -error_dialog
	fi
	echo 100 > $WORKDIR/pfilesearch-splash #end progress bar
	#show result
	if [ "$BACKEND" != "true" ] && [ -s $HOME/.pfilesearch/hits ]; then
		export pfilesearch_result="
		<window title=\"pFilesearch\">
		<vbox>
		 <edit>
		  <input file>$HOME/.pfilesearch/hits</input>
		  <width>400</width><height>400</height>
		 </edit>
		</vbox></window>"
		I=$IFS; IFS=""
		for STATEMENTS in  $(gtkdialog -p pfilesearch_result --center); do
			eval $STATEMENTS > /dev/null 2>&1
		done
		IFS=$I
	fi
	;;


-write_config)
	echo "export SEARCHPATHS=\"$SEARCHPATHS\""				> $HOME/.pfilesearch/pfilesearchrc
	echo "export EXCLUDE=\"$EXCLUDE\""						>> $HOME/.pfilesearch/pfilesearchrc
	echo "export CHKREC=$CHKREC"							>> $HOME/.pfilesearch/pfilesearchrc
	echo "export CHKXDEV=$CHKXDEV"							>> $HOME/.pfilesearch/pfilesearchrc
	echo "export CHKEXACT=$CHKEXACT"						>> $HOME/.pfilesearch/pfilesearchrc
	echo "export CHKCASE=$CHKCASE"							>> $HOME/.pfilesearch/pfilesearchrc
	echo "export CHKFOLLOW_SYM=$CHKFOLLOW_SYM" 				>> $HOME/.pfilesearch/pfilesearchrc
	. $HOME/.pfilesearch/pfilesearchrc #read config file
	;;



-configure)
	export pfilesearch_preferences='
	<window title="pFilesearch '$(gettext 'Preferences')'" icon-name="gtk-find">
	<vbox space-expand="true" space-fill="true">
	 <notebook labels=" '$(gettext 'Search')' | '$(gettext 'MIME')'" space-expand="true" space-fill="true">
	  <vbox>
	   <frame '$(gettext 'Searchpath')'>
		<hbox>
		 <entry><variable>SEARCHPATHS</variable><input>echo '$SEARCHPATHS'</input></entry>
		 <button>
		  <input file stock="gtk-info"></input>
		  <action>FRAME=Info; IMG=dialog-info; TXT1=LOC0317; . '$PROGPATH'/box_ok</action>
		 </button>
		</hbox>
	   </frame>
	   <frame '$(gettext 'Exclude')'>
		<hbox>
		 <entry><variable>EXCLUDE</variable><input>echo "$EXCLUDE"</input></entry>
		 <button>
		  <input file stock="gtk-info"></input>
		  <action>FRAME=Info; IMG=dialog-info; TXT1=LOC0318; . '$PROGPATH'/box_ok</action>
		 </button>
		</hbox>
	   </frame>
	   <frame '$(gettext 'Options')'>
		<checkbox>
		 <label>'$(gettext 'Search also subdirectories')'</label>
		 <variable>CHKREC</variable>
		 <default>'$CHKREC'</default>
		</checkbox>
		<checkbox>
		 <label>'$(gettext 'Search only current filesystem')'</label>
		 <variable>CHKXDEV</variable>
		 <default>'$CHKXDEV'</default>
		</checkbox>
		<checkbox>
		 <label>'$(gettext 'Match only exact filename')'</label>
		 <variable>CHKEXACT</variable>
		 <default>'$CHKEXACT'</default>
		</checkbox>
		<checkbox>
		 <label>'$(gettext 'Case sensitive')'</label>
		 <variable>CHKCASE</variable>
		 <default>'$CHKCASE'</default>
		</checkbox>
		<checkbox>
		 <label>'$(gettext 'Follow symlinks')'</label>
		 <variable>CHKFOLLOW_SYM</variable>
		 <default>'$CHKFOLLOW_SYM'</default>
		</checkbox>
	   </frame>
	  </vbox>
	  <vbox>
	   <frame '$(gettext 'MIME')'>
		<edit><variable>MIME</variable><input file>'$HOME'/.pfilesearch/pfilesearchMIMErc</input></edit>
	   </frame>
	  </vbox>
	 </notebook>
	 <hbox space-expand="false" space-fill="false">
	  <button cancel><input file stock="gtk-cancel"></input></button>
	  <button ok><input file stock="gtk-ok"></input><action type="exit">OK</action></button>
	 </hbox>
	</vbox>
	</window>'
	I=$IFS; IFS=""
	for STATEMENTS in  $(gtkdialog -p pfilesearch_preferences); do
		eval $STATEMENTS
	done
	IFS=$I
	if [ $EXIT = OK ]; then
		echo "$MIME" > $HOME/.pfilesearch/pfilesearchMIMErc
		. $PROGPATH/func -write_config
	fi
	;;

-error_dialog)
	export error_box="
	<window title=\"pFilesearch\">
	<vbox>
	 <frame $(gettext 'Info')>
	  <pixmap icon_size=\"6\"><input file stock=\"gtk-dialog-error\"></input></pixmap>
	  <text><label>$ERROR</label></text>
	 </frame>
	 <button ok></button>
	</vbox></window>"
	I=$IFS; IFS=""
	for STATEMENTS in  $(gtkdialog -p error_box --center); do
		eval $STATEMENTS > /dev/null 2>&1
	done
	IFS=$I
	;;

-about)
	export pfilesearch_about="
<window title=\"pFilesearch - about\" icon-name=\"gtk-find\">
 <vbox>
  <notebook labels=\"About|License|Credits\">
   <vbox>
    <pixmap icon_size=\"6\"><input file stock=\"gtk-find\"></input></pixmap>
    <text use-markup=\"true\"><label>\"<b>pFilesearch $VERSION - File finder</b>\"</label></text>
    <text use-markup=\"true\"><label>Sigmund Berglund, Copyright 2007-2015</label></text>
    <text><label>\"\"</label></text>
     <button>
      <label>\"http://www.murga-linux.com/puppy/viewtopic.php?t=15136\"</label>
      <action>$BROWSER http:\/\/www.murga-linux.com\/puppy\/viewtopic.php?t=15136</action>
     </button>
   </vbox>
   <vbox>
    <text use-markup=\"true\"><label>\"pFilesearch is released under the <b>GNU General Public License</b> (GPL). You have the right to use and modify this software in any way you like, so long as any derivative works remain under a GPL license.\"</label></text>
    <text use-markup=\"true\"><label>\"This program is distributed in the hope that it will be useful, but <b>WITHOUT ANY WARRANTY</b>. See the GNU General Public License homepage for more details.\"</label></text>
    <hbox>
     <button>
      <label>\"http://www.gnu.org/licenses/\"</label>
      <action>$BROWSER http:\/\/www.gnu.org\/licenses\/</action>
     </button>
    </hbox>
   </vbox>
   <vbox>
    <edit><default>\"Translators:
French - Jean-Jacques Moulinier (esmourguit)
German - Thorsten Köbe (aragon)
Greece - (The Hamsters)
Italian - Angelo Gemmi
Japanese - Shigekazu Satoh (himajin)
Norwegian - Sigmund Berglund (zigbert)
Russian -  Valeriy Danilin
Spanish - Jacob S. Preciado (Proudog)

Others:
Disciple for contribution and bugfixing.
klhrevolutionist for ideas, code support and help with config file
Trapster for sed syntax.
...and more\"</default></edit>
   </vbox>
  </notebook>
  <hbox>
   <button ok>
    <input file icon=\"gtk-apply\"></input>
   </button>
  </hbox>
 </vbox>
</window>"
	gtkdialog -p pfilesearch_about --center
	;;
esac
