#!/usr/bin/env perl
use 5.008003;
use strict;
our $VERSION = '0.40';
use Cwd 'cwd', 'abs_path';
use FindBin;
use Config;

sub find_base {
    return $ENV{KWIKI_BASE} if defined $ENV{KWIKI_BASE};

    my $base = $FindBin::Bin;
    my $bin = $0;
    $bin =~ s/.*\///;
    $bin = "$base/$bin";
    my $home = cwd();
    while (-l $bin) {
        chdir $base;
        $bin = abs_path(readlink $bin);
        $base = $bin;
        $base =~ s/(.*)\/.*/$1/;
    }
    chdir $home;
    $base = abs_path("$base/../../..");

    die "*ERROR*: Can't locate Kwiki base repository."
        unless -d "$base/lib/Kwiki" and -d "$base/src/core/Kwiki";
    $ENV{KWIKI_BASE} = $base;
    return $base;
} 

BEGIN {
    my @env_files = qw(kwiki.env .ht-kwiki.env);
    push @env_files, "$ENV{HOME}/.kwiki/kwiki.env" if defined $ENV{HOME};
    my $fh;
    for (@env_files) {
        if (open $fh, $_) {
            do {
                $ENV{$1} ||= $2 if /^(\w+)\s*=\s*['"]?(.*?)['"]?\s*$/;
            } for <$fh>;
            last;
        }
    }

    if ($ENV{KWIKI_TEST_CLEAN}) {
        @INC = (
            $Config::Config{archlib},
            $Config::Config{privlib},
        ); 
    }

    $ENV{KWIKI_LIB_PATH} ||= find_base() . "/lib";
}

use lib grep { -e } split /:/, $ENV{KWIKI_LIB_PATH} || 'lib';
use Kwiki::Boot;
Kwiki::Boot->boot->main->hub->command->process(@ARGV)->hub->remove_hooks;

__END__

=head1 NAME

kwiki - The Kwiki Command Line Tool

=head1 USAGE

    > kwiki -new /path/to/cgi-bin/my-kwiki

    Kwiki software installed! Point your browser at this location.

=head1 DESCRIPTION

Kwiki is a simple extendable wiki framework, written in Perl.

See L<Kwiki::Command> for more information on using this command line tool.

=head1 NOTE

If you are on Mac OS X, use the command C<perldoc Kwiki.pm> to get the
Kwiki B<module> documentation.

=head1 AUTHOR

Ingy döt Net <ingy@cpan.org>

=head1 COPYRIGHT

Copyright (c) 2004-2005. Brian Ingerson. All rights reserved.

Copyright (c) 2006-2007. Ingy döt Net. All rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html

=cut

# vim: set ft=perl:
