#!/usr/local/qddb/bin/qtclsh
# 
# This file is part of the ISPutil utilities package
# Copyright (C) 1996-1997 Mark Metson. All rights reserved.
#
# This file was initially hacked from a part of Qddb Guest Book,
# Copyright (C) 1996 Herrin Software Development, Inc.
# All rights reserved.
#
# ISPutil and Qddb Guest Book are both free software; you can redistribute
# and/or modify them under the terms of the GNU General Public License
# Version 2 as published by the Free Software Foundation.
#
# ISPutil 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 ISPutil package; see the file COPYING.
#

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

# 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 starts with a password of \"Temporary\",
which should IMMEDIATELY be changed using the
<A HREF=\"http://$MAINHOST:98/htmlmod:userpass:\">
Linuxconf password change interface</A>.
<P>
<A HREF=/ISPutil/newuser.spider>More documentation</A> detailing 
what is involved in setting up a new user should be online as part of
the <A HREF=/ISPutil>ISPutil documentation</A>.
"


set page_signature "<b>The <a href=\"http:/ISPutil/\">ISPutil</a> Team</b></p>"

set body \
"<body bgcolor=#F0F0F0 text=#222222 vlink=#1B3E62 link=#962835 alink=#EE0000>"


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

source ./init.tcl

# The following two variables need to be customized to your
# site

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
<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 "<html>"
	puts "<head><title>$page_title</title></head>"
	puts "$body"
	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
	}
	if {![info exists vals(Company.Contact.PIN)} {
		qddb_instance setval $v Company.Contact.PIN "NONE"
	}
	if {$vals(Company.Contact.PIN)==""} {
		qddb_instance setval $v Company.Contact.PIN "NONE"
	}
	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.Months 1
	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."
	catch {exec $GREP ^$user: /etc/passwd \
		| $SED -e "s/^$user:x:/$user:Temporary:/" \
		| $NEWUSERS} result
	puts -nonewline \
"<h2>User created!</h2>
Password should presently be \"Temporary\",
which should IMMEDIATELY be changed using the
<A HREF=\"http://$MAINHOST:98/htmlmod:userpass:\">
Linuxconf password change interface</A>.
</body>
</html>
"
}
exit 0
