#!/bin/bash
#pfilesearch
#Copyright 2008,2009,2010,2011,2012,2014
#Sigmund Berglund

#------------------------------
#This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 2.

#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. <http://www.gnu.org/licenses/>.
#------------------------------

CURDIR="`pwd`"
export VERSION="1.35"
export PROGPATH=`dirname "$0"`"/"
export WORKDIR=$HOME/.pfilesearch/tmp
[ ! -e $HOME/.pfilesearch ] && mkdir $HOME/.pfilesearch
[ ! -e $WORKDIR ] && mkdir $WORKDIR
echo -n > $HOME/.pfilesearch/hits
GTKDIALOG="gtkdialog"
export TEXTDOMAIN=pfilesearch #gettext

#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do #check all parameters
		case "$1" in
			-h)echo $(gettext "Options")'
  -b          '$(gettext "use as backend. Do not show searchresult")'
  -bb         '$(gettext "Do not show 'No Files found' box")'
  -bbb        '$(gettext "Do not show 'Searching...' box")'
  -c [STRING] '$(gettext "search quiet. No gui")'
  -d [DIR]    '$(gettext "Search in a specific directory")'
  -e          '$(gettext "Run embedded - gtkdialog code is pointed to stdout")'
  -ee         '$(gettext "Run embedded - skip standalone options")'
  -f [FILTER] '$(gettext "Search for only groups of files:")'
              'documents $(gettext "documents")'
              'pictures $(gettext "pictures")'
              'music $(gettext "music")'
              'videos $(gettext "videos")'
              'compressed_files $(gettext "compressed_files")'
              'code_files $(gettext "code_files")'
  -i          '$(gettext "install config files and exit")'
  -h          '$(gettext "show this help message")'
  -p          '$(gettext "show preferences gui at startup")'
  -t [STRING] '$(gettext "Define default search text")'
  -v          '$(gettext "show Pfilesearch version")
 		 		exit
 		 		;;
 			-b) export BACKEND=true;;
 			-bb) export BACKEND=true; export BBACKEND=true;;
 			-bbb) export BACKEND=true; export BBACKEND=true; export BBBACKEND=true;;
 		 	-d) OVERRIDE_SEARCHPATHS="$2"
 		 		[ "$OVERRIDE_SEARCHPATHS" = './' ] && OVERRIDE_SEARCHPATHS="`pwd`"
 		 		shift; I=$[$I + 1];;
 		 	-e) EMBED=true; BACKEND=true;;
 		 	-ee) EMBED=true; EMBED2=true; BACKEND=true;;
 		 	-f) FILTER=$2; shift; I=$[$I + 1];;
  			-i) INTERFACE=install;;
			-c) INTERFACE=cli; CLI_STRING="$2"; shift; I=$[$I + 1];;
			-p) INTERFACE=pref;;
			-t) OVERRIDE_SEARCHTEXT="$2"; shift; I=$[$I + 1];;
			-v) echo "Pfilesearch $VERSION"; exit;;
		esac
		shift
		I=$[$I + 1]
	done
done
#cleanup
echo -n > $WORKDIR/SEARCHPATHS
echo -n > $WORKDIR/pfilesearch-execfind

if [ "$BACKEND" = "true" ]; then
	#default 
#	CHKFILENAME="true"; CHKCONTENT="false"; SEARCHSTRING="Search text"
	CHKFILE="false"; CHKSYMLN="false"; CHKDIR="false"
	TODAY=`date +%m/%d/%Y`; CHKATIME="false"; CHKCTIME="false"; CHKMTIME="false"; DATE_FROM="01/01/1970"; DATE_TO="$TODAY"
	P_USER=""; SIZE_GT=""; SIZE_LT=""
	CHK_UR="false"; CHK_UW="false"; CHK_UX="false"
	CHK_GR="false"; CHK_GW="false"; CHK_GX="false"
	CHK_OR="false"; CHK_OW="false"; CHK_OX="false"
fi
CHKDOC="false"; CHKPIC="false"; CHKMUSIC="false"; CHKVIDEO="false"; CHKMAIL="false"; CHKZIP="false"; CHKCODE="false" #filters
case $FILTER in
	documents)	CHKDOC=true;;
	pictures)	CHKPIC=true;;
	music)		CHKMUSIC=true;;
	videos)		CHKVIDEO=true;;
	compressed_files) CHKZIP=true;;
	code_files)	CHKCODE=true;;
esac


#check if MIME-file is installed
if [ ! -f $HOME/.pfilesearch/pfilesearchMIMErc ]; then #no rc-file
	mkdir $HOME/.pfilesearch 2> /dev/null
	cp -f $PROGPATH/pfilesearchMIMErc $HOME/.pfilesearch/pfilesearchMIMErc
fi

[ ! -e $HOME/.pfilesearch/pfilesearchrc ] && cp $PROGPATH/pfilesearchrc $HOME/.pfilesearch/pfilesearchrc


export EXTDOC=`grep document= $HOME/.pfilesearch/pfilesearchMIMErc | sed -e s/^.*\=//g | sed -e 's/ ./$\\\|\\\./g'`
#'$' makes the end of word (extension) '\|' makes grep either
export EXTPIC=`grep picture= $HOME/.pfilesearch/pfilesearchMIMErc | sed -e s/^.*\=//g | sed -e 's/ ./$\\\|\\\./g'`
export EXTMUSIC=`grep music= $HOME/.pfilesearch/pfilesearchMIMErc | sed -e s/^.*\=//g | sed -e 's/ ./$\\\|\\\./g'`
export EXTVIDEO=`grep video= $HOME/.pfilesearch/pfilesearchMIMErc | sed -e s/^.*\=//g | sed -e 's/ ./$\\\|\\\./g'`
export EXTMAIL=`grep mail= $HOME/.pfilesearch/pfilesearchMIMErc | sed -e s/^.*\=//g | sed -e 's/ ./$\\\|\\\./g'`
export EXTZIP=`grep zip= $HOME/.pfilesearch/pfilesearchMIMErc | sed -e s/^.*\=//g | sed -e 's/ ./$\\\|\\\./g'`
export EXTCODE=`grep code= $HOME/.pfilesearch/pfilesearchMIMErc | sed -e s/^.*\=//g | sed -e 's/ ./$\\\|\\\./g'`

#default 
TODAY=`date +%m/%d/%Y`; DATE_FROM="01/01/1970"; DATE_TO="$TODAY"
#rcfile
export SEARCHPATHS="/"
export EXCLUDE="/initrd/pup_*"
. $HOME/.pfilesearch/pfilesearchrc 2> /dev/null #read config file

[ "$OVERRIDE_SEARCHPATHS" ] && export SEARCHPATHS="$OVERRIDE_SEARCHPATHS" #parameter -d is used
[ "$OVERRIDE_SEARCHTEXT" ] && export SEARCHSTRING="$OVERRIDE_SEARCHTEXT" #parameter -t is used
[ "$SEARCHPATHS" = "/" ] && SEARCHPATHS="/  ($(gettext 'All files'))"
echo "$SEARCHPATHS" > $WORKDIR/SEARCHPATHS #we add it to a file to update the field later inside ie. Pfind

case $INTERFACE in
	install)
		. $PROGPATH/func -write_config
		exit
		;;
	cli)
		SEARCHSTRING="$CLI_STRING"
		. $PROGPATH/func -search
		exit
		;;
	pref)
		. $PROGPATH/func -configure
		exit
		;;
esac


[ "$EMBED" != "true" ] && S='<window title= "Pfilesearch" icon-name="gtk-find">'
 S=$S'<hbox space-expand="true" space-fill="true">
  <vbox space-expand="true" space-fill="true">
   <hbox>
    <vbox space-expand="true" space-fill="true">
     <frame '$(gettext "Search for")'>'
      [ "$EMBED2" != "true" ] && S=$S'<hbox>
       <hbox width-request="100" space-expand="true" space-fill="true">
        <entry activates-default="true">
         <variable>SEARCHSTRING</variable>
         <input>echo "'$SEARCHSTRING'"</input>
        </entry>
       </hbox>
       <button>
        <input file stock="gtk-info"></input>
        <action>FRAME=Info; IMG=dialog-info; TXT1="LOC0313"; . '$PROGPATH'/box_ok</action>
       </button>
      </hbox>'
      S=$S'
      <hbox>
       <vbox>
        <checkbox><label>'$(gettext 'Filename')'</label><variable>CHKFILENAME</variable><default>true</default></checkbox>
        <checkbox><label>'$(gettext 'Text in file (slow)')'</label><variable>CHKCONTENT</variable></checkbox>
       </vbox>
       <text space-expand="true" space-fill="true"><label>""</label></text>
       <vbox>
        <text space-expand="true" space-fill="true"><label>""</label></text>
        <button>
         <input file stock="gtk-info"></input>
         <action>FRAME=Info; IMG=dialog-info; TXT1="LOC0312"; . '$PROGPATH'/box_ok</action>
        </button>
       </vbox>
      </hbox>
     </frame>
     <frame '$(gettext 'Search in')'>
      <hbox>
       <hbox width-request="100" space-expand="true" space-fill="true">
        <entry accept="directory">
         <variable>SEARCHPATHS</variable>
         <input file>'$WORKDIR'/SEARCHPATHS</input>
        </entry>
       </hbox>
       <button>
        <input file stock="gtk-open"></input>
        <action type="fileselect">SEARCHPATHS</action>
       </button>
       <button>
        <input file stock="gtk-info"></input>
        <action>FRAME=Info; IMG=dialog-info; TXT1="LOC0311"; . '$PROGPATH'/box_ok</action>
       </button>
      </hbox>
     </frame>
     <frame '$(gettext 'Exclude')'>
      <hbox>
       <hbox width-request="100" space-expand="true" space-fill="true">
      <entry accept="directory"><variable>EXCLUDE</variable><input>echo "'$EXCLUDE'"</input></entry>
       </hbox>
       <button>
        <input file stock="gtk-open"></input>
        <action type="fileselect">EXCLUDE</action>
       </button>
       <button>'"
        <input file stock=\"gtk-info\"></input>
        <action>FRAME=Info; IMG=dialog-info; TXT1=LOC0316; . $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>
    <hbox>
     <frame '$(gettext 'Match only')'>
      <vbox space-expand="false" space-fill="false">
       <radiobutton visible="false"><label>a</label><default>true</default></radiobutton>
       <radiobutton>
        <label>'$(gettext 'File')'</label>
        <variable>CHKFILE</variable>
        <action>if true enable:CHKDOC</action>
        <action>if true enable:CHKPIC</action>
        <action>if true enable:CHKMUSIC</action>
        <action>if true enable:CHKVIDEO</action>
        <action>if true enable:CHKZIP</action>
        <action>if true enable:CHKCODE</action>
       </radiobutton>
       <radiobutton>
        <label>'$(gettext 'Symlink')'</label>
        <variable>CHKSYMLN</variable>
        <action>if true enable:CHKDOC</action>
        <action>if true enable:CHKPIC</action>
        <action>if true enable:CHKMUSIC</action>
        <action>if true enable:CHKVIDEO</action>
        <action>if true enable:CHKZIP</action>
        <action>if true enable:CHKCODE</action>
       </radiobutton>
       <radiobutton>
        <label>'$(gettext 'Directory')'</label>
        <variable>CHKDIR</variable>
        <action>if true disable:CHKDOC</action>
        <action>if true disable:CHKPIC</action>
        <action>if true disable:CHKMUSIC</action>
        <action>if true disable:CHKVIDEO</action>
        <action>if true disable:CHKZIP</action>
        <action>if true disable:CHKCODE</action>
       </radiobutton>
       <text><label>""</label></text>
       <text><label>""</label></text>
       <checkbox><label>'$(gettext 'Documents')'</label><variable>CHKDOC</variable><default>'$CHKDOC'</default></checkbox>
       <checkbox><label>'$(gettext 'Pictures')'</label><variable>CHKPIC</variable><default>'$CHKPIC'</default></checkbox>
       <checkbox><label>'$(gettext 'Soundfiles')'</label><variable>CHKMUSIC</variable><default>'$CHKMUSIC'</default></checkbox>
       <checkbox><label>'$(gettext 'Videos')'</label><variable>CHKVIDEO</variable><default>'$CHKVIDEO'</default></checkbox>
       <checkbox><label>'$(gettext 'Compressed files')'</label><variable>CHKZIP</variable><default>'$CHKZIP'</default></checkbox>
       <checkbox><label>'$(gettext 'Codefiles files')'</label><variable>CHKCODE</variable><default>'$CHKCODE'</default></checkbox>
      </vbox>
     </frame>
    </hbox>
   <vbox>
    <frame '$(gettext 'Match date')'>
      <vbox>
       <hbox spacing="0">
        <radiobutton visible="false"><label>a</label><default>true</default></radiobutton>
        <radiobutton>
         <label>atime</label>
         <variable>CHKATIME</variable>
         <action>enable:DATE_TO</action>
         <action>enable:DATE_FROM</action>
        </radiobutton>
        <radiobutton>
         <label>ctime</label>
         <variable>CHKCTIME</variable>
         <action>enable:DATE_TO</action>
         <action>enable:DATE_FROM</action>
        </radiobutton>
        <radiobutton>
         <label>mtime</label>
         <variable>CHKMTIME</variable>
         <action>enable:DATE_TO</action>
         <action>enable:DATE_FROM</action>
        </radiobutton>
       </hbox>
       <hbox>
        <text><label>'$(gettext 'from')'</label></text>
        <vbox>
         <entry>
         <variable>DATE_FROM</variable>
         <input>echo "'$DATE_FROM'"</input>
         <width>90</width><height>27</height>
         <sensitive>false</sensitive>
        </entry>
       </vbox>
      </hbox>
       <hbox>
        <button space-expand="false" space-fill="false">
         <input file stock="gtk-info"></input>
         <action>FRAME=Info; IMG=dialog-info; TXT1="LOC0314"; . '$PROGPATH'/box_ok</action>
        </button>
        <text space-expand="true" space-fill="true"><label>""</label></text>
        <text space-expand="false" space-fill="false"><label>'$(gettext 'to')'</label></text>
        <hbox width-request="90">
         <entry>
          <variable>DATE_TO</variable>
          <input>echo "'$DATE_TO'"</input>
          <width>90</width><height>27</height>
          <sensitive>false</sensitive>
         </entry>
        </hbox>
       </hbox>
      </vbox>
    </frame>
    <frame '$(gettext 'Match size')'>
      <vbox>
       <hbox>
        <text><label>'$(gettext 'Greater than')'</label></text>
        <vbox>
         <entry>
          <variable>SIZE_GT</variable>
          <input>echo "'$SIZE_GT'"</input>
          <width>50</width><height>27</height>
         </entry>
        </vbox>
       </hbox>
       <hbox>
        <button space-expand="false" space-fill="false">
         <input file stock="gtk-info"></input>
         <action>FRAME=Info; IMG=dialog-info; TXT1="LOC0315"; . '$PROGPATH'/box_ok</action>
        </button>
        <text space-expand="true" space-fill="true"><label>""</label></text>
        <text space-expand="false" space-fill="false"><label>'$(gettext 'Less than')'</label></text>
        <vbox>
         <entry>
          <variable>SIZE_LT</variable>
          <input>echo "'$SIZE_LT'"</input>
          <width>50</width><height>27</height>
         </entry>
        </vbox>
       </hbox>
      </vbox>
    </frame>
    <frame '$(gettext 'Match user')'>
     <hbox>
      <text><label>'$(gettext 'Name or ID')'</label></text>
     <hbox width-request="90">
      <entry><variable>P_USER</variable><input>echo "'$P_USER'"</input><width>90</width><height>27</height></entry>
     </hbox>
     </hbox>
    </frame>
    <frame '$(gettext 'Match mode')'>
     <hbox>
      <vbox spacing="0">
       <text><label>"'$(gettext 'Read')'  "</label></text>
       <checkbox><label>" "</label><variable>CHK_UR</variable></checkbox>
       <checkbox><label>" "</label><variable>CHK_GR</variable></checkbox>
       <checkbox><label>" "</label><variable>CHK_OR</variable></checkbox>
      </vbox>
      <vbox spacing="0">
       <text><label>"'$(gettext 'Write')'  "</label></text>
       <checkbox><label>" "</label><variable>CHK_UW</variable></checkbox>
       <checkbox><label>" "</label><variable>CHK_GW</variable></checkbox>
       <checkbox><label>" "</label><variable>CHK_OW</variable></checkbox>
      </vbox>
      <vbox spacing="0">
       <hbox>
        <text space-expand="false" space-fill="false"><label>"'$(gettext 'exec')'"</label></text>
        <text space-expand="true" space-fill="true"><label>""</label></text>
       </hbox>
       <checkbox><label>"   '$(gettext 'User')'"</label><variable>CHK_UX</variable></checkbox>
       <checkbox><label>"   '$(gettext 'Group')'"</label><variable>CHK_GX</variable></checkbox>
       <checkbox><label>"   '$(gettext 'Other')'"</label><variable>CHK_OX</variable></checkbox>
      </vbox>
     </hbox>
    </frame>
   </vbox>
  </hbox>'
  
  [ "$EMBED2" != "true" ] && S=$S'<hbox space-expand="false" space-fill="false">
   <button cancel>
    <input file stock="gtk-cancel"></input>'"
    <action type=\"exit\">99</action>
   </button>
   <text><label>\"      \"</label></text>
   <button>
    <input file stock=\"gtk-preferences\"></input>
    <label>$(gettext 'Preferences')</label>
    <action type=\"exit\">98</action>
   </button>
   <button can-default=\"true\" has-default=\"true\" use-stock=\"true\">
    <input file stock=\"gtk-find\"></input>
    <label>$(gettext 'Search')</label>
    <action type=\"exit\">80</action>
   </button>
  </hbox>"

 S=$S'</vbox>
</hbox>'
[ "$EMBED" != "true" ] && S=$S'</window>'
export pfilesearch_gui="$S"

if [ "$EMBED" = "true" ]; then echo "$pfilesearch_gui"; exit; fi

I=$IFS; IFS=""
for STATEMENTS in  $($GTKDIALOG -p pfilesearch_gui); do
	eval $STATEMENTS
done
IFS=$I
case $EXIT in
	80) . $PROGPATH/func -search;;
	98) . $PROGPATH/func -configure;;
	*)	rm $WORKDIR/pfilesearch* > /dev/null 2>&1
		exit 0;;
esac
