#!/usr/bin/perl -w
use strict;
use SVK;
our $VERSION = $SVK::VERSION;
use SVK::I18N;
use Getopt::Long qw(:config no_ignore_case bundling);
use autouse 'SVK::Util' => qw(get_anchor catfile catdir);

=head1 NAME

svk - A Distributed Version Control System

=head1 SYNOPSIS

B<svk> I<command> S<[I<options>]> [I<args>]

=head1 DESCRIPTION

B<SVK> is a decentralized version control system written in Perl.
It uses the Subversion filesystem but provides additional features:

=over 4

=item * Offline operations like C<checkin>, C<log>, C<merge>.

=item * Distributed branches.

=item * Lightweight checkout copy management (no F<.svn> directories).

=item * Advanced merge algorithms, like I<star-merge> and I<cherry picking>.

=back

For more information about the SVK project, visit L<http://svk.elixus.org/>.

Run C<svk help> to access the built-in tool documentation.

By default svk stores its state in the F<.svk> directory in your home
directory. You can change this default by setting the SVKROOT environment
variable to your preferred svk depot path.

=cut

my $cmd = shift;

if (!$cmd or $cmd =~ /^-{0,2}[Hh](?:elp)?$/) {
    SVK::Command->invoke (undef, 'help', undef, @ARGV);
    exit 0;
}

{
    my $show_version;
    local *ARGV = [$cmd || ''];
    GetOptions ('v|version' => \$show_version) or exit;

    if ($show_version || ($cmd && $cmd eq 'version')) {
	print loc("This is svk, version %1.\n", $VERSION);
	exit 0;
    }
}

$ENV{HOME} ||= (
    $ENV{HOMEDRIVE} ? catdir(@ENV{qw( HOMEDRIVE HOMEPATH )}) : ''
) || (getpwuid($<))[7];
$ENV{USER} ||= (
    (defined &Win32::LoginName) ? Win32::LoginName() : ''
) || $ENV{USERNAME} || (getpwuid($<))[0];

my $svkpath = $ENV{SVKROOT} || catfile($ENV{HOME}, ".svk");
my $ret;
{
    my $xd = SVK::XD->new ( giantlock => catfile($svkpath, 'lock'),
			    statefile => catfile($svkpath, 'config'),
			    svkpath => $svkpath,
			  );
    $xd->load();
    $SIG{INT} = sub {
	die loc("Interrupted.\n");
    };
    unless ($ENV{SVKNOSVNCONFIG}) {
	SVN::Core::config_ensure (undef);
	$xd->{svnconfig} = SVN::Core::config_get_config (undef);
    }

    $ret = SVK::Command->invoke ($xd, $cmd, undef, @ARGV);
    $xd->store ();
}

1;

exit (defined $ret ? $ret : 1);

require PerlIO;
require PerlIO::via;
require PerlIO::scalar;
require Encode::TW;

=head1 AUTHORS

Chia-liang Kao E<lt>clkao@clkao.orgE<gt>

=head1 COPYRIGHT

Copyright 2003-2005 by Chia-liang Kao E<lt>clkao@clkao.orgE<gt>.

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

See L<http://www.perl.com/perl/misc/Artistic.html>

=cut
