#!/bin/sh
#-
_VER='$VER: FKN-kde_dialog_timeout_shutdown.sh 0.1.1.2 (20110923) (20110920) FKN-Systems.de/Ng $'
_DESC='$DESC: An user breakable shutdown in time. eg: root call this at remote (ssh) machines to shutdown and the remote user can break this. $'
#-
_TODOS="
	20110921: FKN: CHG: make requester modal at the front of screen?
"
# TODOS_END
#-
# some docs about kdialog see: http://techbase.kde.org/Development/Tutorials/Shell_Scripting_with_KDE_Dialogs
#-
# VARS
#-
# set variables and commands
timeout='30' # in secs
timeout_passive='10' # in secs. an eyecatcher, when called by remote root it will open an unbreakable centered message window without any user action, so these time should shorter
title='Shutdown Sequence in Progress'
text1a='Um das Herunterfahren abzubrechen, müssen Sie innerhalb von' # + <time_var> + text1b
text1b='Sekunden den Vorgang abbrechen.'
testmode='NO' # YES|!yes
#testmode='YES' # YES|!yes
cmd_do='poweroff' # command here
cmd_do='kdialog --title "TESTWINDOW" --msgbox "Congratulation! When you see this is all good!"'
cmd_break='echo "Kommando Abgebrochen"'
#-
# MAIN
#-
# intern vars for do that
dothis='false'
inc=${timeout}-1
echo -e "\a\a$_VER\a"
# set 1==2 for use with kde versions before 4.3 (!) this function are NOT complete and only a demo, see below !!
if [  1 == 1 ] ; then
	dbusRef_p=$(kdialog --title "${title}" --passivepopup "${text1a} ${timeout} ${text1b}" ${timeout_passive} )
	dbusRef=$(kdialog --title "${title}" --progressbar "${text1a} ${timeout} ${text1b}" ${timeout} )
	# /dev/null need because echo something \n
	qdbus $dbusRef org.kde.kdialog.ProgressDialog.value ${timeout} > /dev/null
	qdbus $dbusRef org.kde.kdialog.ProgressDialog.showCancelButton true > /dev/null
	# hmm, wont work, why?: 
	#qdbus $dbusRef org.kde.kdialog.ProgressDialog.autoClose true
	# show all probs: 
	#qdbus $dbusRef
	until [ "true" == $(qdbus $dbusRef org.kde.kdialog.ProgressDialog.wasCancelled) ]; do
		sleep 1
		inc=$(($(qdbus $dbusRef org.kde.kdialog.ProgressDialog.value) - 1))
		qdbus $dbusRef org.kde.kdialog.ProgressDialog.value $inc > /dev/null
		qdbus $dbusRef org.kde.kdialog.ProgressDialog.setLabelText "${text1a} ${inc} ${text1b}" > /dev/null
		if [ "$inc" -lt "0" ] ; then
			dothis='true'
			break
		fi
	done
	qdbus $dbusRef org.kde.kdialog.ProgressDialog.close > /dev/null
	if [ "${dothis}" == "true" ] ; then
		if [ "${testmode}" == "YES" ] ; then
			echo "TEST: ${cmd_do}"
		else
			echo "DO: ${cmd_do}"
			${cmd_do}
		fi
		break
	else
		if [ "${testmode}" == "YES" ] ; then
			echo "TEST: ${cmd_break}"
		else
			echo "DO: ${cmd_break}"
			${cmd_break}
		fi
		break
	fi
else
	# for use with kde befor version 4.3
	# ! not complete only a demo !!!
	# !!! if yout complete this, please send a copy to author!!!
	dcopRef=$(kdialog --progressbar "Press Cancel at Any time" 10)
	dcop $dcopRef showCancelButton true
	until [ "true" = $(dcop $dcopRef wasCancelled) ]; do
		sleep 1
		inc=$(($(dcop $dcopRef progress) + 1))
		dcop $dcopRef setProgress $inc;
	done
	dcop $dcopRef close
fi
#-
# FIN
#-
