#!/usr/bin/wish -f

# GUI

wm title . "Online TV Player"

listbox .box -width 50 -yscrollcommand { .scroll set }
scrollbar .scroll -command { .box.list yview }
pack .box .scroll -side left -fill both

bind .box <Double-Button> play
bind . q exit

# Main function
proc play {} {
    global addresses
    set address [lindex $addresses [.box curselection]]
    puts "Starting video playback at $address..."
    exec mplayer $address 2> /dev/null
    puts "Video playback ended."
}

# Initialization
proc init {} {
    global addresses
    set initfile [open "channels"]
    while {![eof $initfile]} {
        set channel [split [gets $initfile] "="]
        .box insert end [string trimright [lindex $channel 0]]
        lappend addresses [string trimleft [lindex $channel 1]]
    }
    close $initfile
}

init