#!/bin/bash
#logout gui for jwm and labwc

export TEXTDOMAIN=logout_gui_labwc

export GTKDIALOG=gtkdialog

export DIALOG="gtkdialog-splash -close never -margin 4 -placement center -fontsize large -bg goldenrod"

if pidof jwm >/dev/null 2>&1; then
	ICONDIR="/usr/local/jwmdesk"
	RESTART_MENUAPP="$(gettext 'Restart JWM')"
	WM="`jwm -v | grep JWM | cut -d " " -f1,2`"
	WM_TOOLTIP=""
	DS_TOOLTIP="Restart X11 display server"
elif pidof labwc >/dev/null 2>&1; then
	ICONDIR="/usr/share/pixmaps/puppy"
	RESTART_MENUAPP="$(gettext 'Restart Labwc')"
	WM="`labwc -v`"
	WM_TOOLTIP="$(gettext 'Restart Labwc compositor and SFWbar panel')"
	DS_TOOLTIP="$(gettext 'Restart Wayland display server')"
fi
export ICONDIR RESTART_MENUAPP $WM WM_TOOLTIP DS_TOOLTIP

[ ! -f /usr/share/icons/hicolor/48x48/apps/shutdown.svg ] && \
ln -sf $ICONDIR/shutdown.svg /usr/share/icons/hicolor/48x48/apps && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

########################################################################
#                                                                      #
# HEADER CONFIGURATION                                                 #
#                                                                      #
########################################################################

XML_INFO_COLOR='#EDEBD7' # background color
XML_INFO_OPACITY=0.5 # background opacity
. /usr/lib/gtkdialog/xml_info gtk > /dev/null # build bg_pixmap for gtk-theme

. /etc/DISTRO_SPECS
BOX_HEIGHT=90
ICON=$ICONDIR/puppy_config.svg
ICON_HEIGHT=85
MSG_1="<b>$DISTRO_NAME $DISTRO_VERSION</b>"
MSG_2="$(gettext 'Window manager'): <b>$WM</b>"
MSG_3="$(gettext 'Linux'): <b>`uname -r` (`uname -m`)</b>"
MSG_4="$(gettext 'System uptime:') <b>`uptime | awk -F'( |,|:)+' '{if ($7=="min") m=$6; else {if ($7~/^day/) {d=$6;h=$8;m=$9} else {h=$6;m=$7}}} {print d+0"d",h+0"h",m+0"m"}'`</b>"
HEADER="
    <hbox height-request="'"${BOX_HEIGHT}"'">
    "$(. /usr/lib/gtkdialog/xml_info "$BOX_HEIGHT" "$ICON" "$ICON_HEIGHT" "$MSG_1" "$MSG_2" "$MSG_3" "$MSG_4")"
    </hbox>"

########################################################################
#                                                                      #
# GUI BUTTON PARAMETERS                                                #
#                                                                      #
########################################################################

[ -n "$WAYLAND_DISPLAY" ] && DISPLAY_SERVER=Wayland || DISPLAY_SERVER=X-server

# define Backup option
BTN7=true
[ "`which grsync 2>/dev/null`" ] && backup_app="grsync"
[ ! "$backup_app" ] && [ "`which snap2 2>/dev/null`" ] && backup_app="snap2"
[ ! "$backup_app" ] && [ "`which gadmin-rsync 2>/dev/null`" ] && backup_app="gadmin-rsync"
[ ! "$backup_app" ] && [ "`which pmirror 2>/dev/null`" ] && backup_app="pmirror"
[ ! "$backup_app" ] && [ "`which Pudd 2>/dev/null`" ] && backup_app="Pudd"
[ ! "$backup_app" ] && [ "`which pupsave-backup 2>/dev/null`" ] && backup_app="pupsave-backup"
[ ! "$backup_app" ] &&  BTN7=false backup_app="NA"
export backup_app

# define Suspend option for laptops/notebooks (button-8)
if [ ! -x /tmp/suspend2ram.sh ]; then
    if [ -f /etc/acpi/actions/suspend.sh ]; then # override restrictions imposed by suspend.sh when usb media mounted
        if [[ $(grep 'DISTRO_TARGETARCH' /etc/acpi/actions/suspend.sh 2>/dev/null) ]]; then
            cat /etc/acpi/actions/suspend.sh > /tmp/suspend2ram.sh
            sed -i '0,/DISTRO_TARGETARCH/s/x86//' /tmp/suspend2ram.sh
        else
            grep -Ev 'mount|^USB|^for|^do' /etc/acpi/actions/suspend.sh > /tmp/suspend2ram.sh
        fi
        chmod 755 /tmp/suspend2ram.sh
    else
        echo '#!/bin/sh
        echo -n mem > /sys/power/state' > /tmp/suspend2ram.sh
        chmod 755 /tmp/suspend2ram.sh
    fi
fi
export suspend2ram="/tmp/suspend2ram.sh"

func_suspend(){
    usbmounted=""
    USBS=$(probedisk2|grep '|usb' | cut -d'|' -f1 )
    for USB in $USBS
    do
        mount | grep -q "^$USB" && usbmounted="true"
    done
    [ "$usbmounted" = "true" ] && $DIALOG -timeout 4 -text " $(gettext 'Do not remove USB flash drive !')" /
    $DIALOG -timeout 2 -text " $(gettext 'Suspending to RAM...')" /
    ${suspend2ram} 2>/dev/null &
}
export -f func_suspend

SUSPEND='
      <button image-position="2" tooltip-text=" '$(gettext 'Suspend to RAM')' ">
        <label>'$(gettext 'Suspend')'</label>
        <input file>'$ICONDIR'/suspend.svg</input><height>42</height><width>42</width>
        <action>exit:suspend</action>
      </button>'

# define Screen-lock option (alternate button-8)
func_screenlock(){
    if [ -f /sbin/puplock -a -f /bin/swaylock ] && [ -n "$WAYLAND_DISPLAY" ]; then
        [ "`pidof swaylock`" ] && killall swaylock
        exec swaylock -F -f --indicator-idle-visible -i `cat ~/.config/wallpaper/bg_img` --scaling=stretch &
    else
        if [ ! -s $HOME/.xlockrc ]; then
            rm -f $HOME/.xlockrc
            /usr/local/apps/Xlock/AppRun &
        else
            sleep 2
            /usr/local/apps/Xlock/AppRun &
        fi
    fi
}
export -f func_screenlock

if [ "`which xlock 2>/dev/null`" ]; then
    LOCK_CMD="XLOCK"
elif [ -f /sbin/puplock -a -f /bin/swaylock ] && [ -n "$WAYLAND_DISPLAY" ]; then
    LOCK_CMD="SWAYLOCK"
else
    LOCK_CMD=""
fi

[ "$LOCK_CMD" = "XLOCK" -o "$LOCK_CMD" = "SWAYLOCK" ] && SCREENLOCK='
      <button image-position="2">
        <label>'$(gettext 'Lock Screen')'</label>
        <input file>'$ICONDIR'/screen_lock.svg</input><height>42</height><width>42</width>
        <action>exit:lockscreen</action>
      </button>'

BTN8="$SCREENLOCK"

########################################################################
#                                                                      #
# MAIN DIALOG                                                          #
#                                                                      #
########################################################################

NAME="Puppy"

S='
<window title="'$NAME' '$(gettext 'Log Out')'" icon-name="shutdown" window-position="1" resizable="false">
<vbox space-expand="true" space-fill="true">
  <vbox margin="2">
  '${HEADER}'
  </vbox>
  <vbox margin="2" spacing="10" space-expand="true" space-fill="true">

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2">
        <label>'$(gettext 'Reboot')'</label>
        <input file>'$ICONDIR'/reboot.svg</input><height>42</height><width>42</width>
        <action>exit:reboot</action>
      </button>
      <button image-position="2">
        <label>'$(gettext 'Shut Down')'</label>
        <input file>'$ICONDIR'/shutdown.svg</input><height>42</height><width>42</width>
        <action>exit:poweroff</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2" tooltip-text=" '${DS_TOOLTIP}' ">
        <label>'$(gettext 'Restart')' '$DISPLAY_SERVER'</label>
        <input file>'$ICONDIR'/graphics_restart.svg</input><height>42</height><width>42</width>
        <action>exit:restartx</action>
      </button>
      <button image-position="2">
        <label>'$(gettext 'Exit to Prompt')'</label>
        <input file>'$ICONDIR'/terminal.svg</input><height>42</height><width>42</width>
        <action>exit:logout</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2" tooltip-text=" '${WM_TOOLTIP}' ">
        <label>'${RESTART_MENUAPP}'</label>
        <input file>'$ICONDIR'/wm_restart.svg</input><height>42</height><width>42</width>
        <action>exit:restartwm</action>
      </button>
      <button image-position="2" tooltip-text=" '$(gettext 'Run fixmenus')' ">
        <label>'$(gettext 'Rebuild Menu')'</label>
        <input file>'$ICONDIR'/menu.svg</input><height>42</height><width>42</width>
        <action>exit:refreshwm</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button sensitive="'"$BTN7"'" image-position="2" tooltip-text=" '${backup_app}' ">
        <label>'$(gettext 'Backup')'</label>
        <input file>'$ICONDIR'/install_options.svg</input><height>42</height><width>42</width>
        <action>exit:backup</action>
      </button>
      '${BTN8}'
    </hbox>

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

export logout="$S"

retvals="`$GTKDIALOG -p logout --styles=/tmp/gtkrc_xml_info.css`"
eval "$retvals"
case $EXIT in
  reboot) $DIALOG -timeout 2 -text " $(gettext 'Rebooting...')" /
  exec wmreboot ;;

  poweroff) $DIALOG -timeout 2 -text " $(gettext 'Closing session...')" /
  exec wmpoweroff ;;

  restartx) $DIALOG -timeout 2 -text " $(gettext 'Restarting') $DISPLAY_SERVER..." /
  exec restartwm ;;

  logout) $DIALOG -timeout 2 -text " $(gettext 'Exiting to prompt...')" /
  exec wmexit ;;

  restartwm) $DIALOG -timeout 2 -text " $(gettext 'Restarting window manager...')" /
  pidof jwm >/dev/null 2>&1 && jwm -restart; pidof labwc >/dev/null 2>&1 && labwc -r; pidof sfwbar >/dev/null 2>&1 && killall -SIGHUP sfwbar & ;;

  refreshwm) $DIALOG -timeout 2 -text " $(gettext 'Rebuilding menu...')" /
  fixmenus; pidof jwm >/dev/null 2>&1 && jwm -reload; pidof labwc >/dev/null 2>&1 && labwc -r & ;;

  suspend) func_suspend ;;
 
  lockscreen) func_screenlock ;;

  backup) ${backup_app} & ;;
  *)exit ;;
esac
