#!/mod/bin/jimsh

package require sqlite3
package require cgi
source /mod/webif/lib/setup
require ts.class system.class

jqplugin progressbar
jscss decrypt.js
header

set mode "dlna"
if {[system model] eq "HD"} { set mode "direct" }

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

if {$mode eq "dlna"} {
	if {![system is_listening 9000]} {
		puts {
<div id=sysnotify class=warningbox style="width: 90%"><center>
The Humax media server is not running.<br>
Decryption will not use hardware acceleration.<br>
Have you enabled <i>Content Sharing</i> in the Humax menus?<br>
</center></div>
		}
		set mode "direct"
	}
}

if {$mode eq "dlna"} {
	set method "Hardware accelerated (via DLNA)"
} else {
	set method "Directly without hardware acceleration"
}

puts "
<fieldset class=cleft>
<legend>Decrypt in-place</legend>

<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>Decryption method:</th><td>$method</td></tr>
</table>

<span class=hidden id=params
    dir=\"[cgi_quote_url $dir]\"
    file=\"[cgi_quote_url $rfile]\"
    mode=\"$mode\"
></span>

<div style=\"padding: 1em\">
<span id=decryptdiv>
<button id=decryptit>Perform decryption</button>
</span>
<button id=back>Back to media list</button>
</div>

<div id=progressdiv class=hidden>
Decrypting: <div id=progressbar></div>
</div>

<div id=output class=\"hidden pre\" style=\"margin-top: 10px\"></div>
</fieldset>
"

