
if {![exists -proc require]} {

	proc require {args} {{done {}}} {
		foreach file $args {
			if {$file ni $done} {
				lappend done $file
				uplevel source "/mod/webif/lib/$file"
			}
		}
	}

	require system.class
	set ::mws::headerdone 0
	set ::mws::js {}
	set ::mws::css {}
	set ::mws::q "[system pkgver]"

	require fileops overrides utils

	# Jim 0.75 removes legacy 'case'; some plugins still use it.
	# This will fix the simple cases.
	alias case switch

	proc httpredirect {url {extra ""}} {
		puts -nonewline "Status: 302 redirect\r\n"
		puts -nonewline "Location: $url\r\n"
		if {$extra ne ""} {
			puts -nonewline "$extra\r\n"
		}
		puts -nonewline "\r\n"
		puts "Redirecting to $url"
		exit
	}

	proc httpheader {{type "text/html"} {cache 0} {extra ""}} {{done 0}} {
		if {$done} return
		if {!$cache} {
			puts -nonewline "Content-Type: $type; charset=\"UTF-8\"; no-cache\r\n"
			puts -nonewline "Expires: -1\r\n"
			puts -nonewline "Connection: close\r\n"
			puts -nonewline "Pragma: no-cache\r\n"
			puts -nonewline "Cache-Control: no-cache\r\n"
		} else {
			puts -nonewline "Content-Type: $type; charset=\"UTF-8\"\r\n"
		}
		if {$extra ne ""} { puts -nonewline "$extra" }
		puts -nonewline "\r\n"
		set done 1
	}

	proc setpagetag {name} {
		set ::mws::pagetag $name
	}

	proc ::mws::setpagetitle {} {
		global env
		if {[info exists ::mws::pagetitle]} return
		if {![dict exists $env REQUEST_URI]} {
			set ::mws::pagetitle "CLI"
			return
		}
		if {![info exists ::mws::pagetag]} {
		    switch -glob -- $env(REQUEST_URI) {
			"/sched/*" { set ::mws::pagetag "Schedule" }
			"/diag/*" { set ::mws::pagetag "Diagnostics" }
			"/browse/*" { set ::mws::pagetag "Browse Files" }
			"/xepg/*" { set ::mws::pagetag "EPG" }
			"/settings/*" { set ::mws::pagetag "Settings" }
			"/services/*" { set ::mws::pagetag "Services" }
			"/pkg/*" { set ::mws::pagetag "Packages" }
			"/cgi-bin/*" { set ::mws::pagetag "" }
			default {
				set _xpath [split $env(REQUEST_URI) /]
				if {[llength $_xpath] > 2} {
					set ::mws::pagetag [string totitle [\
					    lindex $_xpath 1]]
				} else {
					set ::mws::pagetag ""
				}
			}
		    }
		}

		set ::mws::pagetitle "Humax [system model] Fox T2 ([\
		    system hostname])"
		if {$::mws::pagetag ne ""} {
			append ::mws::pagetitle " - $::mws::pagetag"
		}
	}

	proc header {{type "text/html"} {cache 0}} {
		httpheader $type $cache
		::mws::setpagetitle
		uplevel source /mod/webif/html/lib/header.jim
		incr ::mws::headerdone
	}

	proc noheader {{type "text/html"} {cache 0}} {
		httpheader $type $cache
		uplevel source /mod/webif/html/lib/noheader.jim
		incr ::mws::headerdone
	}

	proc footer {} {
		uplevel source /mod/webif/html/lib/footer.jim
	}

	proc mheader {} {
		uplevel source /mod/webif/html/m/lib/header.jim
	}

	proc mfooter {} {
		uplevel source /mod/webif/html/m/lib/footer.jim
	}

	proc ::mws::_css {file} {
		if {$::mws::headerdone} {
		  puts "<link href=\"$file?$::mws::q\" rel=stylesheet type=text/css />"
		} else {
			lappend ::mws::css $file
		}
	}

	proc ::mws::_js {file} {
		if {$::mws::headerdone} {
		  puts "<script type=text/javascript src=\"$file?$::mws::q\"></script>"
		} else {
			lappend ::mws::js $file
		}
	}

	proc ::mws::emit_jscss {} {
		foreach js $::mws::js { ::mws::_js $js }
		foreach css $::mws::css { ::mws::_css $css }
	}

	proc ::mws::_jqplugin_sort {a b} {
		return $([string length $a] - [string length $b])
	}

	proc jqplugin {args} {{done {}}}  {
	    foreach name $args {
		if {$name in $done} continue
		lappend done $name
		set dir "/mod/webif/html/lib/jquery.plugin/$name"
		if {![file isdirectory $dir]} {
			error "Unknown JQ Plugin - '$name'"
		}
		foreach file [lsort -command ::mws::_jqplugin_sort [\
		    glob -nocomplain "$dir/*.js"]] {
			set file [join [lrange [split $file /] 4 end] /]
			::mws::_js "/$file"
		}
		foreach file [lsort -command ::mws::_jqplugin_sort [\
		    glob -nocomplain "$dir/*.css"]] {
			set file [join [lrange [split $file /] 4 end] /]
			::mws::_css "/$file"
		}
	    }
	}

	proc jscss {{js ""} {css ""}} {
		foreach j $js { ::mws::_js $j }
		foreach c $css { ::mws::_css $c }
	}
}

