#!/bin/bash
#PupMenu 3.3
#pRun 1.0
#Copyright 2013 GPL3
#Roger Grider (radky)

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@}

#check for another instance of pRun
PRPID=$(ps ax | grep gtkdialog | grep PRUN_DIALOG | awk '{print $1}')
if [ "$PRPID" ]; then
 sleep 1
 [ "$PRPID" ] && Xdialog --title Alert --beep --msgbox "  pRun is already active.  " 0 0 && exit 0
fi

DEF_LANG=$LANG; LANG=C
export WORKDIR="/usr/local/prun"
export ICONDIR="/usr/local/PupMenu/icons"

#define gtkdialog
if [ "`which gtkdialog4`" ]; then
	GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
	GTKDIALOG=gtkdialog3
else
	GTKDIALOG=gtkdialog
fi

#define special Sans font for pRun
FONT_SIZE=`cat $WORKDIR/preferences/fontsize`
[ "$FONT_SIZE" = "" ] && FONT_SIZE="10"
echo "style \"specialsize\"
{
  font_name=\"Sans $FONT_SIZE\"
}
widget \"*\" style \"specialsize\"
class \"*\" style \"specialsize\"

style \"specialframe\"
{
  font_name=\"bold\"
  fg[NORMAL]=\"grey40\"
}
widget \"*.GtkFrame.GtkLabel\" style \"specialframe\"
class \"*.GtkFrame.GtkLabel\" style \"specialframe\"" > "$WORKDIR/gtkrc_special"

export GTK2_RC_FILES="$WORKDIR/gtkrc_special:${HOME}/.gtkrc-2.0"

#define title-bar icon
ln -snf $ICONDIR/prun48.png /usr/share/icons/hicolor/48x48/apps && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

#define togglebutton parameters
TMPDIR=/tmp/pRun/prun
mkdir -p "$TMPDIR"
[ ! -f "$TMPDIR/search.png" ] && cp $ICONDIR/enhancedsearch.png $ICONDIR/standardsearch.png $TMPDIR/
searchENHANCED=`cat $WORKDIR/preferences/search_all`
[ "$searchENHANCED" = "true" ] && ln -sf $TMPDIR/enhancedsearch.png $TMPDIR/search.png
[ "$searchENHANCED" = "false" ] && ln -sf $TMPDIR/standardsearch.png $TMPDIR/search.png

#define default terminal
export TERM=`cat $WORKDIR/preferences/terminal_emulator`
#[ "$TERM" = "rxvt" ] && TERM="rxvt -rv"

#define terminal status after launch
export CLOSE_TERMINAL=`cat $WORKDIR/preferences/close_terminal`

#define closing of GUI after launch
export CLOSE_PRUN=`cat $WORKDIR/preferences/close_prun`

#define maximum number of items in launch history
export HISTORY_ITEMS=`cat $WORKDIR/preferences/history_items`

#define applications in $PATH
searchPATH=`echo "$PATH" | sed 's/:/ /g'`
PRUN_ITEMS=`ls $searchPATH | grep -Ev '^$|\[|:|.txt|^[0-9]|NOTUSED' | sort -fu`
echo -e "$PRUN_ITEMS" > /tmp/PR-apps
echo -e "$PRUN_ITEMS" > /tmp/PR-search

#pRun launch history
echo '#!/bin/sh
WORKDIR="/usr/local/prun"
if [ "`which gtkdialog4`" ]; then
 GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
 GTKDIALOG=gtkdialog3
else
 GTKDIALOG=gtkdialog
fi
HISTORY_ITEMS=`cat $WORKDIR/preferences/history_items`' > /tmp/PR-history
echo 'export PRUN_HISTORY="
<window title=\"pRun\" icon-name=\"prun48\" resizable=\"false\">
  <vbox>
   <text use-markup=\"true\"><label>\"<b><span size='"'large'"'>Launch History</span></b>\"</label></text>
   <hbox>
    <vbox>
     <tree rules-hint=\"false\" stock-id=\"gtk-justify-fill\" tooltip-text=\" Left-click to launch \" headers-visible=\"false\">
      <label>Select an application</label>
      <height>385</height><width>315</width>
      <variable>hxAPP</variable>
      <input>tac $WORKDIR/PR-history | head -$HISTORY_ITEMS</input>
      <action type=\"enable\">HXLAUNCH_BUTTON</action>
      <action signal=\"button-release-event\">echo \$hxAPP > /tmp/PR-hxAPP</action>
      <action signal=\"button-release-event\">echo \$hxAPP >> $WORKDIR/PR-history</action>
      <action signal=\"button-release-event\">. $WORKDIR/func -history_launch &</action>
      <action signal=\"button-release-event\">exit:Exit</action>
      <action signal=\"key-release-event\">echo \$hxAPP > /tmp/PR-hxAPP</action>
      <action signal=\"key-release-event\">echo \$hxAPP >> $WORKDIR/PR-history</action>
     </tree>
     <hseparator></hseparator>
    </vbox>
   </hbox>
    <vbox>
    <hbox homogeneous=\"true\">
     <button use-underline=\"true\" width-request=\"100\" tooltip-text=\" Launch menu item \">
      <label>_Launch </label>
      <input file>$ICONDIR/execute18.png</input>
      <variable>HXLAUNCH_BUTTON</variable>
      <action>. $WORKDIR/func -history_launch &</action>
      <action type=\"exit\">quit_now</action>
     </button>
     <button use-underline=\"true\" width-request=\"100\" tooltip-text=\" Edit launch history in default text editor \">
      <label>_Edit </label>
      <input file stock=\"gtk-edit\"></input>
      <action>defaulttexteditor /$WORKDIR/PR-history &</action>
      <action type=\"exit\">quit_now</action>
     </button>
     <button use-underline=\"true\" width-request=\"100\">
      <label>_Close </label>
      <input file stock=\"gtk-close\"></input>
      <action type=\"exit\">quit_now</action>
     </button>
    </hbox>
    </vbox>
  </vbox>
</window>"
$GTKDIALOG --center -p PRUN_HISTORY
unset PRUN_HISTORY' >> /tmp/PR-history
chmod 777 /tmp/PR-history

#pRun dlg
export PRUN_DIALOG="
<window title=\"pRun\" icon-name=\"prun48\" resizable=\"false\">
  <vbox>
    <hbox>
    <vbox>
     <tree rules-hint=\"false\" enable-search=\"false\" headers-visible=\"false\">
      <height>97</height><width>300</width>
      <variable>selectedAPP</variable>
      <input>cat /tmp/PR-apps</input>
      <action signal=\"button-release-event\">enable:PRLAUNCH_BUTTON</action>
      <action signal=\"button-release-event\">echo \$selectedAPP > /tmp/PR-selectedAPP</action>
      <action signal=\"key-release-event\">enable:PRLAUNCH_BUTTON</action>
      <action signal=\"key-release-event\">echo \$selectedAPP > /tmp/PR-selectedAPP</action>
     </tree>
    </vbox>
    </hbox>

    <hbox>
     <button relief=\"2\" height-request=\"30\" width-request=\"30\" tooltip-text=\" Open default terminal \">
      <input file>$ICONDIR/console20.png</input>
      <action>$TERM &</action>
     </button>
     <entry activates-default=\"true\" is-focus=\"true\" tooltip-text=\" Search by entering (part of) application name. 
 Press the 'Enter key' to launch first item in list. \" secondary-icon-stock=\"gtk-clear\" secondary-icon-tooltip-text=\" Clear current search \">
      <variable>entryPRUNAPP</variable>
      <action signal=\"activate\">. $WORKDIR/func -entry_launch &</action>
      <action signal=\"key-release-event\">disable:PRLAUNCH_BUTTON</action>
      <action signal=\"key-release-event\">. $WORKDIR/func -prun_search</action>
      <action signal=\"key-release-event\">refresh:selectedAPP</action>
      <action signal=\"key-release-event\">echo \$entryPRUNAPP > /tmp/PR-selectedAPP</action>
      <action signal=\"secondary-icon-release\">disable:PRLAUNCH_BUTTON</action>
      <action signal=\"secondary-icon-release\">clear:entryPRUNAPP</action>
      <action signal=\"secondary-icon-release\">rm -f /tmp/PR-selectedAPP</action>
      <action signal=\"secondary-icon-release\">. $WORKDIR/func -prun_search</action>
      <action signal=\"secondary-icon-release\">refresh:selectedAPP</action>
     </entry>
     <togglebutton relief=\"2\" height-request=\"30\" width-request=\"30\" tooltip-text=\" Toggle the search mode 
 (standard or enhanced) \">
      <variable>SEARCHMODE</variable>
      <input>cat $WORKDIR/preferences/search_all</input>
      <input file>$TMPDIR/search.png</input>
      <action>if true ln -sf $TMPDIR/enhancedsearch.png $TMPDIR/search.png && echo true > $WORKDIR/preferences/search_all</action>
      <action>if false ln -sf $TMPDIR/standardsearch.png $TMPDIR/search.png && echo false > $WORKDIR/preferences/search_all</action>
      <action>refresh:SEARCHMODE</action>
      <action>disable:PRLAUNCH_BUTTON</action>
      <action>clear:entryPRUNAPP</action>
      <action>rm -f /tmp/PR-selectedAPP</action>
      <action>. $WORKDIR/func -prun_search</action>
      <action>refresh:selectedAPP</action>
     </togglebutton>
    </hbox>
     <hseparator></hseparator>
    <vbox width-request=\"100\">
    <hbox homogeneous=\"true\">
     <button width-request=\"55\" use-underline=\"true\" tooltip-text=\" Launch selected item \">
      <sensitive>false</sensitive>
      <input file>$ICONDIR/execute18.png</input>
      <variable>PRLAUNCH_BUTTON</variable>
      <action>. $WORKDIR/func -button_launch &</action>
     </button>
     <button width-request=\"55\" use-underline=\"true\" tooltip-text=\" Launch history \">
      <input file>$ICONDIR/history20.png</input>
      <action>/tmp/PR-history &</action>
     </button>
     <checkbox use-underline=\"true\" tooltip-text=\" Run in terminal – $TERM \">
      <label>_Term</label>
      <variable>TERMINAL_LAUNCH</variable>
      <input>cat $WORKDIR/preferences/terminal_launch</input>
      <action>if true echo true > $WORKDIR/preferences/terminal_launch</action>
      <action>if false echo false > $WORKDIR/preferences/terminal_launch</action>
     </checkbox>
     <button width-request=\"55\" use-underline=\"true\" tooltip-text=\" Preferences \">
      <input file>$ICONDIR/preferences20.png</input>
      <action>. $WORKDIR/pref &</action>
      <action type=\"exit\">quit_now</action>
     </button>
     <button width-request=\"55\" use-underline=\"true\" tooltip-text=\" Close pRun \">
      <input file>$ICONDIR/exit20.png</input>
      <action type=\"exit\">quit_now</action>
     </button>
  </hbox>
  </vbox>
  </vbox>
</window>"

$GTKDIALOG --center -p PRUN_DIALOG > /dev/null 2>&1 </dev/null
rm -f /tmp/PR-*
unset PRUN_DIALOG
LANG=$DEF_LANG

exit 0
