#!/usr/bin/perl -w
use strict;

use SVN::Client;       # for accessing subversion
use SVN::Core;         # for SVN::Auth::SSL
use URI::Escape;
use Getopt::Long;
use FileHandle;

my %options;
GetOptions( \%options,
    'verbose!',
    'debug!',
    'config:s',
    'user:s',
    'pass:s',
    'url:s',
    'revision:s',
);

die ("Must supply a url") unless $options{url};

my %svnargs = {};
$svnargs{'auth'} = [
    SVN::Client::get_username_provider(),
    SVN::Client::get_simple_prompt_provider(\&svn_provider,2),
    SVN::Client::get_ssl_server_trust_prompt_provider(\&svn_ssl),
    SVN::Client::get_ssl_client_cert_file_provider(),
    SVN::Client::get_ssl_client_cert_pw_file_provider(),
];
if ( defined($options{config}) ) {
    $svnargs{config} = $options{config};
}
my $svnctx = SVN::Client->new(%svnargs);
my $target = $options{url};
my $revision = 'HEAD';
if ( defined($options{revision}) ) {
    $revision = $options{revision};
}
my $recursive = 1;

my $svnls = $svnctx->ls($target, $revision, $recursive);

foreach my $l (keys %$svnls) {
    my $parser_type = undef;
    my $svndirent = $svnls->{$l};
    my $ll = $l;
    uri_escape($ll);
    my $path = $target . "/" . $ll;
    print "$path\n";
}

sub svn_provider {
    my $cred = shift;
    my $realm = shift;
    my $default_username = shift;
    my $may_save = shift;
    my $pool = shift;

    my $username = '';
    if ( defined($options{user}) ) {
        $username = $options{user};
    }
    $cred->username($username);
    my $password = '';
    if ( defined($options{pass}) ) {
        $password = $options{pass};
    }
    $cred->password($password);
}

sub svn_ssl {
    my $cred = shift;
    my $realm = shift;
    my $certfail = shift;
    my $info = shift;
    my $may_save = shift;
    my $pool = shift;

    $cred->accepted_failures($SVN::Auth::SSL::UNKNOWNCA);
}

exit;
