#!/mod/bin/jimsh

package require cgi
package require sqlite3
source /mod/webif/lib/setup
require altrow progressbar epg.class system.class pkg.class

header

if {[catch {set db [sqlite3.open /var/lib/humaxtv/channel.db]} msg]} {
	puts "Error opening channel database: $msg"
	exit
}

pkg loadmuxdb

proc f2c {frequency} {
	set ch int($((($frequency / 1000) - 303.25) / 8))
	return [expr int($ch)]
}

puts "
	<div class=va>
		<img class=va src=/img/aerials.png height=50>
		&nbsp;
		Tuned Multiplex Information
	</div>
	<button id=expandall>Expand All</button>
	<button id=collapseall>Collapse All</button>
	<br><br>
	<table class=borders>
	<tr>
		<th>Channel</th>
		<th>Frequency</th>
		<th>Signal Strength</th>
		<th>Signal Quality</th>
		<th>Network</th>
		<th>Mux</th>
		<th>Type</th>
		<th>Channels</th>
		<th>&gt;799</th>
	</tr>
"
foreach tw [$db query {
		select tsIdx, szNetName, usTsID, ulFrequency, ucLevel,
		ucQuality, eTransMode, eConstellation, eSystem
		from TBL_TS join TBL_NET using (netIdx)
		order by ulFrequency
	}] {
	lassign $tw \
	    x tsIdx x netName x usTsID x ulFrequency x ucLevel x ucQuality \
	    x eTransMode x eConstellation x eSystem

	puts "
		<tr class=odd>
		<td>[f2c $ulFrequency]</td>
		<td>[expr $ulFrequency / 1000.0] MHz</td>
	"
	puts "<td>[progressbar $ucLevel]</td>"
	puts "<td>[progressbar $ucQuality]</td>"
	puts "<td>[system strip $netName]</td>"

	set channels [$db query {
		select usLcn, szSvcName, szPrvName, aucDefaultAuthority
		from TBL_SVC left join TBL_PRV using (prvIdx)
		where tsIdx = %s
		order by usLcn
		} $tsIdx]

	if {$eConstellation == 1} {
		set mux "Local"
	} else {
		set mux "Unknown"
	}
	set ehs 0
	foreach chan $channels {
		lassign $chan x lcn x name
		set name [system strip $name]
		if {$lcn >= 800} { incr ehs }
		if {[dict exists $::muxdb $name]} {
			set mux $::muxdb($name)
		}
	}
	puts "<td>$mux</td>"
	if {$eSystem == 0} {
		puts "<td>DVB-T (SD;$eTransMode)</td>"
	} else {
		puts "<td class=blood>DVB-T2 (HD;$eTransMode)</td>"
	}

	puts "<td>[llength $channels]
		<a class=mchan ts=$tsIdx href=#>
			<img border=0 height=14
			    src=/images/421_1_00_CH_Title_2R_Arrow.png>
			    view
		</a></td>"
	puts "<td>$ehs</td>"
	puts "</tr>"
	puts "<tr id=mchan_$tsIdx class=mchan style=\"display: none\">
		<td colspan=9>"
	puts "<table style=\"margin-left: 5em\">"
	puts "<tr>
		<th colspan=3>Channel</th>
		<th>Provider</th>
		<th>Authority</th>
	      </tr>"
	altrow reset
	foreach chan $channels {
		lassign $chan x lcn x name x prv x auth
		set name [system strip $name]
		set prv [system strip $prv]
		altrow
		puts "
			<td class=va>[epg channelicon $name 50]</td>
				<td>$lcn</td><td>$name</td>
				<td>$prv</td>
				<td>$auth</td>
			</tr>
		"
	}
	puts "</table>"
	puts "</td></tr>"
}

puts "</table>"

puts {
<script type=text/javascript>

$(document).ready(function() {
	$('a.mchan').click(function(e) {
		e.preventDefault();
		$('#mchan_' + $(this).attr('ts')).slideToggle('slow');
	});
	$('#expandall').button().click(function() {
		$('tr.mchan').slideDown('slow');
	});
	$('#collapseall').button().click(function() {
		$('tr.mchan').slideUp('slow');
	});
});

</script>
}

footer

