#!/tvbin/tivosh

source /tvlib/tcl/tv/mfslib.tcl

set serverid [lindex $argv 0]
set channelno [lindex $argv 1]
set callsign [lindex $argv 2]
set name [lindex $argv 3]
set lineupid [lindex $argv 4]

set db [dbopen]
RetryTransaction {
    set station [db $db create Station]
    set stationid [dbobj $station fsid]/[dbobj $station subobjid] 
    puts "The file id you will need to use add-channel is $stationid"
    dbobj $station set ServerId $serverid
    dbobj $station set CallSign $callsign
    dbobj $station set Name $name
}
#puts "created /Server/$serverid"

#Start Add-Channel.tcl

proc OpenObject {db objspec} {
     if {[string range $objspec 0 0] == "/"} {
        set obj [db $db open $objspec]
     } elseif { [regexp {([0-9]*)/(.*)} $objspec junk fsid subobjid] } {
        set obj [db $db openidconstruction $fsid $subobjid]
     } else {
        set obj [db $db openid $objspec]
     }
     return $obj
}

set db [dbopen]
RetryTransaction {
    set station [OpenObject $db $stationid]
    set stationname [dbobj $station get Name]
    puts "station $stationname"

    set lineup [OpenObject $db $lineupid]

    set channel [db $db createsub Channel $lineup]
    dbobj $channel set Number $channelno
    dbobj $channel set Station $station
    puts "channel $channelno"

    dbobj $lineup add Channel $channel
}

