#!/mod/bin/jimsh

package require cgi
source /mod/webif/lib/setup
require settings.class plugin system.class spinner.class

cgi_input

httpheader

set modules {general auto tvdb web epg network advanced}

set settings [settings new]
if {![dict exists $env SCRIPT_NAME]} { set env(SCRIPT_NAME) "" }

proc _handle_update {class var old text {trim 1} {min ""} {max ""}} {
	global _cgi
	global settings

	if {[dict exists $_cgi $var]} {
		set new [dict get $_cgi $var]
		if {$trim} {
			set new [string trim $new]
		}
		if {$new == $old} {
			puts "$text unchanged."
		} elseif {[string is $class -strict $new]} {
			if {$class == "digit" && $min ne "" && $new < $min} {
				puts "Invalid value for $var (too small)."
				exit
			}
			if {$class == "digit" && $max ne "" && $new > $max} {
				puts "Invalid value for $var (too large)."
				exit
			}
			$settings $var $new
			puts "$text updated."
		} else {
			puts "Invalid value for $var."
		}
		exit
	}
}

proc handle_int_update {var old {text "Value"} {trim 1} {min ""} {max ""}} {
	_handle_update digit $var $old $text $trim $min $max
}

proc handle_str_update {var old {text "Value"} {class alnum} {trim 1}} {
	_handle_update $class $var $old $text $trim
}

######################################################################
# Settings Modules

set moddir /mod/webif/html/settings/modules
foreach mod $modules {
	if {[file exists "$moddir/$mod/.disabled"]} continue
	if {[catch {source "$moddir/$mod/init.hook"} msg]} {
		puts "ERROR: $msg"
	}
}

######################################################################
# Web Page

jqplugin form actual iphone-style-checkboxes touchpunch multiselect
jscss settings.js style.css
header

[spinner new {
        text "Loading Settings..."
        size "1.2em"
        style "margin: 1em;"
        }] start

puts {
<div id=accordion class=hidden>
}

proc setting_toggle {name attr checked {invert 0} {val 0}} {
	puts -nonewline "
	    <tr>
		<th class=key>$name</th>
		<td><input class=setting_toggle type=checkbox value=yes"
	if {$checked} { puts -nonewline " checked" }
	puts -nonewline " attr=$attr"
	if {$invert} { puts -nonewline " invert=1" }
	if {$val} { puts -nonewline " useval=1" }
	puts -nonewline ">
		<div id=${attr}_output></div>
		</td>
	</tr>
	"
}

proc setting_number {name descr val {min ""} {max ""}} {
	puts -nonewline "
	<tr>
	<form class=auto id=${name} method=get action=$::env(SCRIPT_NAME)>
		<th class=key>$descr</th>
		<td>
			<input name=${name} size=5 type=number
			    class=\"text ui-widget-content ui-corner-all\"
			    maxlength=10"
	if {$min ne ""} {
		puts -nonewline " min=$min"
	}
	if {$max ne ""} {
		puts -nonewline " max=$max"
	}
	puts -nonewline " value=\"$val\">
			<small>
			<input value=\"set\" type=submit>
			</small>
			<div id=${name}_output></div>
		</td>
	</form>
	</tr>
	"
}

######################################################################
# Settings Modules

foreach mod $modules {
	if {[file exists "$moddir/$mod/.disabled"]} continue
	if {[catch {source "$moddir/$mod/settings.hook"} msg]} {
		puts "ERROR: $msg"
	}
}

######################################################################
# Plugins

proc plugin_callback {plugin hook} {
	puts "<h4>Settings for <i>[file tail $plugin]</i> package</h4>"
	puts "<div class=pluginsettings>"
	if {[catch {uplevel source "$plugin/$hook"} msg]} {
		puts "ERROR: $msg"
	}
	puts "</div>"
}

eval_plugins settings 0 plugin_callback

puts "</div>"

footer

