#!/bin/sh

# =============================================================================
# Find'N'Run - find and run apps very quickly
  Version=1.0
# March 2015 by SFR and L18L; GNU GPL applies
# http://www.murga-linux.com/puppy/viewtopic.php?t=98330
# Req.: Gtkdialog >= 0.8.3, grep, tac, sort, cut, head, tail
# =============================================================================

export TEXTDOMAIN=find_n_run
export OUTPUT_CHARSET=UTF-8

export APP_TITLE="$(gettext 'Find'\''N'\''Run')-${Version}"
export ABOUT="`tail -n +4 "$0" | head -n 4 | cut -c 2-`"

export LOC1="/usr/share/applications/"
export LOC2="/usr/local/share/applications/"
export LOC3="${XDG_DATA_HOME}/applications/"
#export LOC4="${HOME}/.local/share/applications/"

export CONFIG="${HOME}/.findnrunrc"

# -----------------------------------------------------------------------------

#LANG=de_DE.UTF-8 # just for demo
LR=${LANG%.*} #ex:pt_BR
L=${LANG%_*}  #ex:pt

# -----------------------------------------------------------------------------

# Defaults
defOPEN=false

if [ -f "$CONFIG" ]; then
. "$CONFIG"
fi

# =============================================================================

unset varENTRY	# to avoid passing this var while launching F'N'R from within F'N'R ;)

[ "`which gtkdialog4 2>/dev/null`" ] && GTKDIALOG=gtkdialog4 || GTKDIALOG=gtkdialog

# -----------------------------------------------------------------------------

export GUI_ABOUT='
<window title="'${APP_TITLE}'" icon-name="edit-find" window-position="2">
  <vbox>
    <frame>
      <text justify="2" selectable="true" can-focus="false">
        <input>echo "${ABOUT}"</input>
      </text>
    </frame>
    <hbox homogeneous="true">
      <button ok>
        <action>closewindow:GUI_ABOUT</action>
      </button>
    </hbox>
  </vbox>
  <variable>GUI_ABOUT</variable>
</window>'

# -----------------------------------------------------------------------------

export GUI_MAIN='
<window title="'${APP_TITLE}'" icon-name="edit-find" window-position="2"  height-request="278">
  <vbox>    
    <hbox tooltip-text="'$(gettext "Run by double click or ENTER key")'">
      <text>
        <label>'$(gettext 'Narrow search by input of some letters:')'</label>
      </text>
      <entry secondary-icon-stock="gtk-close">
        <variable>varENTRY</variable>
        <action>refresh:varLIST</action>
        <action signal="activate">grabfocus:varLIST</action>
        <action signal="secondary-icon-release">clear:varENTRY</action>
      </entry>
    </hbox>
    
    <tree enable-search="false" exported-column="0" headers-visible="false">
      <variable>varLIST</variable>
      <label>Name</label>
      <input>grep -H -r -E "^Name(\['${LR}'\]|\['${L}'\])?=" ${LOC1} ${LOC2} ${LOC3} --include="*.desktop" 2>/dev/null | tac | sort -t: -u -k1,1 | cut -f2 -d "=" | sort -f -u | grep -F -i -- "$varENTRY"</input>
      <action>$(grep -h -r -C1000 "^Name.*=.*${varLIST}" ${LOC1} ${LOC2} ${LOC3} --include="*.desktop" 2>/dev/null | grep -m1 "^Exec=" | cut -f2 -d "=" | cut -f1 -d "%") &</action>
      <action condition="active_is_false(varOPEN)">exit:EXIT</action>
    </tree>
    
    <hbox>
      <checkbox use-underline="true">
        <label>'$(gettext "_Leave this window opened")'</label>
        <default>'${defOPEN}'</default>
        <variable>varOPEN</variable>
        <action>echo "defOPEN=$varOPEN" > "'${CONFIG}'"</action>
      </checkbox>
      <text space-fill="true" space-expand="true"><label>""</label></text>
 	  <button tooltip-text="'$(gettext "About")'">
	    <input file stock="gtk-about"></input>
	    <action>launch:GUI_ABOUT</action>
	  </button>
      <button tooltip-text="'$(gettext "Exit")'">
        <input file stock="gtk-quit"></input>
        <action>EXIT:quit</action>
      </button>
    </hbox>

  </vbox>
  <action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Escape ] && echo true )">Exit:0</action>
  <action signal="delete-event">EXIT:abort</action>
</window>'

$GTKDIALOG -p GUI_MAIN

###############################################################################
