#!/mod/bin/jimsh

package require cgi
source /mod/webif/lib/setup
require pretty_size

puts "Content-Type: text/html"
puts ""

cgi_input
#cgi_dump

#set _cgi(file) "/media/My Video/Dangermouse/Series 1/01_rogue_robots.avi"

if {![dict exists $_cgi file]} { exit }

set file [dict get $_cgi file]
set type [dict get $_cgi type]

file stat $file st
set sz [pretty_size $st(size)]

if {$type eq "ts"} {
	require epg.class ts.class

	set ts [ts fetch $file]

	puts "
<table class=keyval>
<tr>
	<th>Title</th>
	<td>[$ts get title]</td>
</tr><tr>
	<th>Synopsis</th>
	<td>[$ts get synopsis]</td>
</tr><tr>
	"

	if {[$ts flag "Guidance"] > 0} {
		puts "
			<th>Guidance</th>
			<td><img class=va
			    src=/images/174_1_26_GuidancePolicy.png height=21>
			    [$ts get guidance]
			</td>
			</tr><tr>
		"
	}

	puts "
	<th>Definition</th>
	<td>
	"
	if {[$ts get definition] eq "HD"} {
		puts "<img class=va src=/images/172_1_00_HD.png height=21>"
	} else {
		puts "<img class=va src=/images/172_1_26_SD.png height=21>"
	}
	puts "</td>
</tr><tr>
	<th>Channel</th>
	<td>[epg channelicon [$ts get channel_name] 50 "vertical-align: middle"]
	    [$ts get channel_num] - [$ts get channel_name]</td>
</tr><tr>
	<th>Start Time</th>
	<td>"
	puts [clock format [$ts get start] -format "%c %Z"]
	puts "<font class=also>(Scheduled: [
	    clock format [$ts get schedstart] -format "%c %Z"])</font>"
	puts "</td>
</tr><tr>
	<th>End Time</th>
	<td>"
	puts [clock format [$ts get end] -format "%c %Z"]
	puts "<font class=also>(Scheduled: [clock format \
	    [expr [$ts get schedstart] + [$ts get scheddur]] \
	    -format "%c %Z"])</font>"
	puts "</td>
</tr><tr>
	<th>Duration</th>
	<td>[$ts duration] minute(s).
	    <font class=also>(Scheduled: [expr [$ts get scheddur] / 60])
	    </font></td>
</tr><tr>
	<th>Size</th>
	<td>$sz</td>
"
lassign [$ts dlnaloc] url
if {$url ne ""} {
	puts "</tr><tr><th>DLNA&nbsp;URL</th>
	   <td><a href=\"$url\">$url</a></td>"
}
puts "
</tr><tr>
	<th>Files</th>
	<td>
"

set fileset [$ts fileset]
puts -nonewline "<img id=xfileset class=va src=/img/tree/plusonly.png>"
puts -nonewline "[llength $fileset] file"
if {[llength $fileset] != 1} { puts -nonewline "s" }
puts -nonewline " in set."
puts "<div id=fileset class=hidden><ul>"
foreach f $fileset {
	puts "<li>[file tail $f] ([pretty_size [file size $f]])</li>" 
}
puts "</ul></div>"

puts "</td>
</tr><tr>
	<th>Flags</th>
	<td>[$ts get flags]
"
if {[$ts get bookmarks]} {
		puts " \[Bookmarks: [$ts get bookmarks]\]"
}

	puts "</tr>"
	puts "</table>"
	puts {
<script type=text/javascript>
$('#xfileset').click(function() {
	$('#fileset').slideToggle('slow', function() {
		if ($('#fileset').is(':visible'))
			$('#xfileset').attr('src', '/img/tree/minustop.png');
		else
			$('#xfileset').attr('src', '/img/tree/plusonly.png');
	});
});
</script>
	}
	exit
}

# Otherwise, for a general file.

puts "
<table class=keyval>
<tr>
	<th>File</th>
	<td>$file</td>
</tr><tr>
	<th>Size</th>
	<td>$sz</td>
</tr><tr>
	<th>Info</th>
	<td class=pre id=ffmpeg>
		<img src=/img/loading.gif><i>Loading...</i>
	</td>
</tr>
</table>
"

set url "/cgi-bin/browse/ffmpeg.jim?file=[cgi_quote_url $file]"
puts { <script type="text/javascript"> }
puts "var url = \"$url\";"
puts {
	$('#ffmpeg').load(url);
</script>
}

