#!/usr/bin/wish
lower .
wm withdraw .
set decount_start 0

proc check_clock {} {
    global clock_ok
    scan [clock format [clock seconds] -format "%H"] "%d" num
    if {$num >= 17} {
	puts ok
	winMsg
    } else {
	puts wait
	after 60000 check_clock
    }
}

proc winMsg {} {
    global decount_start

    wm deiconify .
    lower .
    focus -force .
    bind . <KeyPress> {
	continue
    }
    toplevel .msg
    wm overrideredirect .msg 1
    set xmax [winfo screenwidth .]
    set ymax [winfo screenheight .]
    wm geometry .msg [format "%dx%d+0+0" $xmax $ymax]
    wm resizable .msg 0 0
    set bg "\#00f"
    set bg2 "\#aaf"

    .msg configure -background $bg -takefocus 1
    raise .
    bind .msg <ButtonPress-3> {exit 0}
    frame .msg.m -background $bg
    text .msg.m.text -background $bg -borderwidth 0 -highlightthickness 0 -foreground \#fff \
	    -font -*-courier-bold-*-*-*-24-*-*-*-*-*-* -height 4 -width 40
    frame .msg.m.fr -background $bg
    label .msg.m.decount -text "time left before automatic shutdown : 00:15:00" -background $bg -foreground \#fff
    button .msg.m.fr.b1 -text "back to work" -pady 5 -background $bg2 -highlightbackground $bg2 -width 20 -command continue
    button .msg.m.fr.b2 -text "Shutdown now" -pady 5 -background $bg2 -highlightbackground $bg2 -width 20 -command shutdown
    button .msg.m.fr.b3 -text "don't disturb me anymore" -pady 5 -background $bg2 -highlightbackground $bg2 -width 20 -command {exit 0}
#    frame .msg.empty
    pack .msg.m -expand yes
    pack .msg.m.decount -side bottom -anchor s -pady 50
    pack .msg.m.fr -side bottom -anchor n
    pack .msg.m.text -side bottom -anchor s
    pack .msg.m.fr.b1 .msg.m.fr.b2 .msg.m.fr.b3 -side top -pady 3
#    pack .msg.empty -side top -fill both -expand yes
    .msg.m.text tag configure inverted -foreground $bg -background \#fff
    set time [clock format [clock seconds] -format "%H:%M"]
    .msg.m.text insert end "It's already $time !\n"
    .msg.m.text insert end "It's time to go home\n"
    .msg.m.text insert end "Are you sure you still want to work ?\n"
    .msg.m.text configure -state disabled
    set decount_start [clock seconds]
    counter
}

proc counter {} {
    global decount_start
    if {$decount_start == 0} {
	return
    }
    set time_left [expr 15*60 - ([clock seconds]-$decount_start)]
    set min [expr $time_left/60]
    set sec [expr $time_left%60]
    set string [format "time before automatic shutdown : 00:%02d:%02d" $min $sec]
    .msg.m.decount configure -text $string
    if {$time_left == 0} {
	set decount_start 0
	shutdown
    } else {
	after 200 counter
    }
}

proc continue {} {
    global decount_start

    set decount_start 0
    destroy .msg
    wm withdraw .
    after [expr 30*60*1000] winMsg
}

proc shutdown {} {
	set os [string range $tcl_platform(os) 0 2]
	if {$os == "Win"} {
		exec "c:/shutdown/shutdown.exe" /now
	} else {
		exec "/sbin/shutdown -h now"
	}
    exit 0
}

check_clock
