#!/tvbin/tivosh
#
# noreddot.sh
# Removes the 'press red' icon from Sky Digital channels
#
# 8 clear clear to quit
#
# v 1.0  05Jan03  16:53
puts "Starting noreddot.tcl..."

file delete "/tmp/blastquit"

setpri fifo 1
catch {EnableTransactionHoldoff true}


proc BuildRemoteControls {} {
global RemoteControls 
global ControlState
global evrc

set RemoteControls [list \
	[list $evrc(8) $evrc(clear) $evrc(clear) "Terminate"]  \
]

set numcontrols [llength $RemoteControls]

for {set i 0} {$i < $numcontrols} {incr i} {
	set ControlState($i) 0
}

}


proc CommandTrigger { command } {
eval $command
}


proc Terminate {} {
	file delete "/tmp/blastir"
	set fid [open "/tmp/blastquit" w]
	close $fid
	puts "Exiting norreddot.tcl"
	exit
}


proc ProcessRemote {event subevent} {
global EventData TriggerState evrc
global ControlState
global RemoteControls
binary scan $EventData I keypressed
# Only care about Keydown
if {$subevent != 0} {
	return
}

set commandtriggered 0
set whichcontrol 0

foreach Control $RemoteControls {
	set position $ControlState($whichcontrol)
	set expecting [lindex $Control $position]

# If we got what we expected for this control increment the state
# If we got the last key in a sequence, trigger the command
# If we did not get what we expected, reset this state to zero

	if {$keypressed == $expecting} {
		if {$position == 2} {
			set commandtriggered 1
			CommandTrigger [lindex $Control 3]
		} else {
			incr position
			incr ControlState($whichcontrol)
		}
	} elseif {$keypressed == [lindex $Control 0]}  {
		set ControlState($whichcontrol) 1
	} else {
		set ControlState($whichcontrol) 0
	} 
	incr whichcontrol
}

# If we hit a command reset all states to zero
# This prevents remote command sequences from "overlapping"

if {$commandtriggered} {
	foreach index [array names ControlState] {
		set ControlState($index) 0
	}
}

flush stdout

}


##########################################
global RemoteControls
global ControlState
global TriggerState 
global db

source $tcl_library/tv/sendkey.tcl

set TriggerState 0
BuildRemoteControls

event register $TmkEvent::EVT_REMOTEEVENT ProcessRemote

set mfsVerboseG 0
set ircode "0 50 24 0 152 51 25 25 25 25 25 49 25 49 49 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 49 49 25 25 25 25 25 25 25 25 49 25 25 600"

set tcl_library "/tvlib/tcl"

source $tcl_library/tv/mfslib.tcl
source $tcl_library/tv/dumpobj.tcl

#set dbPoolSize [expr 300 * 1024]
#set db [dbopen $dbPoolSize]
#puts "Starting main loop: db: $db"

while {1} {
   RetryTransaction {
	set recs [mfs scan "/Recording/Active" -start "3:" -count 1]
   }
	scan [lindex [lindex $recs 0] 1] "%d:%d:%d:%d" rectype recstartdate recstarttime recfsid
	if { $rectype == "3" } {
puts "Recording in progress"
		set waittime [expr (($recstartdate * 86400) + $recstarttime + 5) - [clock seconds]]
		if { $waittime < 0 } {
puts "Recording already in progress, creating /tmp/blastir now..."
			set bif [open "/tmp/blastir" w]
			close $bif
			set waittime 60
		}
	} else {
		if { $rectype == "4" } {
			set waittime [expr (($recstartdate * 86400) + $recstarttime + 10) - [clock seconds]]
			if { $waittime > 60 } {
				set waittime 60
			}
		} else {
			set waittime 60
		}
	}
if {$waittime < 10 } {
	set waittime 10
}
puts "Waiting $waittime\s\ until [clock format [expr [clock seconds] + $waittime] -format "%H:%M:%S"]"
	after [expr $waittime * 1000]
	update

	if { $rectype == 3 } {
puts "creating /tmp/blastir now..."
		set bif [open "/tmp/blastir" w]
		close $bif
	} else {
puts "deleting /tmp/blastir now..."
		file delete "/tmp/blastir"
	}
}

       