#!/usr/local/qddb/bin/qtclsh
# 
# Copyright (C) 1996 Herrin Software Development, Inc.
# All rights reserved.
#
# This file is part of Qddb Guest Book.
#
# Qddb Guest Book is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License Version 2
# as published by the Free Software Foundation.
#
# Qddb Guest Book is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Qddb Guest Book; see the file LICENSE.  If not, write to:
#
#	Herrin Software Development, Inc. 
#	R&D Division
#	41 South Highland Ave. 
#	Prestonsburg, KY 41653 
#

lappend auto_path $qddb_library/hx
source /var/lib/ISPutil/bin/init.tcl

#
# Note that the "email" and "homepage" attributes
# must exist in your GuestBook/Schema since this
# script treats them specially.

# The page_* variables are for customizing your pages.

set page_title "Clients database"
set page_opening "Welcome to the New User input form"
set page_message \
"This is a new-user creation form. It creates a bare account
with no months until expiry, no online hours, no extras or
specials of any kind. It does not even have a password.
(The 
<A HREF=\"http://$MAINHOST:98/htmlmod:userpass:\">
password change interface</A>
can be used to change passwords).
"
set page_signature "<p align=right><b>The Knotwork Team</b></p>"


# ---- YOU SHOULDN'T HAVE TO CHANGE MUCH BELOW THIS LINE
# (But of course you can if you want:-)


# The following two variables need to be customized to your
# site
source ./init.tcl

set env(QDDBDIRS) $dir
set mypath "$cgidir/client"

source postform.tcl
# allow privnote entry, but not searching or display

GuestBookEntryPostForm ;# append privnote or other private entries

set s [qddb_schema open $DBname]
Hx:Init $s

if {[string compare $env(REQUEST_METHOD) GET] == 0} {
	puts -nonewline \
"Content-type: text/html

<html>
<head><title>$page_title</title></head>
<body bgcolor=#F0F0F0 text=#222222 link=#1B3E62 vlink=#962835 alink=#EE0000>
<h2>$page_opening</h2>
<p>
$page_message
<p>
<br>
$page_signature
<hr size=3 width=95% align=center NOSHADE>

<form method=POST ACTION=${mypath}?$env(QUERY_STRING)>
<pre>
[Hx:DefinedEntryForm $s $postform]
<br>
<input type=submit value=\"Submit form!\"> <input type=reset value=\"Clear entry\">

</pre>
</form>

</body>
</html>
"
} elseif {[string compare $env(REQUEST_METHOD) POST] == 0} {
	source $dir/utils.tcl
	set l1 [Hx:GetPostedData]
	set t [qddb_tuple new $s]
	set v [qddb_view define $t {}]
	qddb_instance setval $v DateOfEntry "now"
	foreach i $l1 {
		set attr [lindex $i 0]
		set attrval [lindex $i 1]
		if {[string length [string trim $attrval]] == 0} {
			continue
		}
		set vals($attr) $attrval
		qddb_instance setval $v $attr $attrval
	}
	if {![info exists vals(Company.Contact.Username)] \
		|| ![info exists vals(Company.Contact.First)] \
		|| ![info exists vals(Company.Contact.Last)]} {
		Usage
		exit 0
	}
	set user $vals(Company.Contact.Username)
	catch {exec $NAMEINUSE $user} result
	if {$errorCode != "NONE"} {
		BadName
		exit 0
	}
	puts "Content-type: text/html\n\n"
	puts "<H1>Creating User</H1>\n\n<PRE>\n"
	if {[info exists vals(Company.Name)]} {
		set fullname $vals(Company.Name)
	} else {
	set fullname "$vals(Company.Contact.First) $vals(Company.Contact.Last)"
	}
	set result ""
	catch { exec $MAKEUSER $user \
		"$DEFAULT_GID" "$DEFAULT_EXPIRY" "$DEFAULT_TIME" \
		"$DEFAULT_SESSION"  "$DEFAULT_PRIORITY" \
		 "$DEFAULT_SHELL" "$fullname" \
	} result
	puts "$result"
	puts "</PRE>\n"
	if {$errorCode != "NONE"} {
		FailedMakeuser
		exit 0
	}
	qddb_instance setval $v SalesRep $env(REMOTE_USER)
	qddb_instance setval $v CustomerClass Internet
	qddb_instance setval $v CustomerType COD
	qddb_instance setval $v Company.Contact.Homedir "/home/$user"
	qddb_instance setval $v Company.Contact.ID [nextClient]
	qddb_instance setval $v Company.Contact.GID $DEFAULT_GID
	qddb_instance setval $v Company.Contact.StartDate "now"
	qddb_instance setval $v Company.Contact.Shell $DEFAULT_SHELL
	qddb_instance setval $v Company.Contact.Email \
		"$vals(Company.Contact.Username)@$MAILSERVER"
	qddb_instance setval $v Company.Contact.Pending 0.00
	qddb_instance setval $v Company.Contact.WebProject.Title "$fullname"
	qddb_instance setval $v Company.Contact.WebProject.Path \
		"http://$WEBSERVER/~$user"
	qddb_tuple write $t
	puts "Client record written to database."
	puts -nonewline \
"<html>
<head><title>$page_title</title></head>
<body bgcolor=#F0F0F0 text=#222222 link=#1B3E62 vlink=#962835 alink=#EE0000>
<h2>User created!</h2>
</body>
</html>
"
}
exit 0
