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

export TEXTDOMAIN=pupshutdown
export OUTPUT_CHARSET=UTF-8

VERSION="3.0"
WORKDIR="/usr/local/PupShutdown"
APPS_DIR="$WORKDIR/defaultapps"

#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 custom font
echo "style \"customsize\"
{
  font_name=\"Sans 10\"
}
widget \"*\" style \"customsize\"
class \"*\" style \"customsize\"

style \"customframe\"
{
  font_name=\"Sans bold 8\"
}
widget \"*.GtkFrame.GtkLabel\" style \"customframe\"
class \"*.GtkFrame.GtkLabel\" style \"customframe\"" > "$WORKDIR/gtkrc_custom"

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

#define current default apps
BACKUP=$(cat $APPS_DIR/backup | tail -1)
TASKMANAGER=$(cat $APPS_DIR/taskmanager | tail -1 | sed 's/rxvt +sb -fn 9x15 -bg black -fg white -title htop -n htop -e htop/htop/')

#define new font attributes and new graphic
NEWFONTNAME=$(cat $HOME/.PupShutdown/pupshutdown.rc | grep -a NEWFONTNAME= | cut -d "=" -f 2 | sed 's/\"//g')
NEWFONTSTYLE=$(cat $HOME/.PupShutdown/pupshutdown.rc | grep -a NEWFONTSTYLE= | cut -d "=" -f 2 | sed 's/\"//g')
NEWFONTWEIGHT=$(cat $HOME/.PupShutdown/pupshutdown.rc | grep -a NEWFONTWEIGHT= | cut -d "=" -f 2 | sed 's/\"//g')
NEWGRAPHIC=$(cat $HOME/.PupShutdown/pupshutdown.rc | grep -a NEWGRAPHIC= | cut -d "=" -f 2 | sed 's/\"//g')
NEWLABEL=$(cat $HOME/.PupShutdown/pupshutdown.rc | grep -a NEWLABEL= | cut -d "=" -f 2 | sed 's/\"//g')

if [ -s /tmp/PSD-color.tmp ]; then
 NEWFONTCOLOR=$(cat /tmp/PSD-color.tmp)
else
 NEWFONTCOLOR=$(cat $HOME/.PupShutdown/pupshutdown.rc | grep -a NEWFONTCOLOR= | cut -d "=" -f 2 | sed 's/\"//g')
fi

if [ "$NEWLABEL" = "Hide_Label" ]; then
 NEWFONTSIZE="0"
else
 NEWFONTSIZE=$(cat $HOME/.PupShutdown/pupshutdown.rc | grep -a NEWFONTSIZE= | cut -d "=" -f 2  | sed 's/\"//g')
 sed -i "s/fill:.*;/fill:$NEWFONTCOLOR;/" $WORKDIR/logos/Shutdown.png
fi

NEWFONTNAME_SIZE="$NEWFONTNAME $NEWFONTSIZE"

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

#define optional SUSPEND tray button
if [ -f /etc/acpi/actions/suspend.sh ] || [ "`which acpitool`" ]; then
     SUSPEND="
     <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Suspend to RAM') \">
      <input file>$WORKDIR/icons/suspend-ram20.png</input>
      <action>$WORKDIR/func -suspend_standby &</action>
      <action type=\"exit\">quit_now</action>
     </button>"
else
     SUSPEND=""
fi

#define optional wmSWITCHER tray button
if [ "`which wmswitcher`" -o "`which changewm.sh`" -o "`which change-wm`" -o "`which wm`" ]; then
     wmSWITCHER="
     <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Select alternate window manager') \">
      <input file>$WORKDIR/icons/wmswitcher20.png</input>
      <action>$WORKDIR/func -wmswitch &</action>
     </button>"
else
     wmSWITCHER=""
fi

#define optional CONTROLPANEL tray button
if [ "`which PupControl`" -o "`which wizardwizard`" ]; then
[ "`which PupControl`" ] && CPT="tooltip-text=\" $(gettext 'Control Panel')" || CPT="tooltip-text=\" $(gettext 'Puppy Setup')"
     CONTROLPANEL="
     <button width-request=\"32\" height-request=\"32\" $CPT \">
      <input file>$WORKDIR/icons/control-panel_20.png</input>
      <action>$WORKDIR/func -controlpanel &</action>
     </button>"
else
     CONTROLPANEL=""
fi

#define optional TASKMGR tray button
if [ "$TASKMANAGER" ]; then
     TASKMGR="
     <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Manage system tasks with') $TASKMANAGER \">
      <input file>$WORKDIR/icons/task20.png</input>
      <action>$WORKDIR/defaultapps/taskmanager &</action>
     </button>"
else
     TASKMGR=""
fi

#define optional SYSTEMBUTTONS for tray
EXTRA_BUTTONS=`cat $WORKDIR/defaultapps/extrabuttons | tail -1`
if [ "$EXTRA_BUTTONS" = "$(gettext 'enable')" ]; then
     SYSTEMBUTTONS="
     <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Mount/Unmount drives') \">
      <input file>$WORKDIR/icons/mount20.png</input>
      <action>$WORKDIR/func -mount &</action>
     </button>

     <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Manage drive partitions') \">
      <input file>$WORKDIR/icons/partition20.png</input>
      <action>/usr/sbin/gparted_shell &</action>
     </button>

     <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Manage system events') \">
      <input file>$WORKDIR/icons/event20.png</input>
      <action>/usr/sbin/eventmanager &</action>
     </button>

     <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Manage boot configuration') \">
      <input file>$WORKDIR/icons/boot20bw.png</input>
      <action>/usr/sbin/bootmanager &</action>
     </button>"
else
     SYSTEMBUTTONS=""
fi

#define Exit dialog
if [ -f "/tmp/pupmenu-marker" ]; then
     EXIT_DIALOG="$(gettext 'Return to PupMenu')"
else
     EXIT_DIALOG="$(gettext 'Close PupShutdown')"
fi

#detect current language
CURRLANG=`echo $LANG | sed "s/\(..\).*/\1/"` 
case $CURRLANG in
en)	PUP_POWEROFF="$(gettext '_Power-Off')"
	PUP_REBOOT="$(gettext '_Reboot')"
	PUP_RESTART="$(gettext 'Restart _X')"
	PUP_RESTARTWM="$(gettext 'Restart _WM')"
	PUP_PROMPT="$(gettext '_Log-Out')"
	PUP_BACKUP="$(gettext 'Backup _Files')"
	PUP_LOCK="$(gettext 'Lock _Screen')"
	PUP_UPDATE="$(gettext 'Update _Menu')" ;;
*)	PUP_POWEROFF="$(gettext '_Power-Off')"
	PUP_REBOOT="$(gettext '_Reboot')"
	PUP_RESTART="$(gettext 'Restart _X')"
	PUP_RESTARTWM="$(gettext 'Restart _WM')"
	PUP_PROMPT="$(gettext '_Log-Out')"
	PUP_BACKUP="$(gettext 'Backup _Files')"
	PUP_LOCK="$(gettext 'Lock _Screen')"
	PUP_UPDATE="$(gettext 'Update _Menu')" ;;
esac

#Create Logo dlg...
echo '#!/bin/sh
if [ "`which gtkdialog4`" ]; then
     GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
     GTKDIALOG=gtkdialog3
else
     GTKDIALOG=gtkdialog
fi' > /tmp/PSD-createlogo
echo 'export CREATE_LOGO_DIALOG="<window title=\"'$(gettext 'Preferences')'\" icon-name=\"gtk-preferences\" resizable=\"false\" window-position=\"1\">
 <vbox>
  <frame '$(gettext 'Create a New Logo')'>
  <text use-markup=\"true\"><label>\"
'"$(gettext 'Any bitmap editor can be used to format a new logo graphic, but mtPaint is easy to use and is included with most Puppy derivatives. The Template.png image included with PSD (PupShutdown) has a 72-pixel vertical dimension and will easily accommodate standard 64-pixel graphics.

Note: All PSD graphics are in /usr/local/PupShutdown/logos/

<b>mtPaint Instructions:</b>

1 - File->Open -- select a 64-pixel graphic for the logo
2 - Selection->Select All -- places focus on the graphic
3 - Edit->Copy -- copies graphic to the mtPaint clipboard
4 - File->Open -- select the PupShutdown Template.png
5 - Edit->Paste to centre -- centers graphic in the template
6 - With your mouse -- right-click the pasted center graphic
7 - Selection->Select All -- places focus on the new logo
8 - File->Save As -- name the new logo.png and save')"'\"</label></text>
  </frame>
   <hbox homogeneous=\"true\">
     <button height-request=\"35\" tooltip-text=\" '$(gettext 'Return to main Preferences dialog')' \">
     <label>\" '$(gettext 'Exit')'   \"</label>
      <input file stock=\"gtk-close\"></input>
      <action>/usr/local/PupShutdown/PSD-pref &</action>
      <action type=\"exit\">quit_now</action>
     </button>
   </hbox>
 </vbox>
</window>"
$GTKDIALOG --program=CREATE_LOGO_DIALOG' >> /tmp/PSD-createlogo
chmod 777 /tmp/PSD-createlogo

#Main dlg...
export PUPSHUTDOWN="<window title=\"PSD\" icon-name=\"shutdown64\" resizable=\"false\" window-position=\"1\">
 <vbox margin=\"3\">
  <text use-markup=\"true\"><label>\"<span font='"$NEWFONTNAME_SIZE"' size='"xx-large"' weight='"$NEWFONTWEIGHT"' style='"$NEWFONTSTYLE"' color='"$NEWFONTCOLOR"'><b>"$NEWLABEL"</b></span>\"</label></text>

    <pixmap>
     <input file>$WORKDIR/logos/"$NEWGRAPHIC"</input>  
    </pixmap>

  <vbox>
   <hbox>
    <vbox>
     <pixmap><input file>$WORKDIR/icons/tab170.png</input></pixmap>
     <button image-position=\"2\" use-underline=\"true\" tooltip-text=\" $(gettext 'Turn off PC') \">
      <input file>$WORKDIR/icons/shutdown20.png</input>
      <label>\"$PUP_POWEROFF\"</label>
      <action>$WORKDIR/func -poweroff &</action>
      <action type=\"exit\">quit_now</action>
     </button>
    </vbox>
    <button space-expand=\"true\" space-fill=\"true\" visible=\"false\"><label>\" \"</label></button>
    <vbox>
     <pixmap><input file>$WORKDIR/icons/tab170.png</input></pixmap>
     <button image-position=\"2\" use-underline=\"true\" tooltip-text=\" $(gettext 'Exit to command prompt') \">
      <input file>$WORKDIR/icons/console20.png</input>
      <label>\"$PUP_PROMPT\"</label>
      <action>$WORKDIR/func -logout &</action>
      <action type=\"exit\">quit_now</action>
     </button>
    </vbox>
    <button space-expand=\"true\" space-fill=\"true\" visible=\"false\"><label>\" \"</label></button>
   </hbox>

   <hbox>
    <vbox>
     <pixmap><input file>$WORKDIR/icons/tab170.png</input></pixmap>
     <button image-position=\"2\" use-underline=\"true\" tooltip-text=\" $(gettext 'Restart PC') \">
      <input file>$WORKDIR/icons/reboot20.png</input>
      <label>\"$PUP_REBOOT\"</label>
      <action>$WORKDIR/func -reboot &</action>
      <action type=\"exit\">quit_now</action>
     </button>
    </vbox>
    <button space-expand=\"true\" space-fill=\"true\" visible=\"false\"><label>\" \"</label></button>
    <vbox>
     <pixmap><input file>$WORKDIR/icons/tab170.png</input></pixmap>
     <button image-position=\"2\" use-underline=\"true\" tooltip-text=\" $(gettext 'Backup files with') $BACKUP \">
      <input file>$WORKDIR/icons/backup20.png</input>
      <label>\"$PUP_BACKUP\"</label>
      <action>$WORKDIR/defaultapps/backup &</action>
     </button>
    </vbox>
    <button space-expand=\"true\" space-fill=\"true\" visible=\"false\"><label>\" \"</label></button>
   </hbox>

   <hbox>
    <vbox>
     <pixmap><input file>$WORKDIR/icons/tab170.png</input></pixmap>
     <button image-position=\"2\" use-underline=\"true\" tooltip-text=\" $(gettext 'Restart X-server') \">
      <input file>$WORKDIR/icons/x20.png</input>
      <label>\"$PUP_RESTART\"</label>
      <action>$WORKDIR/func -restartX &</action>
     </button>
    </vbox>
    <button space-expand=\"true\" space-fill=\"true\" visible=\"false\"><label>\" \"</label></button>
    <vbox>
     <pixmap><input file>$WORKDIR/icons/tab170.png</input></pixmap>
     <button image-position=\"2\" use-underline=\"true\" tooltip-text=\" $(gettext 'Lock screen with password') \">
      <input file>$WORKDIR/icons/lock20.png</input>
      <label>\"$PUP_LOCK\"</label>
      <action>[ ! -s $HOME/.xlockrc ] && rm -f $HOME/.xlockrc</action>
      <action>/usr/local/apps/Xlock/AppRun &</action>
     </button>
    </vbox>
    <button space-expand=\"true\" space-fill=\"true\" visible=\"false\"><label>\" \"</label></button>
   </hbox>

   <hbox>
    <vbox>
     <pixmap><input file>$WORKDIR/icons/tab170.png</input></pixmap>
     <button image-position=\"2\" use-underline=\"true\" tooltip-text=\" $(gettext 'Restart') $CURRWM \">
      <input file>$WORKDIR/icons/wm20.png</input>
      <label>\"$PUP_RESTARTWM\"</label>
      <action>$WORKDIR/func -restartWM &</action>
     </button>
    </vbox>
    <button space-expand=\"true\" space-fill=\"true\" visible=\"false\"><label>\" \"</label></button>
    <vbox>
     <pixmap><input file>$WORKDIR/icons/tab170.png</input></pixmap>
     <button image-position=\"2\" use-underline=\"true\" tooltip-text=\" $(gettext 'Refresh XDG menu') \">
      <input file>$WORKDIR/icons/menu20.png</input>
      <label>\"$PUP_UPDATE\"</label>
      <action>$WORKDIR/func -menu_update &</action>
     </button>
    </vbox>
    <button space-expand=\"true\" space-fill=\"true\" visible=\"false\"><label>\" \"</label></button>
   </hbox>
   <pixmap><input file>$WORKDIR/icons/tab170.png</input></pixmap>
  </vbox>

   <hseparator></hseparator>

   <hbox space-expand=\"false\" space-fill=\"true\" homogeneous=\"true\">
    $SUSPEND
    $wmSWITCHER
    $CONTROLPANEL
    $TASKMGR
    $SYSTEMBUTTONS

     <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Preferences') \">
      <input file>$WORKDIR/icons/preferences20.png</input>
      <action>$WORKDIR/PSD-pref &</action>
      <action type=\"exit\">quit_now</action>
     </button>

     <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $EXIT_DIALOG \">
      <input file>$WORKDIR/icons/exit20.png</input>
      <action>[ -f "/tmp/pupmenu-marker" ] && PupMenu && rm -f /tmp/pupmenu-marker &</action>
      <action type=\"exit\">quit_now</action>
     </button>
   </hbox>
  </vbox>
</window>"

$GTKDIALOG -p PUPSHUTDOWN
unset PUPSHUTDOWN

exit 0
