#!/usr/bin/perl -w  
#  wiki2html 
#   Copyright (C) 1998 Marcus Denker
#   Copyright (C) 1998 ATIS Department of Computer Science, 
#                      University of Karlsruhe / Germany  
#   Copyright (C) 2004 Andrew Ruthven (andrew@etc.gen.nz)
# 
#   Written by:  Marcus Denker <marcus@ira.uka.de>
#   Created: Aug 1998
#   Modified: 2004/03/16 - AGR - Modified to convert AtisWiki to Kwiki.
#
#   This program is based about wiki2html as written by Marcus Denker.
#   It has been rewritten to convert an AtisWiki database into a kwiki
#   database.  It retains as much information as possible (but not
#   CVS or RCS history information, I couldn't be bothered with that).
#
#   There are no guarantees as to what this will look like.  It works
#   for my pages, in a format I'm happy with.  I don't think I'll need
#   it again, but I'm happy to apply patches if people find things that
#   need to be changed/extended.
#
#   This file is not part of the AtisWiki
#
#   This programm 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 programm 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 for more details.
#
#   You should have received a copy of the GNU General Public License 
#   along with this library; if not, write to the Free Software Foundation, 
#   Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

use strict;
use vars qw($pa $LinkPattern %page);

use lib '/home/puck/lib/Wiki';

use PageArchive;
use Wiki;
use FormatWiki;
use FormatSwiki; 
use FormatRawhtml;
use FormatAscii;
use FormatTransclude;

my $defaultType = "wiki";
my %page;
my $name = "PuckWiki";
my $wikipath =  "/home/puck/data/wiki";
my $topath = "/home/puck/public_html/cgi-bin/kwiki";
$pa = new PageArchive($wikipath);  

sub convertPage {
    my $id = shift;
    my $title = r_u($id);
    %page = $pa->getPage($id);

    return if $id eq 'RecentChanges';

    # Grab the mtime before we change the id name.
    my ($mtime) = (stat("$wikipath/$id.db"))[9];

    # Wiki links in Kwiki must be alphanumeric strings.  Replace anything
    # that isn't alphanumeric with a hyphen (which does appear to be allowed).
    $id =~ s/\W/-/g;
    $page{text} =~ s/_(.*?)\W(.*?)_/_$1-$2_/g;

    # Forced Wiki links us a different format in kwiki.
    $id =~ s/^_//;
    $id =~ s/_$//;
    $page{text} =~ s/_(.*?)_/\[$1\]/g;

    open (OUT, "> $topath/database/$id")
      || die "Failed to open $topath/database/$id for writing: $!\n";

    print OUT "$page{text}\n";
    close OUT;

    open (OUT, "> $topath/metabase/metadata/$id")
      || die "Failed to open $topath/metabase/metadata/$id for writing: $!\n";

    print OUT "edit_by: "
      . ($page{logname} ne "" ? $page{logname} : "AtisWiki2kwiki")
      . "\n";
    print OUT "edit_time: " . gmtime($mtime) . "\n";

    close OUT;
}


foreach($pa->iterator()) {
  convertPage($_);
}
