#!/mod/bin/jimsh

package require sqlite3
package require cgi
source /mod/var/mongoose/lib/setup
require ts.class

puts "Content-Type: text/html\r\n\r\n"

cgi_input
#cgi_dump

set rfile [cgi_get file]
set ts [ts fetch $rfile]
set dir [file dirname $rfile]
set len [$ts duration 1]
lassign [$ts dlnaloc] url

if {[cgi_get do] eq "it"} {
	set xstart [clock milliseconds]

	set base [file rootname $rfile]
	set origdir "$dir/_original"
	if {![file exists $origdir]} { file mkdir $origdir }

	set shname [file tail $base]
	puts "Processing $shname"

	exec wget -O "$rfile.decrypting" $url

	puts "Moving recording to $origdir"

	foreach f [glob -nocomplain "${base}.*"] {
		if {[file extension $f] eq ".decrypting"} { continue }
		set tail [file tail $f]
		puts "  $tail"
		file rename $f "${origdir}/$tail"
	}

	file rename "$rfile.decrypting" $rfile

	foreach ext {nts hmt thm} {
		set sidecar "$shname.$ext"
		if {[file exists "$origdir/$sidecar"]} {
			puts "Copying back sidecar $ext"
			file copy "$origdir/$sidecar" "$dir/$sidecar"
		}
	}

	if {[file exists "$dir/$shname.hmt"]} {
		exec /mod/bin/hmt -encrypted "$dir/$shname.hmt"
	}

	set xtime [expr [expr [clock milliseconds] - $xstart] / 1000.0]
	puts "Time taken: $xtime"

	exit
}

header

puts "
<link href=/css/jquery.progressbar.css rel=stylesheet type=text/css />
<script type=\"text/javascript\" src=\"/js/jquery.progressbar.js\"></script>

<table class=keyval cellpadding=5>
<tr><th>File:</th><td>$rfile</td></tr>
<tr><th>Length:</th><td>[clock format $len -format "%T"]</td></tr>
<tr><th>DLNA URL</th><td>$url</td></tr>
</table>
"

if {$url eq ""} {
	puts "This file has not been indexed by the media server.
	    Cannot decrypt."
	puts "Have you enabled <i>Content Sharing</i> in the Humax menus?"
	exit
}

puts {

<div style="margin-top: 10px"></div>
<div id=decryptdiv><button id=decryptit>Perform decryption</button></div>
<div id=progressdiv style="display: none">
Decrypting: <div id=progressbar></div>
}
puts "<button id=back
    dir=\"[cgi_quote_url $dir]\"
    rfile=\"[cgi_quote_url $rfile]\"
    style=\"display: none\">Back to media list</button>"
puts {
<div id=output class=pre style="margin-top: 10px"></div>
</div>

<script type=text/javascript>

var handle = 0;

function update()
{
	$.get('/cgi-bin/browse/decrypt_progress.jim?file='
	    + $('#back').attr('rfile'), function(data) {
		if (handle)
			$('#progressbar').reportprogress(data);
	});
}

$(document).ready(function() {

$('#progressbar').reportprogress(0);

$('#back').button().click(function() {
	window.location = '/cgi-bin/browse.jim?dir=' + $(this).attr('dir');
});

$('#decryptit').button().click(function() {
	$('#decryptdiv').hide('slow');
	$('#progressdiv').show('slow');
	handle = setInterval("update()", 1000);
	$('#output').load(document.URL + '&do=it', function() {
		clearInterval(handle);
		handle = 0;
		$('#back').show();
		$('#progressbar').reportprogress(100);
	});
});

});
</script>
</div>

}

