#!/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 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 new font attributes, new graphic, and new label
NEWFONTCOLOR=$(cat $HOME/.PupShutdown/pupshutdown.rc | grep -a NEWFONTCOLOR= | cut -d "=" -f 2 | sed 's/\"//g')
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 [ "$NEWLABEL" = "Hide_Label" ]; then
	NEWFONTSIZE="0"
else
	NEWFONTSIZE=$(cat $HOME/.PupShutdown/pupshutdown.rc | grep -a NEWFONTSIZE= | cut -d "=" -f 2  | sed 's/\"//g')
fi

case "$1" in

-current_fontcolor)
	[ ! -s /tmp/PSD-color.tmp ] && cat $HOME/.PupShutdown/pupshutdown-restore.rc | grep -a NEWFONTCOLOR= | cut -d "=" -f 2 | sed 's/\"//g' > /tmp/PSD-color.tmp
	;;

-current_fontname)
	echo "`fc-list : family | cut -d',' -f1 | sort -u`" | awk '{print "<item>"$1" "$2" "$3" "$4"</item>"}' | sed -e 's/ //g;s/\\//g' > /tmp/fontlist_1.tmp
	cat $HOME/.PupShutdown/pupshutdown-restore.rc | grep -a NEWFONTNAME= | cut -d "=" -f 2 | sed 's/\"//g' | awk '{print "<item>"$1"</item>"}' > /tmp/fontlist_2.tmp
	cat /tmp/fontlist_1.tmp | awk 'BEGIN{print ""}1' > /tmp/fontlist_3.tmp
	sed '1r /tmp/fontlist_2.tmp' < /tmp/fontlist_3.tmp | sed '1d' > /tmp/PSD-fontlist.txt
	rm -f /tmp/fontlist_*.tmp
	;;

-current_graphic)
	[ ! -f $WORKDIR/logos/Hide_Graphic ] && cp -f $WORKDIR/icons/Hide_Graphic $WORKDIR/logos/Hide_Graphic
	ls $WORKDIR/logos | awk '{print "<item>"$1"</item>"}' | sed 's/ //g' > /tmp/logolist_1.tmp
	cat $HOME/.PupShutdown/pupshutdown-restore.rc | grep -a NEWGRAPHIC= | cut -d "=" -f 2 | sed 's/\"//g' | awk '{print "<item>"$1"</item>"}' > /tmp/logolist_2.tmp
	cat /tmp/logolist_1.tmp | awk 'BEGIN{print ""}1' > /tmp/logolist_3.tmp
	sed '1r /tmp/logolist_2.tmp' < /tmp/logolist_3.tmp | sed '1d' > /tmp/PSD-logolist.txt
	rm -f /tmp/logolist_*.tmp
	;;

-controlpanel)
	if   [ "`which PupControl`" ]; then
	  if [ "`pidof PupControl-bin`" ]; then
	    if [ "`which gtkdialog-splash`" ]; then gtkdialog-splash -close never -timeout 2 -fontsize large -bg goldenrod -text " $(gettext 'PupControl is already active') "
	    elif [ "`which Xdialog`" ]; then Xdialog --title '' --beep --no-buttons --infobox "\n $(gettext 'PupControl is already active') \n" 0 0 2000
	    fi
	  else PupControl &
	  fi
	else wizardwizard &
	fi
	;;

-defaultapps_nochange)
	cp -f /tmp/PSD-apps.rc.tmp $WORKDIR/PSD-apps.rc
	if [ "`which gtkdialog-splash`" ]; then gtkdialog-splash -close never -timeout 2 -fontsize large -bg goldenrod -text " $(gettext 'Changes not saved...') "
	elif [ "`which Xdialog`" ]; then Xdialog --title '' --no-buttons --infobox "\n $(gettext 'Changes not saved...') \n" 0 0 2000
	fi
	;;

-defaultapps_set)
	sleep .3
	cp -f $WORKDIR/PSD-apps.rc $WORKDIR/PSD-apps2.rc
	cat $WORKDIR/PSD-apps.rc | grep -a BACKUP= | cut -d "=" -f 2 | sed 's/\"//g' | sed '1i\#!/bin/bash' > /tmp/APP1.tmp; chmod 755 /tmp/APP1.tmp; mv -f /tmp/APP1.tmp $WORKDIR/defaultapps/backup
	cat $WORKDIR/PSD-apps.rc | grep -a TASK_MANAGER= | cut -d "=" -f 2 | sed 's/\"//g' | sed '1i\#!/bin/bash' | sed 's/htop/rxvt +sb -fn 9x15 -bg black -fg white -title htop -n htop -e htop/' > /tmp/APP2.tmp; chmod 755 /tmp/APP2.tmp; mv -f /tmp/APP2.tmp $WORKDIR/defaultapps/taskmanager
	cat $WORKDIR/PSD-apps.rc | grep -a EXTRA_BUTTONS= | cut -d "=" -f 2 | sed 's/\"//g'> $WORKDIR/defaultapps/extrabuttons
	if [ "`which gtkdialog-splash`" ]; then gtkdialog-splash -close never -timeout 2 -fontsize large -bg goldenrod -text " $(gettext 'Saving default options...') "
	elif [ "`which Xdialog`" ]; then Xdialog --title '' --no-buttons --infobox "\n $(gettext 'Saving default options...') \n" 0 0 2000
	fi
	;;

-logout)
	if [ "`which gtkdialog-splash`" ]; then gtkdialog-splash -close never -timeout 2 -fontsize large -bg goldenrod -text " $(gettext 'Logging out to command prompt...') "
	elif [ "`which Xdialog`" ]; then Xdialog --title Log-out --no-buttons --infobox " $(gettext 'Logging out to command prompt...') " 0 0 2000
	fi
	if   [ "$DISTRO_NAME" = "Fatdog64" ]; then
		  wmexit terminal &
	elif [ "`pidof openbox`" ]; then
		  wmexit
		  killall openbox &
		  [ "`pidof lxsession`" ] && killall lxsession &
	elif [ "$CURRWM" = "startxfce4" -o "$CURRWM" = "xfce4-session" -o "$CURRWM" = "xfwm4" ]; then killall X &
	elif [ "$CURRWM" = "startfluxbox" -o "$CURRWM" = "fluxbox" -o "$CURRWM" = "fluxbox-session" ]; then kill -9 `pidof fluxbox` && killall X &
	elif [ "$CURRWM" = "enlightenment_start" ] && [ -f /usr/bin/wmexit ]; then
		  wmexit
		  killall enlightenment &
	elif [ "$CURRWM" != "enlightenment_start" ] && [ -f /usr/bin/wmexit ]; then wmexit &
	else killall X &
	fi
	;;

-menu_update) #update the Puppy XDG menu
	if [ "`pidof jwm`" ]; then
	 if [ "`jwm -h | grep '\-reload'`" ]; then
	  JWM_RESTART="jwm -reload"
	 else
	  JWM_RESTART="jwm -restart"
	 fi
	fi
	[ "`pidof openbox`" ] && openbox --reconfigure &
	if [ "$CURRWM" = "startlxde" -o "$CURRWM" = "startxfce4" -o "$CURRWM" = "xfce4-session" -o "$CURRWM" = "xfwm4" -o "$CURRWM" = "startkde" -o "`pidof enlightenment`" -o "`pidof fluxbox`" -o "$DISTRO_NAME" = "Fatdog64" ]; then
	 if [ "`which gtkdialog-splash`" ]; then sleep 1; gtkdialog-splash -close never -timeout 2 -fontsize large -bg goldenrod -text " $(gettext 'Updating the menu...') "
	 elif [ "`which Xdialog`" ]; then sleep 1; Xdialog --title " " --no-buttons --infobox "\n $(gettext 'Updating the menu...') \n" 0 0 2000
	 fi
	 [ "$CURRWM" = "startxfce4" -o "$CURRWM" = "xfce4-session" -o "$CURRWM" = "xfwm4" ] && xfdesktop --reload
	 [ "`pidof fluxbox`" ] && fluxbox_menu_refresh 2>/dev/null
	elif [ "`pidof jwm`" ] && [ ! "`grep -a '<!-- generated by' $HOME/.jwmrc`" = "" ]; then #jwm_tools active
	 sleep 1; Xdialog --title " " --no-buttons --infobox "\n      $(gettext 'Updating the menu...')     \n" 0 0 2000
	 [ "`which fixmenus`" ] && fixmenus 2>/dev/null
	 $JWM_RESTART &
	elif [ "`which fixmenus`" ]; then #standard Puppy
	 if [ "`which gtkdialog-splash`" ]; then sleep 1; gtkdialog-splash -close never -fontsize large -bg goldenrod -text " $(gettext 'Updating the menu...') " &
	 elif [ "`which Xdialog`" ]; then sleep 1; Xdialog --title " " --no-buttons --infobox "\n      $(gettext 'Updating the menu...')     \n" 0 0 30000 &
	 fi
	 XPID=$!
	 fixmenus 2>/dev/null
	 [ "`pidof jwm`" ] && $JWM_RESTART &
	 kill $XPID 2>/dev/null
	else
	 Xdialog --title "Alert" --msgbox " The fixmenus command is not available. If the current desktop \n environment has no option to refresh the menu, please restart X. " 0 0
	fi
	;;

-mount) if [ "`which pmount`" ]; then pmount & elif [ "`which ymount`" ]; then ymount & else Xdialog --title Alert --beep --msgbox " $(gettext 'Pmount and Ymount are not available.') " 0 0 &  fi ;;

-poweroff)
	if [ "`which gtkdialog-splash`" ]; then gtkdialog-splash -close never -timeout 2 -fontsize large -bg goldenrod -text " $(gettext 'Please wait, closing session...') "
	elif [ "`which Xdialog`" ]; then Xdialog --title Power-off --no-buttons --infobox " $(gettext 'Please wait, closing session...') " 0 0 2000
	fi
	if   [ "$DISTRO_NAME" = "Fatdog64" ]; then
		  wmexit poweroff &
	elif [ "`pidof openbox`" ]; then
		  wmpoweroff
		  killall openbox &
		  [ "`pidof lxsession`" ] && killall lxsession &
	elif [ "$CURRWM" = "enlightenment_start" ]; then
		  wmpoweroff
		  killall enlightenment &
	elif [ "$CURRWM" = "startxfce4" -o "$CURRWM" = "xfce4-session" -o "$CURRWM" = "xfwm4" ]; then wmpoweroff && killall X &
	elif [ "$CURRWM" = "startfluxbox" -o "$CURRWM" = "fluxbox" -o "$CURRWM" = "fluxbox-session" ]; then kill -9 `pidof fluxbox` && wmpoweroff &
	else wmpoweroff &
	fi
	;;

-preferences_new)
	sleep 0.5 ; [ "`pidof PSD-pref`" ] && sleep 1
	if   [ ! "$NEWFONTCOLOR" ]; then cp -f $HOME/.PupShutdown/pupshutdown-restore.rc $HOME/.PupShutdown/pupshutdown.rc && Xdialog --title Alert --no-cancel --beep --msgbox "$(gettext 'Please select a color!\n\nThe original entries will now repopulate the fields.\n Make any necessary corrections and save again.') " 0 0 && $WORKDIR/PSD-pref && exit
	elif [ ! "$NEWFONTNAME" ]; then cp -f $HOME/.PupShutdown/pupshutdown-restore.rc $HOME/.PupShutdown/pupshutdown.rc &&  Xdialog --title Alert --no-cancel --beep --msgbox " $(gettext 'Please confirm the font-name selection is correct!') " 0 0 && $WORKDIR/PSD-pref && exit
	elif [ ! "$NEWFONTSIZE" ]; then cp -f $HOME/.PupShutdown/pupshutdown-restore.rc $HOME/.PupShutdown/pupshutdown.rc && Xdialog --title Alert --no-cancel --beep --msgbox " $(gettext 'Please select a font size!') " 0 0 && $WORKDIR/PSD-pref && exit
	elif [ ! "$NEWFONTSTYLE" ]; then cp -f $HOME/.PupShutdown/pupshutdown-restore.rc $HOME/.PupShutdown/pupshutdown.rc && Xdialog --title Alert --no-cancel --beep --msgbox " $(gettext 'Please select a font style!') " 0 0 && $WORKDIR/PSD-pref && exit
	elif [ ! "$NEWFONTWEIGHT" ]; then cp -f $HOME/.PupShutdown/pupshutdown-restore.rc $HOME/.PupShutdown/pupshutdown.rc && Xdialog --title Alert --no-cancel --beep --msgbox " $(gettext 'Please select a font weight!') " 0 0 && $WORKDIR/PSD-pref && exit
	elif [ ! "$NEWGRAPHIC" ]; then cp -f $HOME/.PupShutdown/pupshutdown-restore.rc $HOME/.PupShutdown/pupshutdown.rc && Xdialog --title Alert --no-cancel --beep --msgbox " $(gettext 'Please confirm the logo-graphic selection is correct!') " 0 0 && $WORKDIR/PSD-pref && exit
	elif [ ! "$NEWLABEL" ]; then cp -f $HOME/.PupShutdown/pupshutdown-restore.rc $HOME/.PupShutdown/pupshutdown.rc && Xdialog --title Alert --no-cancel --beep --msgbox " $(gettext 'Please enter a name for your Puppy!') " 0 0 && $WORKDIR/PSD-pref && exit
	else $WORKDIR/PupShutdown &
	fi
	;;

-reboot)
	if [ "`which gtkdialog-splash`" ]; then gtkdialog-splash -close never -timeout 2 -fontsize large -bg goldenrod -text " $(gettext 'Please wait, rebooting...') "
	elif [ "`which Xdialog`" ]; then Xdialog --title Reboot --no-buttons --infobox " $(gettext 'Please wait, rebooting...') " 0 0 2000
	fi
	if   [ "$DISTRO_NAME" = "Fatdog64" ]; then
	      wmexit reboot &
	elif [ "$CURRWM" = "enlightenment_start" ]; then
		  wmreboot
		  killall enlightenment &
	elif [ "`pidof openbox`" ]; then
		  wmreboot
		  killall openbox &
		  [ "`pidof lxsession`" ] && killall lxsession &
	elif [ "$CURRWM" = "startxfce4" -o "$CURRWM" = "xfce4-session" -o "$CURRWM" = "xfwm4" ]; then wmreboot && killall X &
	elif [ "$CURRWM" = "startfluxbox" -o "$CURRWM" = "fluxbox" -o "$CURRWM" = "fluxbox-session" ]; then kill -9 `pidof fluxbox` && wmreboot &
	else wmreboot &
	fi
	;;

-restartWM)
	if   [ "$CURRWM" = "enlightenment_start" ]; then
		  restartwm
		  killall enlightenment &
	elif [ "`pidof openbox`" ]; then
		  restartwm
		  killall openbox &
		  [ "`pidof lxsession`" ] && killall lxsession &
	elif [ "$CURRWM" = "icewm" -o "$CURRWM" = "starticewm" -o "$CURRWM" = "icewm-session" ]; then killall -HUP icewm &
	elif [ "$CURRWM" = "jwm" -o "$CURRWM" = "jwm-session" -o "$CURRWM" = "startjwm" ]; then jwm -restart &
	elif [ "$CURRWM" = "startfluxbox" -o "$CURRWM" = "fluxbox" -o "$CURRWM" = "fluxbox-session" ]; then kill -1 $(xprop -root _BLACKBOX_PID | awk '{print $3}') &
	elif [ "$CURRWM" = "startxfce4" -o "$CURRWM" = "xfce4-session" -o "$CURRWM" = "xfwm4" ]; then restartwm && killall X &
	else restartwm &
	fi
	;;

-restartX)
	if   [ "$DISTRO_NAME" = "Fatdog64" ]; then
		  wmexit restart &
	elif [ "$CURRWM" = "enlightenment_start" ]; then
		  restartwm
		  killall enlightenment &
	elif [ "`pidof openbox`" ]; then
		  restartwm
		  killall openbox &
		  [ "`pidof lxsession`" ] && killall lxsession &
	elif [ "$CURRWM" = "startxfce4" -o "$CURRWM" = "xfce4-session" -o "$CURRWM" = "xfwm4" ]; then restartwm && killall X &
	elif [ "$CURRWM" = "startfluxbox" -o "$CURRWM" = "fluxbox" -o "$CURRWM" = "fluxbox-session" ]; then kill -9 `pidof fluxbox` && restartwm &
	elif [ -f /usr/bin/restartx ]; then /usr/bin/restartx & #jemimah script support
	else restartwm &
	fi
	;;

-restore)
	RESTORE=`cat $HOME/.PupShutdown/pupshutdown-restore.rc | grep -a NEWFONTCOLOR= | cut -d "=" -f 2  | sed 's/\"//g'`
	echo "$RESTORE" > /tmp/PSD-color.tmp
	cp -f $HOME/.PupShutdown/pupshutdown-restore.rc $HOME/.PupShutdown/pupshutdown.rc
	$WORKDIR/PupShutdown &
	;;

-sysinfo) if [ "`which hardinfo`" ]; then hardinfo & elif [ "`which Pup-SysInfo`" ]; then Pup-SysInfo & else Xdialog --title Alert --beep --msgbox " Hardinfo is not available. " 0 0 & fi ;;

-suspend_standby)
	if [ "`which gtkdialog-splash`" ]; then gtkdialog-splash -close never -timeout 2 -fontsize large -bg goldenrod -text " $(gettext 'Suspending...') "
	elif [ "`which Xdialog`" ]; then Xdialog --title " " --no-buttons --infobox "\n $(gettext 'Suspending...') \n" 0 0 2000
	fi
	if [ -f /etc/acpi/actions/suspend.sh ]; then /etc/acpi/actions/suspend.sh &
	elif [ "`which acpitool`" ]; then acpitool -s &
	fi
	;;

-wmswitch) if [ "`which wmswitcher`" ]; then wmswitcher & elif [ "`which changewm.sh`" ]; then changewm.sh & elif [ "`which change-wm`" ]; then change-wm & elif [ "`which wm`" ]; then wm & else Xdialog --title Alert --beep --msgbox " $(gettext 'PupShutdown finds no WM-Switcher.') " 0 0 & fi ;;

esac
