#!/bin/sh
#
# Tcl HTTPD on sagetop.scriptics.com
#
# This is the main script for an HTTP server. 
# To test out of the box, do
# tclsh httpd -debug
# or
# wish httpd -debug
#
# Ordinarily you'll want to
# edit this script to set various parameters, plus you'll
# want to set the path to wish or tclsh
# (Tcl 7.5 or higher is required,
# this works with the latest version of Tcl 8.0, too)
#
# For a quick spin, see the settings under QUICK START.
# For fully custom operation, see the settings under CONFIGURATION.
#
# Copyright (c) 1997 Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# \
exec tclsh8.0 "$0" ${1+"$@"}

# Bootstrap the location of the script library.  This requires that
# the main httpd script be in a peer directory (e.g., ./bin and ./lib)
# home is our location
# Httpd(library) is where the script libraries are

set home [string trimright [file dirname [info script]] ./]
set home [file join [pwd] $home]
set Httpd(library) [file join [file dirname $home] lib]
lappend auto_path $Httpd(library)

#QUICK CONFIG
# The server comes with a URL tree under htdocs
#set docRoot	/proj/tcl/ws/public_html
set docRoot	/home/welch/demo
set port 	80			;# 80 is standard, of course
set host 	sagetop.scriptics.com	;# Server name, really
#set ipaddr	1.2.3.4			;# To bind to a specific interface
set webmaster	webmaster@scriptics.com	;# An email address for errors
set uid		50			;# "tclhttpd", for setuid
set gid		100			;# "users", for caching templates
#end QUICK

# 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 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
#Auth_AccessFile		.htaccess		;# Enable Basic Auth
Log_SetFile		/tmp/log$port:
Log_FlushMinutes	1
#END CONFIGURATION

# Begin demo support
package require demo
package require siteview
::siteview::root	/tcl			;# Root of dynamic URL tree
::siteview::directUrl	/live			;# Form handling direct URL
::siteview::dbDir	/home/welch/web/db	;# Directory containing data
::siteview::/genhtml				;# Init the tree

if [catch {
    set port 80
    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 /tmp/log$port:
	    break
	}
    }
}

Log_Flush

# 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"}
    vwait forever
}
