#!/mod/bin/jimsh

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

source _lib.jim

set dload [cgi_get dload -]
set file [cgi_get file "-"]
if {$file eq "-"} { set file "/var/log/humaxtv.log" }
if {$file ni $loglist} { exit }

if {$dload ne "-"} {
	httpheader "application/force-download" 0 "Content-Disposition: attachment; filename=\"[file tail $file].txt\"\r\n"
} else {
	httpheader
}

proc quote {s} {
	return [string map {
		{&} {&amp;}
		"\"" {&quot;}
		{<} {&lt;}
		{>} {&gt;}
	    } $s]
}

set lines [cgi_get lines 0]

if {$lines} {
	set fp [popen [list tail -n $lines $file]]
} else {
	set fp [open $file r]
}
if {$dload ne "-"} {
	while {![$fp eof]} {
		$fp gets line
		if {[$fp eof]} break
		puts $line
	}
} else {
	set l 1
	while {![$fp eof]} {
		$fp gets line
		if {[$fp eof]} break
		puts "<tr><td>$l</td><td>[quote $line]</td></tr>"
		incr l
	}
}
$fp close

