#!/mod/bin/jimsh

package require cgi
source /mod/webif/lib/setup
require epg.class system.class settings.class

set irinst [system pkginst ir]

httpheader

set service [cgi_get service 0]
set event [cgi_get event 0]
if {![cgi_exists bare]} header else noheader

set record [lindex [\
    epg dbfetch dump -service $service -event $event -sort ""] 0]
$record get_channel_info
set others [$record othertimes]

if {[[settings] chanchangenc]} {
	puts { <script type=text/javascript src=/js/chanchangenc.js></script> }
} else {
	jqplugin confirmAction
	puts { <script type=text/javascript src=/js/chanchange.js></script> }
}

$record process_sched
if {[$record get sched_type] < 1} {
	set conflicts [rsv checkconflict \
	    [$record get start] [$record get duration] \
	    [system tuners]]
	if {[llength $conflicts]} {
		puts {
<div class=warningbox style="width: auto; margin: 0.2em"><div>
This event conflicts with the following items in your scheduled recording list.
<ul>
		}
		foreach x $conflicts {
			puts "<li>$x</li>\n"
		}

		puts "</div></div>"
	}
}

puts "
	<div id=epginfo_extra></div>
	<table class=keyval>
	<tr>
		<th>Programme</th>
		<td>[$record get name]</td>
	</tr><tr>
		<th>Synopsis</th>
		<td>[$record get text]</td>
	</tr><tr>
		<th>Start</th>
		<td>[clock format [$record get start] -format {%a %b %e %R %Z}]
"

foreach o $others {
	if {[$o ended]} { continue }
	$o get_channel_info
	$o process_sched
	if {[$o get series_crid] eq ""} { set ro 1 } else { set ro 2 }
	puts -nonewline "<br><font class=also>&nbsp;&nbsp;&nbsp;&nbsp;"
	puts -nonewline "Also: "
	puts -nonewline "<a class=event href=# xs=[$o get service_id] "
	puts -nonewline "xe=[$o get event_id] "
	puts -nonewline "sch=[$o get sched_type] rec=$ro>"
	puts -nonewline "[clock format [$o get start] -format {%a %b %e %R %Z}]"
	puts -nonewline " on "
	puts -nonewline [$o channel_icon 16]
	puts -nonewline "[$o get channel_name] "
	puts "</a>"
	puts "</font>"
}

puts "
		</td>
	</tr><tr>
		<th>Duration</th>
		<td>[clock format [$record get duration] -format %T]
"
if {[$record showing]} {
	puts [progressbar [$record percent]]
}
set icon [$record type_icon]
if {$icon != ""} { set icon "<img class=genre src=$icon>" }
puts "
	</td>
	</tr><tr>
		<th>Type</th>
		<td>$icon [$record get content_type] ([$record get content_code])</td>
	</tr>
"

if {[$record get warning] != ""} {
	if {[$record get warning_mode]} {
		set gcol red
	} else {
		set gcol blue
	}
	puts "
	<tr>
		<th>Warning</th>
		<td><img class=va src=/img/Guidance_$gcol.png height=21>
		    [$record get warning]</td>
	</tr>
	"
}

if {[$record get event_crid] != ""} {
	puts "
	<tr>
		<th>CRID</th><td>
		<a href=/cgi-bin/epg/search.jim?crid=[cgi_quote_url [$record get event_crid]]>
			<img border=0 height=14
			    src=/images/421_1_00_CH_Title_2R_Arrow.png>
			[$record get channel_crid][$record get event_crid]
			</a>
		</td>
	</tr>
	"
}

if {[$record get series_crid] != ""} {
	puts "
	<tr>
		<th nowrap>Series CRID</th><td>
		<a href=/cgi-bin/epg/search.jim?scrid=[cgi_quote_url [$record get series_crid]]>
			<img border=0 height=14
			    src=/images/421_1_00_CH_Title_2R_Arrow.png>
			[$record get channel_crid][$record get series_crid]
			</a>
		</td>
	</tr>
	"
}

if {[$record get rec_crid] != ""} {
	set rec [$record recommended]
	if {$rec != ""} {
		puts "<tr><th>Recommends</th><td>
			<a href=/cgi-bin/epg/search.jim?crid=[cgi_quote_url [$rec get event_crid]]>
			<img border=0 height=14
			    src=/images/421_1_00_CH_Title_2R_Arrow.png>
			[$rec get name]
			([$rec get channel_crid][$rec get event_crid])
			</a>
		</td></tr>"
	}
}

puts "
	<tr>
		<td colspan=2 valign=top>
"
if {$irinst} {
	puts "<a class=chanchange chan=[$record get channel_num] href=#>"
}
puts [$record channel_icon 70]
if {$irinst} { puts "</a>" }

puts "
			[join [$record icon_set] "&nbsp;"]
		</td>
	</tr>
	</table>

	<a class=footnote href=/cgi-bin/epg/dump.jim?service=[$record get service_id]&event=[$record get event_id]&raw=1>
		<img border=0 src=/images/532_1_00_Find_Path_Arrow.png>
		Show raw EPG info
	</a>
"

epg cleanup
if {![cgi_exists bare]} footer

