#!/bin/bash
#PupShutdown 3.0
#Copyright 2017 GPL3
#Roger Grider (radky)
#20130204 internationalized (vicmz)

export TEXTDOMAIN=pupshutdown
export OUTPUT_CHARSET=UTF-8

WORKDIR="/usr/local/PupShutdown"

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

#define current window manager
if [ -f "$HOME/Choices/windowmanager" ]; then
  CURRWM=`cat "$HOME/Choices/windowmanager"` #TazOC LHP
else
  CURRWM=`cat /etc/windowmanager`
fi
. /etc/DISTRO_SPECS

#define current default apps
BACKUP=`cat $WORKDIR/defaultapps/backup | tail -1`
[ ! "$BACKUP" ] && BACKUP="none"
TASK_MANAGER=`cat $WORKDIR/defaultapps/taskmanager | tail -1 | sed 's/rxvt +sb -fn 9x15 -bg black -fg white -title htop -n htop -e htop/htop/'`
[ ! "$TASK_MANAGER" ] && TASK_MANAGER="none"
EXTRA_BUTTONS=`cat $WORKDIR/defaultapps/extrabuttons | tail -1`
[ ! "$EXTRA_BUTTONS" ] && EXTRA_BUTTONS="$(gettext 'disable')"

#define $BACKUP_ITEMS for file-backup combobox
BACKUP_ARRAY=("gadmin-rsync" "pbackup" "pmirror" "snap2")
BACKUP_ITEMS="<item>$BACKUP</item>"
for i in "${BACKUP_ARRAY[@]}"
do
   if [ "$i" != "$BACKUP" ]; then
      if [  "`which $i`" != "" ]; then
         BACKUP_ITEMS=`echo "$BACKUP_ITEMS<item>$i</item>"`
      fi
   fi
done

#define $TASK_MANAGER_ITEMS for task-manager combobox
TASK_MANAGER_ARRAY=("htop" "lxtask" "pprocess" "xfce4-taskmanager")
TASK_MANAGER_ITEMS="<item>$TASK_MANAGER</item>"
for i in "${TASK_MANAGER_ARRAY[@]}"
do
   if [ "$i" != "$TASK_MANAGER" ]; then
      if [  "`which $i`" != "" ]; then
         TASK_MANAGER_ITEMS=`echo "$TASK_MANAGER_ITEMS<item>$i</item>"`
      fi
   fi
done

#define hotkey compatibility
if [ "$CURRWM" = "startxfce4" -o "$CURRWM" = "xfce4-session" -o "$CURRWM" = "xfwm4" -o "$CURRWM" = "startkde" -o "`pidof enlightenment`" ]; then
	HOTKEY=""
else
	HOTKEY="
    <button height-request=\"32\" width-request=\"100\" tooltip-text=\" $(gettext 'Set Ctrl+Shift+X hotkey') \" use-underline=\"true\">
     <label>\"$(gettext '_H-key') \"</label>
      <input file>/usr/local/PupShutdown/icons/keyboard20.png</input>
      <action>/usr/local/PupShutdown/hkey_func -PSDhotkey &</action>
     </button>"
fi

#PSD Apps dlg...
export PSD_APPS="
<window title=\"$(gettext 'Configuration')\" icon-name=\"gtk-preferences\" resizable=\"false\">
<vbox>
  <text use-markup=\"true\"><label>\"<b>$(gettext 'Options')</b>\"</label></text>
  <notebook page=\"0\" labels=\"  $(gettext 'Label | Logo | Font Attributes | Save/Exit')  \">
  <vbox>
  <text><label>\"
  $(gettext 'Valid names should be selected for the label with no  
  blank, incomplete or misspelled items. One or more  
  appropriate names (descriptors and a base name),  
  and the version number, can be entered to further  
  personalize each of your Puppy Linux derivatives.')  
  \"</label></text>
  </vbox>

  <vbox>
  <text><label>\"
  $(gettext 'The default Shutdown.png graphic can be replaced  
  by optional 64-pixel graphics which are available in  
  the accessory Logo-addon.pet. You may also add  
  diversity to PupShutdown by placing any graphics  
  of proper size in /usr/local/PupShutdown/logos/.')  
  \"</label></text>
  </vbox>

  <vbox>
  <text><label>\"
  $(gettext 'Change title text attributes by selecting new values  
  for the font name, size, weight, style and color. The  
  font name list updates whenever the Preferences  
  menu is opened. The font color selector provides  
  access to unlimited colors in hexadecimal format.')  
  \"</label></text>
  </vbox>

  <vbox>
  <text><label>\"
  $(gettext 'The Save/Exit option closes the Preferences menu  
  and applies the new configuration selections to the  
  PSD interface. If an entry box is blank when closing  
  the Preferences menu, an alert will prompt you for  
  the information, then return you back to the menu.')  
  \"</label></text>
  </vbox>
</notebook>

  <vbox>
  <frame $(gettext 'Default Options')>
   <hbox>
    <text><label>$(gettext 'Backup Files:')</label></text>
    <comboboxentry wrap-width=\"1\">
     <variable>BACKUP</variable>
     <default>$BACKUP</default>
     $BACKUP_ITEMS
    </comboboxentry>
   </hbox>
   <hbox>
    <text><label>$(gettext 'Manage Task:')</label></text>
    <comboboxentry wrap-width=\"1\" tooltip-text=\" $(gettext 'Process/Task Manager') \">
     <variable>TASK_MANAGER</variable>
     <default>$TASK_MANAGER</default>
     $TASK_MANAGER_ITEMS
    </comboboxentry>
   </hbox>
   <hbox>
    <text><label>$(gettext 'Extra buttons:')</label></text>
    <comboboxentry wrap-width=\"1\" tooltip-text=\" $(gettext 'Add system management buttons to tray') \">
     <variable>EXTRA_BUTTONS</variable>
     <default>$EXTRA_BUTTONS</default>
     <item>$(gettext 'enable')</item>
     <item>$(gettext 'disable')</item>
    </comboboxentry>
   </hbox>
  </frame>
  </vbox>

   <hbox homogeneous=\"true\">
     $HOTKEY
     <button height-request=\"32\" width-request=\"100\" tooltip-text=\" $(gettext 'Save default options') \" use-underline=\"true\">
      <label>$(gettext '_Save') </label>
       <input file stock=\"gtk-ok\"></input>
        <action>$WORKDIR/func -defaultapps_set &</action>
        <action type=\"exit\">quit_now</action>
     </button>
     <button height-request=\"32\" width-request=\"100\" tooltip-text=\" $(gettext 'Exit without saving') \" use-underline=\"true\">
      <label>\" $(gettext '_Exit') \"</label>
       <input file stock=\"gtk-close\"></input>
        <action>$WORKDIR/func -defaultapps_nochange &</action>
        <action type=\"exit\">quit_now</action>
    </button>
   </hbox>
 </vbox>
</window>"

$GTKDIALOG --center --program=PSD_APPS > $WORKDIR/PSD-apps.rc

unset PSD_APPS

exit 0
