
if {![exists -proc require]} {
	proc require {args} {{done {}}} {
		foreach file $args {
			if {$file ni $done} {
				uplevel source "/mod/webif/lib/$file"
				lappend done $file
			}
		}
	}

	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
	}

	set ::_mws_headerdone 0
	set ::_mws_js {}
	set ::_mws_css {}
	proc header {{type "text/html"} {cache 0}} {
		httpheader $type $cache
		uplevel source /mod/webif/html/lib/header.jim
		incr ::_mws_headerdone
	}

	proc noheader {} {
		emit_jscss
		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 _css {file} {
		if {$::_mws_headerdone} {
		  puts "<link href=\"$file\" rel=stylesheet type=text/css />"
		} else {
			lappend ::_mws_css $file
		}
	}

	proc _js {file} {
		if {$::_mws_headerdone} {
		  puts "<script type=text/javascript src=\"$file\"></script>"
		} else {
			lappend ::_mws_js $file
		}
	}

	proc emit_jscss {} {
		foreach js $::_mws_js { _js $js }
		foreach css $::_mws_css { _css $css }
	}

	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 [glob -nocomplain "$dir/*.js"] {
			set file [join [lrange [split $file /] 4 end] /]
			_js "/$file"
		}
		foreach file [glob -nocomplain "$dir/*.css"] {
			set file [join [lrange [split $file /] 4 end] /]
			_css "/$file"
		}
	    }
	}

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

	require fileops overrides utils

	# Jim 0.75 removes legacy 'case'
	alias case switch
}

