#!/usr/bin/perl -wTI.
#
# TWiki WikiClone (see wiki.pm for $wikiversion and other info)
#
# Based on parts of Ward Cunninghams original Wiki and JosWiki.
# Copyright (C) 1998 Markus Peter - SPiN GmbH (warpi@spin.de)
# Some changes by Dave Harris (drh@bhresearch.co.uk) incorporated
# Copyright (C) 1999-2000 Peter Thoeny, peter@thoeny.com
#
# 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

use CGI;
use wiki;


$query= new CGI;

&main();

sub main
{
    my $thePathInfo = $query->path_info(); 
    my $theRemoteUser = $query->remote_user();
    my $theTopic = $query->param( 'topic' );
    my $theUrl = $query->url;

    ( $topic, $webName, $dummy, $userName ) = 
	&wiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl );

    $dummy = "";  # to suppress warning
    my $text = "";
    my $tmp = "";
    my $atext = "";
    my $before = "";
    my $after = "";
    my $wikiUserName = &wiki::userToWikiName( $userName );

    if( ! &wiki::webExists( $webName ) ) {
	$tmpl= &wiki::readTemplate( "noweb" );
	$tmpl = &wiki::handleCommonTags( $tmpl, $topic );
	print $tmpl;
	return;
    }

    # check access permission
    if( ! &wiki::checkAccessPermission( "change", $wikiUserName, "", $topic, $webName ) ) {
        my $url = &wiki::getOopsUrl( $webName, $topic, "oopsaccesschange" );
        print $query->redirect( $url );
        return;
    }

    # get text and other parameters
    $text = $query->param( "text" );
    my $unlock = $query->param( "unlock" ) || "";
    my $saveCmd = $query->param( "cmd" ) || "";

    # PTh 06 Nov 2000: check if proper use of save script
    if( ! ( defined $text ) ) {
        my $url = &wiki::getOopsUrl( $webName, $topic, "oopssave" );
        print $query->redirect( $url );
        return;
    }

    if( $saveCmd eq "repRev" ) {
        # for debug: Use $text as is, it possibly contains an attachment table
    } else {
        # normal case: Get latest attachment from file for preview
        my $tmp = &wiki::readTopic( $topic );
        my ( $tmp1, $atext, $tmp2 ) = split( /<!--TWikiAttachment-->/, $tmp );
        # combine text and attachment
        ( $before, $tmp, $after ) = split( /<!--TWikiAttachment-->/, $text );
        if( ! $after ) { $after = ""; }
        if( ! $atext ) {
            $text = "$before$after";
        } else {
            $text = "$before$after<!--TWikiAttachment-->$atext<!--TWikiAttachment-->";
        }
    }

    # PTh 21 Jun 2000: Fix for Codev.KfmBrowserSupportForEditing
    $text =~ s/%_N_%/\r\n/go;
    $text =~ s/%_L_%/</go;
    $text =~ s/%_G_%/>/go;
    $text =~ s/%_Q_%/\"/go;
    $text =~ s/%_A_%/&/go;
    $text =~ s/ {3}/\t/go;

    &wiki::saveTopic( $topic, $text, $saveCmd, "", $unlock );
    print $query->redirect( &wiki::getViewUrl( "", $topic ) );
}
