#!/mod/bin/jimsh

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

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]

if {[cgi_get do] eq "it"} {

	set cropstart [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"

	if {[file exists "$origdir/$shname.ts"]} {
		puts "This recording already exists within _original"
		puts "Cannot continue."
		exit
	}

	puts "Moving recording to $origdir"
	foreach f [glob -nocomplain "${base}.*"] {
		set tail [file tail $f]
		puts "  $tail"
		file rename $f "$origdir/$tail"
	}

	puts [exec /mod/bin/nicesplice \
	    -in "$origdir/$shname" \
	    -out "$dir/$shname" \
	    -cutBookMarks]

	set newname "$shname-[clock seconds]"
	puts "Renaming file group to $newname"
	ts renamegroup "$dir/$shname.ts" $newname
	exec /mod/bin/hmt "+setfilename=$newname" "$dir/$newname.hmt"

	set croptime [expr [expr [clock milliseconds] - $cropstart] / 1000.0]
	puts "Time taken: $croptime"

	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>Size:</th><td>[pretty_size [$ts size]] ([$ts get definition])</td></tr>
<tr><th>Bookmarks:</th><td>[$ts get bookmarks] @ "

set flag 0
foreach b [$ts bookmarks] {
	if $flag { puts -nonewline ", " }
	incr flag
	puts -nonewline [clock format $b -format "%T"]
}

puts "</td></tr><tr><th>File</th><td>"

puts "<div style=\"position: relative; top: -10px\">"

proc div {type left right} {
	set width $($right - $left)
	puts "<div class=$type style=\"left: ${left}px; width: ${width}px\">
	    $type</div>"
}

set keeping 0
set last 0
set start -1
foreach b [$ts bookmarks] {
	if {$start < 0} {
		set start $b
		continue
	}
	set end $b

	incr keeping $($end - $start)

	set left $($start * 500 / $len)
	set right $($end * 500 / $len)

	div cut $last $($left - 1)
	div keep $left $($right - 1)

	set last $right

	set start -1
}

if {$start > 0} {
	incr keeping $($len - $start)
	set left $($start * 500 / $len)
	div cut $last $($left - 1)
	div keep $left 500
} else {
	div cut $last 500
}

set keepperc $($keeping * 100.0 / $len)
set esttime $(int($keeping * 0.025))
if {[$ts get definition] eq "HD"} { set esttime $($esttime * 4) }

puts "
</div>
</td></tr>
<tr><th>Keeping:</th><td>
[clock format $keeping -format "%T"] ([format "%.2f" $keepperc]%)
</td></tr>
<tr><th>Time:</th>
<td>Cropping will take around [clock format $esttime -format "%T"]</td></tr>
</table>
"

puts "
<div id=cropdiv>
<button id=cropit>Perform crop operation</button>
</div>
<div id=progressdiv style=\"display: none\">
Cropping: <div id=progressbar></div>
<button id=back
    dir=\"[cgi_quote_url $dir]\"
    rfile=\"[cgi_quote_url $rfile]\"
    perc=\"$keepperc\"
    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/crop_progress.jim'
	    + '?perc=' + $('#back').attr('perc')
	    + '&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');
});

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

});
</script>
</div>

}

