#!/mod/bin/jimsh

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

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

cgi_input
#cgi_dump

set dir [cgi_get dir "/media/My Video"]
set doit [cgi_get doit 0]

# Strip double slashes

header

puts "<fieldset style=\"display: inline\">
<legend>De-duplicate <span id=dir>$dir</span></legend>
<table class=borders>
<tr>
	<th>File</th>
	<th>Proposed Filename</th>
	<th>Proposed Title</th>
	<th>Status</th>
</tr>
"

regsub -all -- {\/+} "$dir/*" "/" sdir

set seen {}

foreach file [lsort [glob -nocomplain "$sdir"]] {
	if {[file extension $file] ne ".hmt"} { continue }
	altrow
	set ts [ts fetch $file 1]
	set base [file tail [file rootname $file]]
	puts "<td nowrap>$base</td>"
	set syn [$ts get synopsis]
	regsub -nocase -all -- {^new series\.* *} $syn "" syn
	regsub -nocase -all -- {^cbeebies\. *} $syn "" syn
	regsub -all -- { *[:].*$} $syn "" syn
	if {[string length $syn] > 40} {
		lassign [split $syn "."] v w
		set syn $v
		if {[string length $syn] < 6 && [string length $w] < 6} {
			append syn "_$w"
		}
	}
	if {[string length $syn] < 10} {
		if {[string match "${syn}*" [$ts get title]]} {
			set syn [$ts get title]
		} else {
			append syn " [$ts get title]"
		}
	}
	regsub -all -- {[\/ &]} $syn "_" fn
	puts "<td>$fn</td>"
	puts "<td>$syn</td>"
	puts "<td>"
	if {$fn in $seen} {
		puts -nonewline "Duplicate"
		if {$doit} {
			set dupdir "$dir/_duplicates"
			if {![file exists $dupdir]} { file mkdir $dupdir }
			while {[file exists $dupdir/$fn.hmt]} {
				append fn "~"
			}
			ts renamegroup $file "_duplicates/$fn"
			puts -nonewline " - Renamed to _duplicates/$fn"
		}
	} elseif {[string length $syn] > 40} {
		puts -nonewline "Cannot process"
	} elseif {$base eq $fn} {
		puts -nonewline "Nothing to do"
	} elseif {[file exists "$dir/$fn.ts"]} {
		puts -nonewline "Preserving"
	} elseif {$doit} {
		# Dooooo, it.
		$ts settitle $syn
		ts renamegroup $file $fn
		puts -nonewline "Done"
	}
	lappend seen $fn
	
	puts "</td>"
	puts "</tr>"
}

puts {
</table>
</fieldset>
<div style="padding-top: 2em">
<small>
<button id=browse>Back to media browser</button>
}

if {!$doit} { puts "<button id=dedup>Process folder</button>" }

puts {
</small>

</div>

<script type=text/javascript>
$('#browse').button().click(function() {
	window.location = '/cgi-bin/browse.jim?dir=' +
	    encodeURIComponent($('#dir').text());
});
}

if {!$doit} {
	puts {
	$('#dedup').button().click(function() {
		window.location = window.location + '&doit=1';
	});
	}
}

puts "</script>"

footer

