#!/usr/local/bin/tclsh8.0
#
# Tcl HTTPD for the www.scriptics.com site.
#
# Copyright (c) 1997 Sun Microsystems, Inc.
# Copyright (c) 1998 Scriptics Corporation, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# \
exec /usr/local/bin/tclsh8.0 "$0" ${1+"$@"}

# Hardwire the location of the script library so we can
# run with different software configurations.

set lib /export/tclhttpd2.1.3/lib
set logDir /export/tclhttpd2.1.3/log
set docRoot /export/htdocs.v3
set port 80
set host www.scriptics.com
set uid	50			;# "tclhttpd", for setuid
set gid	50			;# "web", for caching templates
set Httpd(library) $lib
lappend auto_path $Httpd(library)

set webmaster	webmaster@scriptics.com	;# An email address for errors

# Required modules
package require httpd			;# Protocol stack
package require html			;# Simple html generation
package require url			;# URL dispatching
package require auth			;# Basic authentication
package require counter			;# Statistics
package require mtype			;# Mime content types
package require stdin			;# command reader
package require utils			;# junk

# This automatically uses Tk for image maps and
# a simple control panel.  If you have a Tcl-only shell,
# then image maps hits are done differently and you
# don't get a control panel.
# You may need to tweak
# this if your Tcl shell can dynamically load Tk
# because tk_version won't be defined, but it could be.

if [info exists tk_version] {
    # Use a Tk canvas for imagemap hit detection
    package require ismaptk
    # Display Tk control panel
    package require srvui
} else {
    # Do imagemap hit detection in pure Tcl code
    package require ismaptcl
}
Httpd_Init

#CONFIGURATION

# These packages are required for "normal" web servers

package require doc		;# Basic file URLS
package require include		;# Server side includes
#package require safetcl	;# Safe tcl in external process (broken)
package require cgi		;# Standard CGI
package require log		;# Logging
package require dirlist		;# Directory listings

# These packages are for special things built right into the server

package require direct		;# Application Direct URLs
package require status		;# Built in status counters
package require debug		;# Built in status counters
catch {
    source $Httpd(library)/open.tcl ;# Track open files
}

catch {source $Httpd(library)/prodebug.tcl}	;# Tcl-Pro nub

package require mail		;# Crude email support
package require admin		;# Url-based administration
package require session		;# Session state module (better Safe-Tcl)

# These packages are for the SNMP demo application

#package require snmp		;# SNMP form creation
#package require Tnm		;# Low level network stuff

# For information about these calls, see htdocs/reference.html

Doc_Root		$docRoot
Doc_IndexFile		index.{tml,html,shtml,thtml,htm,subst}
Doc_PublicHtml		public_html
Cgi_Directory		/cgi-bin
Status_Url		/status
Debug_Url		/debug
Mail_Url		/mail
Admin_Url		/admin
Doc_TemplateInterp	{}
Doc_CheckTemplates	1
Doc_TemplateLibrary	$docRoot/libtml
Doc_ErrorPage		/error.html
Doc_NotFoundPage	/notfound.html
Doc_Webmaster		$webmaster
if [catch {Auth_AccessFile		.htaccess} err] {
    Stderr "No Basic Authentication: $err"
}
Log_SetFile		$logDir/log${port}_
Log_FlushMinutes	1
#END CONFIGURATION

# Begin Web Resource Center support
package require siteview
::siteview::root	/resource		;# Root of dynamic URL tree
::siteview::directUrl	/live			;# Form handling direct URL
::siteview::dbDir	$docRoot/db.web		;# Directory containing data

# Init look and feel of the Scriptics pages
# It refs the Resource center, so this must come after that is set up
package require scriptics
source $docRoot/.tml
Scriptics_Init


if [catch {
    Httpd_Server 		$port $host	;# Starts the server
}] {
    for {set port 8000} {1} {incr port} {
	if ![catch {
	    Httpd_Server $port $host
	}] {
	    puts stderr "Httpd on [info hostname]:$port"
	    Log_SetFile $logDir/log$port:
	    break
	}
    }
}

Log_Flush

# Build the tree - must do after the server has started.
::siteview::/genhtml

# Try to increase file descriptor limits

if [catch {
    package require limit
    set limit 256
    limit $limit
} err] {
    Stderr $err
    set limit 64
}
Stderr "Running with $limit file descriptor limit"

# Try to change UID to tclhttpd so we can write template caches

if ![catch {
    package require setuid
    setuid $uid $gid
}] {
    Stderr "Running as user $uid"
}

# Start up the user interface and event loop.

if [info exists tk_version] {
    SrvUI_Init "Tcl HTTPD $Httpd(version)"
}
if {[lsearch -glob $argv -de*] >= 0} {
    # Enter interactive command loop, then exit.  Otherwise run forever.
    Stdin_Start "httpd % "
    Httpd_Shutdown
} else {
    catch {puts stderr "httpd started on port $port"}
    vwait forever
}
