#!/bin/sh
# locate-GUI written by smokey01 and CatDude with help from SFR and Geoffrey
# /usr/local/bin/locate-GUI
# Version 2.0 released 26 September 2015

export LOOPPID=/tmp/locator_loop_pid
rm -f "$LOOPPID"

##The right-clicks menu function
gtkdm () {
ITEMS=""
ICONS=""
HEIGHT=20
echo -n > /tmp/OUTPUT

#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do
		case $1 in
		-h|--help)
			echo 'Usage: gtkdialog_menu [OPTION] "stock-icon1,menuitem1" "stock-icon2,menuitem2" ...
example: gtkdialog_menu -m middle "gtk-open,Open file" "gtk-save,Save file"
Options
  -h         Show this information
  -m CLICK   Activate menu by clicking mousebutton
             This uses $BUTTON from main gtkdialog gui
                left
                middle
                right (default)'
            exit
            ;;
		-m) #left-, mid- or right-click
			export MOUSE="$2"
			shift
			;;
		*)
			[ ! "$1" ] && break
			ICON=`echo "$1" | cut -d',' -f1`
			LABEL=`echo "$1" | cut -d',' -f2`
			if [ "$ICON" = "seperator" ]; then
				ICONS="$ICONS"'<text height-request="2"><label>""</label></text>'
				ITEMS="$ITEMS"'<entry height-request="2"></entry>'
			else
				ITEMS="$ITEMS"'<button height-request="20" xalign="0" can-focus="no" relief="2"><label>'"$LABEL"'</label><action>echo '"$LABEL"' > /tmp/OUTPUT</action><action>EXIT:exit</action></button>'
				ICONS="$ICONS"'<pixmap height-request="20" icon_size="1"><input file stock="'"$ICON"'"></input></pixmap>'
			fi
			HEIGHT=$((HEIGHT+20))
			;;
		esac
		shift
		I=$[$I+1]
	done
done



#check if left-click or right-click
case $MOUSE in
	left)	[ ! $BUTTON = 1 ] && exit;;
	middle)	[ ! $BUTTON = 2 ] && exit;;
	*)		[ ! $BUTTON = 3 ] && exit;;
esac


#show menu
export gtkdialog_menu='
<window title="menu" decorated="false"  height-request="'$HEIGHT'" skip_taskbar_hint="true" window_position="2">
 <hbox>
  <vbox>
   '$ICONS'
  </vbox>
  <vbox>
   '$ITEMS'
  </vbox>
 </hbox>
 <action signal="focus-out-event">EXIT:exit</action>
 </window>'
gtkdialog -p gtkdialog_menu > /dev/null
cat /tmp/OUTPUT #send output to stdout
}
export -f gtkdm



pb () {
  while :; do
   for i in $(seq 0 1 100); do echo $i; sleep 0.3; done
  done
}
export -f pb


TMPDIR=/tmp/mylocate/
mkdir -p "$TMPDIR"
export databasefile="/var/lib/locate/locatedb"
export resultsfile=""$TMPDIR"locate-results"
export helpfile=""$TMPDIR"hunt-help"
export dbdatefile=""$TMPDIR"dbdate"


   # If this is the first run, create the 'databasefile'
   if [ ! -f "$databasefile" ]; then
      #echo "" > "$databasefile"
      lastupdate="No current database exists"
      echo "$lastupdate" > "$dbdatefile"
   else
      lastupdate=`date -r "$databasefile"`
      echo "$lastupdate" > "$dbdatefile"
   fi

   # If this is the first run, create the 'resultsfile'
   if [ ! -f "$resultsfile" ]; then
      echo "This is the first run, you have no previous results during this session." > "$resultsfile"
   fi

sleep 0.5

myhelp () {
   echo "Type in your search string and press enter. You can use various patterns like .jpg, photo etc.
Example: -ci .jpg will display the total number of jpg files, ignoring case, in the database.

Right-click on the results to either launch them or show location. Rox or Thunar is used as the 
launcher so don't click on anything you would not normally open in Rox or Thunar.

To create a database of all your files, click on the disk icon. Only Drives/Partitions that are mounted
will be indexed. The previous database will be overwritten so make sure you mount the
Drives/Partitions you want to include prior to updating the locate database.
 
To see locate help, type --help in the search box.
Examples: -S will show statistics, -c file count, -c filename will show instances of file, and
-i filename ignores case.

A very powerful search tool is RegEx (Regular Expression). The -r switch will invoke it.
Used with the (Ignore Case) -i switch can be very useful. For example, I know I have a number of
Dan Brown eBooks I bought for my wife but I have no idea which drive or partition I put them on.
What makes it even more difficult, I can't remember the titles which probably have spaces in them.
If I use the default search for dan brown, it will find every instance of dan and brown or words
than contain the same. It could be a very long list.

This is how I would search for dan brown. I will use the ignore case switch with the regex switch.
EG: '-ri dan\sbrown' This will narrow the search to Dan Brown or dan brown with a space in between.
The \s (space) switch is pretty cool. Sometimes you may be looking for a single word. To isolate
the word, place a space at each end. '-ri \spuppy\s' will find all files with puppy in the name
and a space at each side.   

A pretty good guide to using RegEx can be found here:
http://www.google.com/support/enterprise/static/postini/docs/admin/en/admin_ee_cu/cm_regex.html

I really don't need much more than the -ri and \s switches but there are more if you need them.

A regex search will take a little longer than the default search but it is doing more work.
I have four HDD with a total of 15 partitions which adds up to about 7TB of space and about
1.8M files. The RegEx search takes about five seconds and the default search about one second
but there are a lot of factors than can affect this. I only provided this info as a guideline.

Dependencies: http://www.gnu.org/software/findutils/ (locate & updatedb)

Compile instructions:

   ./configure --prefix=/usr --libdir=/usr/lib --localstatedir=/var/lib/locate
   make
   make install

locate-GUI written by smokey01 and CatDude with help from SFR and Geoffrey. " > "$helpfile"
}
export -f myhelp

golook () {
   locate $search > "$resultsfile"
}
export -f golook

dbrefresh () {
  if [ -e "$databasefile" ]; then
   latestupdate=`date -r "$databasefile"`
   echo "${latestupdate}" > "$dbdatefile"
  fi
}
export -f dbrefresh

external_menu(){
[ "$(command -v thunar)" ] && FILEMANAGER="thunar" || FILEMANAGER="rox"
if [ ! -z "$LIST" ]; then
# echo "$FILEMANAGER"
	OUTPUT="`gtkdm "gtk-open,Show Location" "gtk-open,Open File" "gtk-cancel,Cancel"`"
   case $OUTPUT in
      'Show Location')
         "$FILEMANAGER" "${LIST%/*}" &
         ;;
      'Open File')
         "$FILEMANAGER" "$LIST" &
         ;;
       'Cancel')
		 exit
		 ;;
      esac
 else
echo "Do nothing"
fi     
}
export -f external_menu 

export HELP='
  <window border="0" height-request="450" width-request="723" window-position="3" title="locate-GUI Help Version 2.0" icon-name="gtk-help">
  <vbox  scrollable="true">
  <text width-request="680">
  <input file>'"$helpfile"'</input>
  </text> 
  </vbox>
  <variable>HELP</variable>
  </window>'
 
export DIALOG='
<window window-position="3" title="locate-GUI Version 2.0 - Search for files and folders in the locate database" resizable="true" icon-name="gtk-find">
  <vbox>
  <hbox>
    <text><label>Search:</label></text>
    <entry activates_default="true" tooltip-text=" Enter the Filename to search " width-request="300">
    <variable>search</variable>
    </entry>
    <button tooltip-text=" Clear Search ">
    <input file stock="gtk-clear"></input>
    <action>clear:search</action>
    <action>echo "" > '"$resultsfile"'</action>
    </button>
   
    <button can-default="true" has-default="true" use-stock="true" tooltip-text=" Send request ">
    <input file stock="gtk-ok"></input>
    <action>golook</action>
    </button>
   
    <text><label>Database:</label></text>
    <entry width-request="220" tooltip-text=" This is when the database was last updated ">
    <variable>thedate</variable>
    <default>'"$lastupdate"'</default>
    <input file>/tmp/mylocate/dbdate</input>
    <action>enable:varBUTTON</action>
    </entry>
    </hbox>
   
         <vbox scrollable="true" height="220">
        <tree enable-search="false" hover-selection="true" exported-column="0" headers-visible="false" auto-refresh="true">
            <variable>LIST</variable>
            <input file>'"$resultsfile"'</input>
            <action signal="button-press-event">external_menu &</action>
        </tree>
         </vbox>
     
   
    <hbox homogeneous="false">   
     <notebook show-border="false" show-tabs="false" tab-base-index="0">
         <text><label>""</label></text>
         <progressbar width-request="620">
            <label>"Please wait while the database is being updated..."</label>
           <input>pb & echo $! > "$LOOPPID"</input>
         </progressbar>

        <variable>varINDEX</variable>
        <input>[ -e /var/lib/locate/locatedb.n ] && echo 1 || echo 0</input>
      </notebook>
      <timer visible="false" milliseconds="true" interval="500">
        <action>refresh:varINDEX</action>
        <action>dbrefresh</action>
        <action>refresh:thedate</action>
      </timer>
       
    <button tooltip-text=" Create or Update the database ">
      <variable>varBUTTON</variable>
      <input file stock="gtk-save"></input>
      <action>disable:varBUTTON</action>
      <action>updatedb &</action>
    </button>
   
    <button tooltip-text=" Help ">
      <input file stock="gtk-help"></input>
      <action>myhelp</action>
      <action type="launch">HELP</action>
    </button>
     
    <button tooltip-text=" Quit ">
       <input file stock="gtk-quit"></input>
       <action>Exit:0</action>
    </button>
    </hbox>

     
  </vbox>
</window>
'
gtkdialog -p DIALOG

# we need to kill the infinite loop from 'pb' function
if [ -e "$LOOPPID" ]; then
  PID=`cat "$LOOPPID"`
  kill -0 $PID 2>/dev/null && kill $PID
fi

echo "" > "$resultsfile" 
