#!/home/i2c/bin/tclish  
i2c ivt
ivt adr 34
# for slow devices play with those:
# ivt udelay 10
# ivt mdelay 10 

# check for presence of ivt, video signal, ttx signal
if {1 == [catch {ivt re 1}] } {
  puts stdout "Could not find Videotext decoder!"
  puts stdout "   Please check your cables!"
  exit 1
}

ivt wr 0 1 

set a [ ivt re 1 ]

if { [expr $a & 1] == 0 } {
  puts stdout "Videotext: no TV signal!"
  puts stdout "  Please check your tuner & cables."
  exit 2
}

if { [expr $a & 2] == 0 } {
  puts stdout "Videotext: TV signal is okay, butno Tetetext signal!"
  puts stdout "  Please change station or try again."
  exit 2
}

ivt wr 0 0

# start search for anything in mem 0
ivt wr 2 0 8

# now retrieve clock data in first line ...
# and transform it into a format 'date' understands.
ivt wr 8 0 0 32
set systime [join [eval chr [ivt read 2]] {}]
ivt read 1
set systime "$systime[join [eval chr [ivt read 2]] {}]"
ivt read 1
set systime "$systime.[join [eval chr [ivt read 2]] {}]"

puts stdout "$systime"

# now retrieve current date from date
set MonthDay [exec /bin/date +%m%d]

#now execute the date command with the current time

exec /bin/date "$MonthDay$systime"

#and finally save system time into CMOS clock
exec /sbin/clock -w
