#!/usr/bin/perl -wT
#
# TWiki Collaboration Platform, http://TWiki.org/
#
# Copyright (C) 2002-2004 Peter Thoeny, peter@thoeny.com
#
# For licensing info read license.txt file in the TWiki root.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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, published at
# http://www.gnu.org/copyleft/gpl.html
#
# The manage script is used to manage some actions like creating
# a new web.
#
#usage example:
#
#
#C h a n g e
#
#</form>
#<form name="passwd" action="/%SCRIPTURLPATH%/passwd%SCRIPTSUFFIX%/%WEB%/">
#Username     <input type="text" name="username" value="" size="16" /> <br />
#Old password <input type="password" name="oldpassword" size="16" />
#New password <input type="password" name="password" size="16" />
#retype New password <input type="password" name="passwordA" size="16" />
#<input type="submit" name="passwd" />
#<input type="hidden" name="action" value="changePassword" />
#</form>
#

BEGIN {
    # 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';
}

use CGI::Carp qw( fatalsToBrowser );
use CGI;

use TWiki::UI::Manage;

   my %configHash;
   my $config = \%configHash;
   my $twikiUri = $ENV{"REQUEST_URI"};
   $twikiUri =~ s!/+!/!g ; # remove multiple'/' 
   my @twikiUriItem = split ( '/' , $twikiUri ) ;
   $config->{fileConfig} = $ENV{"SERVER_NAME"} . "Port" . $ENV{"SERVER_PORT"} . $twikiUriItem[ 1 ];
   $config->{TWiki}=TWiki->new( $config );
   $config->{TWiki}->init_config();


my $query = new CGI;
my $action = $query->param( 'action' ) || "";
my $thePathInfo = $query->path_info();
my $theUrl = $query->url;
my $theRemoteUser = $query->remote_user();
my $theTopic = $query->param( 'topic' );

# initialization has to be done differently for each because it is passed
# different parameters, oddly.
if( $action eq "createweb" ) {

  my( $topic, $webName, $dummy, $userName ) = 
    $config->{TWiki}->initialize ( $thePathInfo, $theRemoteUser,
                       $theTopic, $theUrl, $query );

  $config->{TWiki}->{UI}->{Manage}->createWeb ( $webName, $topic, $userName, $query );

} elsif( $action eq "changePassword" ) {

  my $wikiName = $query->param( 'username' );
  my $topicName = $query->param( 'TopicName' );

  my ( $topic, $webName ) =
    $config->{TWiki}->initialize ( $thePathInfo, $wikiName, $topicName, $theUrl, $query );

  $config->{TWiki}->{UI}->{Manage}->changePassword ( $webName, $topic, $query );

} elsif( $action eq "deleteUserAccount" ) {

  my $wikiName = $query->param( 'username' );
  my $topicName = $query->param( 'TopicName' );

  my ( $topic, $webName ) =
    $config->{TWiki}->initialize ( $thePathInfo, $wikiName, $topicName, $theUrl, $query );

    $config->{TWiki}->{UI}->{Manage}->removeUser ($webName, $topic, $wikiName, $query);

} elsif( $action eq "relockrcs" ) {
    $config->{TWiki}->{UI}->{Manage}->relockRcsFiles ();
} elsif( $action ) {

  my( $topic, $webName, $dummy, $userName ) = 
    $config->{TWiki}->initialize ( $thePathInfo, $theRemoteUser,
                       $theTopic, $theUrl, $query );
  $config->{TWiki}->{UI}->oops ( "", "", "manage",
                   $config->{TWiki}->{UI}->{Manage}->_template ("msg_unrecognized_action"),
                   $action );

} else {

  my( $topic, $webName, $dummy, $userName ) = 
    $config->{TWiki}->initialize ( $thePathInfo, $theRemoteUser,
                       $theTopic, $theUrl, $query );
  $config->{TWiki}->{UI}->oops ( "", "", "manage",
                   $config->{TWiki}->{UI}->{Manage}->_template ("msg_missing_action") );

}


