setctx quantum
bind pub - !wakeup wakeup
bind pub - !timezone wakeup


proc wakeup {nick host hand chan arg} {
	set minssince [clock format [clock seconds] -format %M -gmt 1]
	set currhour [clock format [clock seconds] -format %k -gmt 1]
	if {[string is digit $arg] == 1 && [catch {expr {$currhour - ($arg)}} output] == 0} {
		set currhour $output
		set minssince "If you wokeup about $arg hours ago"
	} else {
		set minssince "If you wokeup in the last $minssince minutes"
	}
	set currhour [expr {($currhour - 8)*(-1)}]
	set i 0
	while {$currhour > 12} {
		set currhour [expr {$currhour - 24}]
		incr i
		if {$i > 10} { return "buggy timezone" }
	}
	while {$currhour < -12} {
		set currhour [expr {$currhour + 24}]
		incr i
		if {$i > 10} { return "buggy timezone2" }
	}
	set timezone [timezone $currhour]
	if {$timezone == "0"} { putserv "privmsg $chan :Whoops seems i've not coded timezone $currhour yet"; return }
	set location [listrand [split [lindex $timezone 1] {,}]]

	putserv "PRIVMSG $chan :$minssince, you'll have woken up in $location ([lindex $timezone 0])."
}

proc timezone {offset} {
	switch -- $offset {

		-12 { return "{(GMT-12)} {middle of the Pacific ocean} {}"}
		-11 { return "{NT(GMT-11)} {Midway Island,Samoa} {}"}
		-10 { return "{HAST(GMT-10)} {Hawaii,Cook Islands,Tahiti} {}"}
		-9 { return "{AKST(GMT-9)} {Alaska} {}"}
		-8 { return "{PST(GMT-8)} {Western Mexico,West Coast America} {}"}
		-7 { return "{MST(GMT-7)} {Central Mexico} {}"}
		-6 { return "{CST(GMT-6)} {El Salvador,Costa Rica,Guatemala,Central America} {}"}
		-5 { return "{EST(GMT-5)} {Cuba,Bahamas,Panama,Colombia,Peru,Jamaica,East America} {}"}
		-4 { return "{AST(GMT-4)} {Chile,Puerto Rico,Barbados,Paraguay} {}"}
		-3 { return "{(GMT-3)} {Brazil,Uruguay,Argentina} {}"}
		-2 { return "{AZ(GMT-2)} {the middle of the Atlantic ocean} {}"}
		-1 { return "{WAT(GMT-1)} {Cape Verde,Azores,West Africa} {}"}
		0 { return "{GMT} {England,Scotland,Iceland,Portugal} {}"}
		1 { return "{CET(GMT+1)} {Netherlands,France,Germany,Belgium,Algeria,Italy,Finland} {}"}
		2 { return "{EET(GMT+2)} {Greece,Bulgaria,Egypt,Finland,Israel,Zambia,Zimbabwe} {}"}
		3 { return "{MSK(GMT+3)} {West Russia,Sudan,Kenya,Iraq,Saudi Arabia} {}"}
		4 { return "{GMT+4} {United Arab Emirates,Seychelles,Oman} {}"}
		5 { return "{GMT+5} {Uzbekistan,Maldives,Pakistan} {}"}
		6 { return "{GMT+6} {Bangladesh,Kazakstan,Bhutan} {}"}
		7 { return "{CXT(GMT+7)} {Christmas Island,Indonesia,Vietnam,Cambodia,Thailand } {}"}
		8 { return "{AWST(GMT+8)} {China,Taiwan,Philippines,Singapore,Malaysia} {}"}
		9 { return "{JapST(GMT+9)} {Japan,North Korea,South Korea} {}"}
		10 { return "{AEST(GMT+10)} {East Australia,Papua New Guinea} {}"}
		11 { return "{GMT+11} {Micronesia,Solomon Islands} {}"}
		12 { return "{NzST(GMT+12)} {New Zealand,Fiji} {}"}
		default { return 0 }
	}
}

proc listrand {list} {
	if {$list == ""} { return }
	return [lindex $list [rand [llength $list]]]
}

