#!/usr/bin/perl -w
# uses GCI args:
# uid => remoteUser
# name => firstLastName
# email => emailAddress
# wikiname => emailAddress
# comments

BEGIN { unshift @INC, "/www/wiki/lib";
#    # Set default current working directory
#    if( $ENV{"SCRIPT_FILENAME"} && $ENV{"SCRIPT_FILENAME"} =~ /^(.+)\/[^\/]+$/ ) {
#        chdir $1;
#    }
#    # Set library paths in @INC at compile time
#    unshift @INC, '.';
#    require 'setlib.cfg';
}

# I18N: No locale settings necessary yet - only 7-bit ASCII due
# to Apache limitations on userids.


use CGI::Carp qw(fatalsToBrowser);
use CGI;
use TWiki;
use TWiki::Net;
use TWiki::Plugins;
use TWiki::User;

&main();

sub main
{
    my $query = new CGI;
    # get all parameters 
    my $firstLastName = $query->param("name");
    my $emailAddress = $query->param("email");
    my $wikiName = $query->param("wikiname");
    my $remoteUser = $query->param("uid");
    my $comments = $query->param("comments");

    my $topicName = $wikiNname;
    my $thePathInfo = $query->path_info(); 
    my $theUrl = $query->url;
    ( $topic, $webName ) = 
	&TWiki::initialize( $thePathInfo, $wikiName, $topicName, $theUrl, $query );

    my $text = "";
    my $url = "";

    # check if user entry already exists
    if(  ( $wikiName ) 
      && (  ( &TWiki::Store::topicExists( $webName,  $wikiName ) )
         || ( TWiki::User::UserPasswordExists( $wikiName ) ) 
         ) ) {
        # PTh 20 Jun 2000: changed to getOopsUrl
        $url = &TWiki::getOopsUrl( $webName, $topic, "oopsregexist", $wikiName );
        TWiki::redirect( $query, $url );
        return;
    }

    # a WikiName is safe, so untaint variable
    $wikiName =~ /(.*)/;
    $wikiName = $1;

    # everything OK

    # create user topic if it does not exist
    if( ! &TWiki::Store::topicExists( $TWiki::mainWebname, $wikiName ) ) {
        my $meta = "";
        my $row = "";
        ( $meta, $text ) = &TWiki::Store::readTemplateTopic( "NewUserTemplate" );
        $text = "%SPLIT%\n\t* %KEY%: %VALUE%%SPLIT%\n" unless $text;
        ( $before, $repeat, $after) = split( /%SPLIT%/, $text );

	$row = $repeat;	
	$row =~ s/%KEY%/Name/go; $row =~ s/%VALUE%/$firstLastName/go;
	$before .= $row;
	$row = $repeat;	
	$row =~ s/%KEY%/Email/go; $row =~ s/%VALUE%/$emailAddress/go;
	$before .= $row;
	$row = $repeat;	
	$row =~ s/%KEY%/Account/go; $row =~ s/%VALUE%/<a href=\'http:\/\/people.ilog.fr\/employee-$remoteUser\'>$remoteUser<\/a>/go;
	$before .= $row;
	$row = $repeat;	
	$row =~ s/%KEY%/Comments/go; $row =~ s/%VALUE%/$comments/go;
	$before .= $row;

        $text = "$before$after";

       my $userName = $remoteUser || $wikiName;
       $text = TWiki::expandVariablesOnTopicCreation( $text, $userName, $wikiName, "$webName.$wikiName" );

        $meta->put( "TOPICPARENT", ( "name" => $TWiki::wikiUsersTopicname ) );
        &TWiki::Store::saveTopic( $webName, $wikiName, $text, $meta, "", 1 );
    }

    # Plugin callback to set cookies. Contrib by SvenDowideit
    &TWiki::Plugins::registrationHandler( $webName, $wikiName, $remoteUser );

    # add user to TWikiUsers topic
    my $userTopic = TWiki::User::addUserToTWikiUsersTopic( $wikiName, $remoteUser );

    # write log entry
    if( $TWiki::doLogRegistration ) {
        &TWiki::Store::writeLog( "register", "$webName.$wikiName", $emailAddress, $wikiName );
    }

    if( $senderr ) {
        my $url = &TWiki::getOopsUrl( $webName, $wikiName, "oopssendmailerr", $senderr );
        TWiki::redirect( $query, $url );
    }

    # and finally display thank you page
    $url = &TWiki::getOopsUrl( $webName, $wikiName, "oopsregthanks", $emailAddress );
    TWiki::redirect( $query, $url );
}


# EOF
