#!/bin/bash
#pRun 1.7.2
#Copyright 2025 GPL3
#Roger Grider (radky)

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

export TEXTDOMAIN=prun
export OUTPUT_CHARSET=UTF-8

#check for pRun already active
PRPID=$(busybox ps | grep -Ea -- 'gtkdialog|gtk2dialog' | grep -a -- 'PRUN_DIALOG' | awk '{print $1}')
if [ "$PRPID" ]; then
 sleep 1
 [ "$PRPID" ] && gtkdialog-splash -close never -timeout 3 -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'pRun is already active !') " && exit 0
fi

export WORKDIR="/usr/local/prun"
[ ! -d $WORKDIR/preferences ] && mkdir $WORKDIR/preferences

#define gtkdialog
for g in gtk2dialog gtkdialog4 gtkdialog; do [ "`which $g 2>/dev/null`" ] && { GTKDIALOG="$g"; break; } done
export GTKDIALOG

#title-bar icon
[ ! -f /usr/share/icons/hicolor/48x48/apps/prun48.png ] && \
ln -sf $WORKDIR/icons/prun48.png /usr/share/icons/hicolor/48x48/apps && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

#togglebutton parameters
export TMPDIR=/tmp/pRun
mkdir -p "$TMPDIR"
[ ! -f "$TMPDIR/search.png" ] && cp $WORKDIR/icons/enhancedsearch.png $WORKDIR/icons/standardsearch.png $TMPDIR/
searchENHANCED=$(cat $WORKDIR/preferences/search_all 2>/dev/null)
[ "$searchENHANCED" = "true" ] && ln -sf $TMPDIR/enhancedsearch.png $TMPDIR/search.png
[ "$searchENHANCED" = "false" ] && ln -sf $TMPDIR/standardsearch.png $TMPDIR/search.png

#gui width and height
TREE_WIDTH=$(cat $WORKDIR/preferences/tree_width 2>/dev/null)
[ ! "$TREE_WIDTH" ] && TREE_WIDTH="420" && echo "$TREE_WIDTH" > $WORKDIR/preferences/tree_width

TREE_HEIGHT=$(cat $WORKDIR/preferences/tree_height 2>/dev/null)
[ ! "$TREE_HEIGHT" ] && TREE_HEIGHT="120" && echo "$TREE_HEIGHT" > $WORKDIR/preferences/tree_height

CURRDPI=`grep -a "^Xft\\.dpi:" $HOME/.Xresources 2>/dev/null | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' '`
[ ! "$CURRDPI" ] && CURRDPI="110"
if [[ $CURRDPI -ge 130 ]]; then
 MINI_TREE_HEIGHT="32"
elif [[ $CURRDPI -ge 120 ]]; then
 MINI_TREE_HEIGHT="30"
elif [[ $CURRDPI -ge 110 ]]; then
 MINI_TREE_HEIGHT="28"
else
 MINI_TREE_HEIGHT="26"
fi

[ -n "$WAYLAND_DISPLAY" ] && MINI_TREE_HEIGHT="28"

MINI_GUI=$(cat $WORKDIR/preferences/mini_gui 2>/dev/null)
[ ! "$MINI_GUI" ] && MINI_GUI="true" && echo "$MINI_GUI" > $WORKDIR/preferences/mini_gui
if [ "$MINI_GUI" = "true" ]; then
 TREE_HEIGHT="$MINI_TREE_HEIGHT" HS="false"
 SEARCH_TOOLTIP=" $(gettext 'Search by entering (part of) application name') 
 $(gettext "and press 'Enter key' to launch selected item.") "
else
 TREE_HEIGHT="$TREE_HEIGHT" HS="true"
 SEARCH_TOOLTIP=" $(gettext 'Search by entering (part of) application name.') 
 $(gettext "Press the 'Enter key' to launch first item in list.") "
fi
export TREE_HEIGHT TREE_WIDTH MINI_GUI HS SEARCH_TOOLTIP

#define GTK theme settings
if [ "`ldd $(which gtkdialog) | grep -Fi 'gtk-3' 2>/dev/null`" ] && [ ! "`which gtk2dialog 2>/dev/null`" ]; then #gtk3
 [ -f $TMPDIR/gtkrc.css ] && rm -f $TMPDIR/gtkrc.css
 if [ -e "$HOME/.config/gtk-3.0/settings.ini" ] ; then
  THEME=$(grep 'gtk-theme-name' $HOME/.config/gtk-3.0/settings.ini | cut -d '=' -f2 | awk '{$1=$1};1')
  CSS=$(find /usr/share/themes/"$THEME"/gtk-3* -maxdepth 1 -name 'gtk.css' -print | head -n1)
 fi
 if [ "$THEME" ] && [ -e "$CSS" ] ; then
  DEFINE="@import url(\"$CSS\");"
  echo "$DEFINE" > $TMPDIR/gtkrc.css
 fi
 cat $WORKDIR/gtk.css >> $TMPDIR/gtkrc.css
else #gtk2
 echo 'style "user-font" {
 font_name="Sans 10" }
 widget_class "*" style "user-font"' > $WORKDIR/gtkrc_user-font
 export GTK2_RC_FILES=$HOME/.gtkrc-2.0:$WORKDIR/gtkrc_user-font
 FONTNAME=$(grep 'font_name' $HOME/.gtkrc-2.0 2>/dev/null | cut -d '=' -f2 | sed 's/\"//g' | awk '{$1=$1};1')
 [ -z "$FONTNAME" ] && FONTNAME="Sans 10" && sed -i "s/font_name=.*$/font_name=\"$FONTNAME\"/" $HOME/.gtkrc-2.0
fi

#post-launch status of main GUI
CLOSE_PRUN=$(cat $WORKDIR/preferences/close_prun 2>/dev/null)
[ ! "$CLOSE_PRUN" ] && CLOSE_PRUN="false"
export CLOSE_PRUN

#double-click launching
DOUBLE_CLICK=$(cat $WORKDIR/preferences/double_click 2>/dev/null)
[ ! "$DOUBLE_CLICK" ] && DOUBLE_CLICK="true"
export DOUBLE_CLICK
DCLK=""
LAUNCH_TOOLTIP=" $(gettext 'Launch selected item') "
if [ "$DOUBLE_CLICK" = "true" ]; then
 DCLK="<action>. $WORKDIR/func -basic_launch &</action>"
 LAUNCH_TOOLTIP=" $(gettext 'This option will launch the selected menu item 
 (or you can double-click the application name)') "
fi

#default terminal
TERM=$(cat $WORKDIR/preferences/terminal_emulator 2>/dev/null)
[ ! "$TERM" ] && TERM="urxvt"
export TERM

#post-launch status of terminal
CLOSE_TERMINAL=$(cat $WORKDIR/preferences/close_terminal 2>/dev/null)
[ ! "$CLOSE_TERMINAL" ] && CLOSE_TERMINAL="false"
export CLOSE_TERMINAL

#PATH applications (exclude stray items and parameter-laden executables that are better suited for a terminal)
searchPATH=$(echo "$PATH" | sed 's/:/ /g')
PRUN_ITEMS=$(ls $searchPATH | grep -aEv '^$|\[|:|.txt|^[0-9]|NOTUSED| +' | sort -fu)
echo "$PRUN_ITEMS" > /tmp/PR-apps
echo "$PRUN_ITEMS" > /tmp/PR-search

#launch-history dialog
echo '#!/bin/sh
WORKDIR="/usr/local/prun"

HISTORY_ITEMS=$(cat $WORKDIR/preferences/history_items 2>/dev/null)
[ ! "$HISTORY_ITEMS" ] && HISTORY_ITEMS="20"

WIN_HEIGHT=$(cat $WORKDIR/preferences/window_height 2>/dev/null)
[ ! "$WIN_HEIGHT" ] && WIN_HEIGHT="300"

WIN_WIDTH=$(cat $WORKDIR/preferences/window_width 2>/dev/null)
[ ! "$WIN_WIDTH" ] && WIN_WIDTH="300"

AUTO_CLOSE=$(cat $WORKDIR/preferences/auto_close 2>/dev/null)
if [ "$AUTO_CLOSE" = "true" -o "$AUTO_CLOSE" = "" ]; then #auto-close history GUI after launching application
  ACTION_SIGNAL1="<action signal=\"button-release-event\">EXIT:OK</action>"
  ACTION_SIGNAL2="<action signal=\"row-activated\">EXIT:OK</action>"
else
  ACTION_SIGNAL1=""
  ACTION_SIGNAL2=""
fi

TOOLTIPS=$(cat $WORKDIR/preferences/tooltips 2>/dev/null)
if [ "$TOOLTIPS" = "true" -o "$TOOLTIPS" = "" ]; then #enable optional launch-history tooltips
  export TOOLTIP1="tooltip-text=\" $(gettext "Left-click to launch") \""
  export TOOLTIP2="tooltip-text=\" $(gettext "Path to selected item") \""
  export TOOLTIP3="secondary-icon-tooltip-text=\" $(gettext "Clear entries and start new history") \""
  export TOOLTIP4="tooltip-text=\" $(gettext "Manage launch history") \""
  export TOOLTIP5="tooltip-text=\" $(gettext "Maximum items in launch history (10-50)") \""
  export TOOLTIP6="tooltip-text=\" $(gettext "Default GUI window-height (300-700 px)") \""
  export TOOLTIP7="tooltip-text=\" $(gettext "Default GUI window-width (300-700 px)") \""
  export TOOLTIP8="tooltip-text=\" $(gettext "Check box to auto-close, or uncheck 
 to remain open as desktop launcher.") \""
  export TOOLTIP9="tooltip-text=\" $(gettext "Enable pop-up help text") \""
  export TOOLTIP10="tooltip-text=\" $(gettext "Delete current launch history") \""
  export TOOLTIP11="tooltip-text=\" $(gettext "Edit launch history in default text editor") \""
else
  export TOOLTIP1="tooltip-text=\"\""
  export TOOLTIP2="tooltip-text=\"\""
  export TOOLTIP3="tooltip-text=\"\""
  export TOOLTIP4="tooltip-text=\"\""
  export TOOLTIP5="tooltip-text=\"\""
  export TOOLTIP6="tooltip-text=\"\""
  export TOOLTIP7="tooltip-text=\"\""
  export TOOLTIP8="tooltip-text=\"\""
  export TOOLTIP9="tooltip-text=\"\""
  export TOOLTIP10="tooltip-text=\"\""
  export TOOLTIP11="tooltip-text=\"\""
fi

[ "${LANG%_*}" = "en" ] && WIDTH="340" || WIDTH="440"
export HISTORY_OPTIONS="
<window title=\"pRun $(gettext 'History')\" icon-name=\"gtk-preferences\" resizable=\"false\" width-request=\"$WIDTH\" window-position=\"2\">
 <vbox>
  <frame $(gettext 'Options')>
   <hbox homogeneous=\"true\">
   <vbox>
    <text><label>$(gettext 'History')</label></text>
   <hbox homogeneous=\"true\">
     <spinbutton space-expand=\"false\" space-fill=\"false\" width-request=\"100\" range-min=\"10\" range-max=\"50\" range-step=\"1\" range-value=\"20\" $TOOLTIP5>
      <variable>HISTORYITEMS</variable>
      <input>cat $WORKDIR/preferences/history_items</input>
      <action>echo \$HISTORYITEMS > $WORKDIR/preferences/history_items</action>
     </spinbutton>
   </hbox>
   </vbox>
   <vbox>
    <text><label>$(gettext 'Height')</label></text>
   <hbox>
     <spinbutton space-expand=\"false\" space-fill=\"false\" width-request=\"100\" range-min=\"300\" range-max=\"700\" range-step=\"10\" range-value=\"300\" $TOOLTIP6>
      <variable>WIN_HEIGHT</variable>
      <input>cat $WORKDIR/preferences/window_height 2>/dev/null</input>
      <action>echo \$WIN_HEIGHT > $WORKDIR/preferences/window_height</action>
     </spinbutton>
   </hbox>
   </vbox>
   <vbox>
    <text><label>$(gettext 'Width')</label></text>
   <hbox>
     <spinbutton space-expand=\"false\" space-fill=\"false\" width-request=\"100\" range-min=\"300\" range-max=\"700\" range-step=\"10\" range-value=\"300\" $TOOLTIP7>
      <variable>WIN_WIDTH</variable>
      <input>cat $WORKDIR/preferences/window_width 2>/dev/null</input>
      <action>echo \$WIN_WIDTH > $WORKDIR/preferences/window_width</action>
     </spinbutton>
   </hbox>
   </vbox>
   </hbox>
     <text height-request=\"2\"><label>\"\"</label></text>
   <hbox space-expand=\"true\" space-fill=\"true\">
   <vbox><spinbutton visible=\"false\"></spinbutton></vbox>
   <vbox>
     <checkbox space-expand=\"true\" space-fill=\"true\" $TOOLTIP8>
      <label>$(gettext "Auto-close")</label>
      <variable>AUTO_CLOSE</variable>
      <input>cat $WORKDIR/preferences/auto_close 2>/dev/null</input>
      <action>if true echo true > $WORKDIR/preferences/auto_close</action>
      <action>if false echo false > $WORKDIR/preferences/auto_close</action>
     </checkbox>
   </vbox>
   <vbox>
     <checkbox space-expand=\"true\" space-fill=\"true\" $TOOLTIP9>
      <label>$(gettext "Tooltips")</label>
      <variable>TOOLTIPS</variable>
      <input>cat $WORKDIR/preferences/tooltips 2>/dev/null</input>
      <action>if true echo true > $WORKDIR/preferences/tooltips</action>
      <action>if false echo false > $WORKDIR/preferences/tooltips</action>
     </checkbox>
   </vbox>
   </hbox>
     <hseparator></hseparator>
   <hbox homogeneous=\"true\">
     <button space-expand=\"false\" space-fill=\"true\" height-request=\"32\" width-request=\"100\" $TOOLTIP10>
      <label>$(gettext 'Delete')</label>
      <input file stock=\"gtk-delete\"></input>
      <action>rm -f $WORKDIR/PR-history 2>/dev/null &</action>
      <action>rm -f /tmp/PR-path 2>/dev/null &</action>
      <action type=\"exit\">quit_now</action>
     </button>
     <button space-expand=\"false\" space-fill=\"true\" height-request=\"32\" width-request=\"140\" $TOOLTIP11>
      <label>$(gettext 'Edit')</label>
      <input file stock=\"gtk-edit\"></input>
      <action>defaulttexteditor /$WORKDIR/PR-history &</action>
      <action type=\"exit\">quit_now</action>
     </button>
     <button space-expand=\"false\" space-fill=\"true\" height-request=\"32\" width-request=\"140\">
      <label>$(gettext 'OK')</label>
      <input file stock=\"gtk-ok\"></input>
      <action>. $WORKDIR/func -launchhistory_update</action>
      <action>/tmp/PR-history &</action>
      <action type=\"exit\">quit_now</action>
     </button>
   </hbox>
  </frame>
 </vbox>
<variable>HISTORY_OPTIONS</variable>
<action signal=\"delete-event\">EXIT:abort</action>
</window>"
' > /tmp/PR-history
echo 'export PRUN_HISTORY="
<window title=\"pRun\" icon-name=\"prun48\" window-position=\"2\">
  <vbox>
   <text use-markup=\"true\"><label>\"<b><span size='"'large'"'>$(gettext "Launch History")</span></b>\"</label></text>
    <hbox space-expand=\"true\" space-fill=\"true\">
    <vbox>
     <tree hover-selection=\"true\" rules-hint=\"false\" stock-id=\"gtk-justify-fill\" headers-visible=\"false\" $TOOLTIP1>
      <height>$WIN_HEIGHT</height><width>$WIN_WIDTH</width>
      <variable>hxAPP</variable>
      <input>tac $WORKDIR/PR-history | head -$HISTORY_ITEMS</input>
      <action type=\"enable\">HXLAUNCH_BUTTON</action>
      <action signal=\"cursor-changed\">which \$hxAPP 2>/dev/null > /tmp/PR-path</action>
      <action signal=\"cursor-changed\">Refresh:ENTRY_PATH</action>
      <action signal=\"leave-notify-event\">clear:ENTRY_PATH</action>
      <action signal=\"leave-notify-event\">rm -f /tmp/PR-path 2>/dev/null</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\">Refresh:hxAPP</action>
      <action signal=\"button-release-event\">rm -f /tmp/PR-path 2>/dev/null</action>
      $ACTION_SIGNAL1
      <action signal=\"row-activated\">echo \$hxAPP > /tmp/PR-hxAPP</action>
      <action signal=\"row-activated\">echo \$hxAPP >> $WORKDIR/PR-history</action>
      <action signal=\"row-activated\">. $WORKDIR/func -history_launch</action>
      <action signal=\"row-activated\">Refresh:hxAPP</action>
      <action signal=\"row-activated\">rm -f /tmp/PR-path 2>/dev/null</action>
      $ACTION_SIGNAL2
     </tree>
     <hseparator></hseparator>
    <hbox>
     <entry height-request=\"32\" $TOOLTIP2 secondary-icon-stock=\"gtk-clear\" $TOOLTIP3>
      <variable>ENTRY_PATH</variable>
      <input>cat /tmp/PR-path 2>/dev/null</input>
      <action signal=\"secondary-icon-release\">. $WORKDIR/func -launchhistory_clear &</action>
      <action signal=\"secondary-icon-release\">EXIT:OK</action>
     </entry>
    </hbox>
    </vbox>
    </hbox>
    <hbox space-expand=\"false\" space-fill=\"false\" homogeneous=\"true\">
    <button space-expand=\"false\" space-fill=\"true\" height-request=\"32\" width-request=\"100\" $TOOLTIP4>
      <label>$(gettext 'Manage')</label>
      <input file stock=\"gtk-jump-to\"></input>
      <action>launch:HISTORY_OPTIONS</action>
     </button>
     <button space-expand=\"false\" space-fill=\"true\" height-request=\"32\" width-request=\"100\">
      <label>$(gettext 'Close')</label>
      <input file stock=\"gtk-close\"></input>
      <action type=\"exit\">quit_now</action>
     </button>
    </hbox>
  </vbox>
</window>"
$GTKDIALOG -p PRUN_HISTORY
unset PRUN_HISTORY' >> /tmp/PR-history
chmod 777 /tmp/PR-history

#main dialog
export PRUN_DIALOG="
<window title=\"pRun\" icon-name=\"prun48\" resizable=\"true\" window-position=\"1\">
  <vbox>
    <hbox space-expand=\"true\" space-fill=\"true\">
    <vbox space-expand=\"true\" space-fill=\"true\">
     <tree rules-hint=\"false\" enable-search=\"false\" headers-visible=\"false\">
      <height>$TREE_HEIGHT</height><width>$TREE_WIDTH</width>
      <variable>selectedAPP</variable>
      <input>cat /tmp/PR-apps</input>
      $DCLK
      <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 space-expand=\"false\" space-fill=\"true\">
     <button relief=\"2\" space-expand=\"false\" space-fill=\"false\" height-request=\"32\" width-request=\"32\" tooltip-text=\" $(gettext 'Default terminal') - $TERM \">
      <input file>$WORKDIR/icons/console20.png</input>
      <action>$TERM &</action>
     </button>
     <entry space-expand=\"true\" space-fill=\"true\" activates-default=\"true\" is-focus=\"true\" tooltip-text=\"$SEARCH_TOOLTIP\" secondary-icon-stock=\"gtk-clear\" secondary-icon-tooltip-text=\" $(gettext '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 space-expand=\"false\" space-fill=\"false\" relief=\"2\" height-request=\"30\" width-request=\"34\" tooltip-text=\" $(gettext '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>
    <hbox homogeneous=\"true\">
     <checkbox tooltip-text=\" $(gettext 'Run in terminal emulator') – $TERM \">
      <label>$(gettext 'Terminal')</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>
    </hbox>

    <hbox homogeneous=\"true\" space-expand=\"false\" space-fill=\"true\">
     <button width-request=\"55\" tooltip-text=\"$LAUNCH_TOOLTIP\">
      <sensitive>false</sensitive>
      <input file>$WORKDIR/icons/execute18.png</input>
      <variable>PRLAUNCH_BUTTON</variable>
      <action>. $WORKDIR/func -basic_launch &</action>
     </button>
     <button width-request=\"55\" tooltip-text=\" $(gettext 'Launch history') \">
      <input file>$WORKDIR/icons/history20.png</input>
      <action>/tmp/PR-history &</action>
     </button>
     <button width-request=\"55\" tooltip-text=\" $(gettext 'Preferences') \">
      <input file>$WORKDIR/icons/preferences20.png</input>
      <action>. $WORKDIR/pref &</action>
      <action type=\"exit\">quit_now</action>
     </button>
     <button width-request=\"55\" tooltip-text=\" $(gettext 'Close pRun') \">
      <input file>$WORKDIR/icons/exit20.png</input>
      <action type=\"exit\">quit_now</action>
     </button>
     <button space-expand=\"true\" space-fill=\"true\" visible=\"false\"><label>\" \"</label></button>
  </hbox>
  </vbox>
 <action signal=\"key-press-event\" condition=\"command_is_true([ \$KEY_SYM = Escape ] && echo true )\">exit:EXIT</action>
</window>"

$GTKDIALOG -p PRUN_DIALOG --styles=$TMPDIR/gtkrc.css > /dev/null 2>&1 </dev/null
rm -f /tmp/PR-*
unset PRUN_DIALOG

exit 0
