#!/usr/bin/perl -w
# File: simple.pl
# Created: Brooke Smith 2005 02 10
# Purpose: To create a small perl svn script that I can sent to the svn-users list to see
#	if they help work out why it craps out.


use SVN::Client;

my $gRepoPath = "svn://eeow/new/tags";

my $ctx = new SVN::Client();

#local $SVN::Error::handler=undef; -- won't work - fails with BUS ERROR

sub error_handler_PRINT() {
	my $lsvn_error_t = $_[0];

	print STDERR "  SVN::Error::handler\n";
	print STDERR "  message: " . $lsvn_error_t -> message() . "\n";
	print STDERR "  apr_err: " . $lsvn_error_t -> apr_err() . "\n";
	if ($lsvn_error_t -> apr_err() == $SVN::Error::CLIENT_BAD_REVISION) {
		print STDERR "    CLIENT_BAD_REVISION error\n";
	}
	print STDERR "  strerror: " . $lsvn_error_t -> strerror() . "\n";
	print STDERR "  is_error: ";
	if (SVN::Error::is_error($lsvn_error_t)) {# -> apr_err())) {
		print STDERR " yes\n";
	} else {
		print STDERR " no\n";
	}
	
	print STDERR "  expanded_message: " . $lsvn_error_t -> expanded_message() . "\n";
} # error_handler_PRINT()

#This still dies with a bus error.
sub error_handler_ls_ignore(){
	my $lsvn_error_t = $_[0];

	if ($lsvn_error_t -> apr_err() == $SVN::Error::FS_NOT_FOUND) {
		print STDERR "    FS_NOT_FOUND error\n";
		&error_handler_PRINT($lsvn_error_t);
		SVN::Error::ignore_error($lsvn_error_t);	# Won't work, dies with a Bus Error
		print STDERR "  ls_ignore handler - after returning from ignore_error\n";
		$lsvn_error_t->clear();	# Even though the Core.pm (SVN::Error package)
		#die("die you bitch");	# this will work and avoid the bus error, but not what i want
	} else {
		&error_handler_PRINT($lsvn_error_t);
		SVN::Error::croak_on_error($lsvn_error_t);	
	}
	print STDERR "  ls_ignore handler - just before exiting the handler.\n";\
	return;
}

sub error_handler_ls_confess(){
	my $lsvn_error_t = $_[0];

	if ($lsvn_error_t -> apr_err() == $SVN::Error::FS_NOT_FOUND) {
		print STDERR "    FS_NOT_FOUND error\n";
		&error_handler_PRINT($lsvn_error_t);
		SVN::Error::confess_on_error($lsvn_error_t);	
	} else {
		&error_handler_PRINT($lsvn_error_t);
		SVN::Error::croak_on_error($lsvn_error_t);	
	}
}

sub error_handler_ls_croak(){
	my $lsvn_error_t = $_[0];

	if ($lsvn_error_t -> apr_err() == $SVN::Error::FS_NOT_FOUND) {
		print STDERR "    FS_NOT_FOUND error\n";
		&error_handler_PRINT($lsvn_error_t);
		SVN::Error::croak_on_error($lsvn_error_t);	
	} else {
		&error_handler_PRINT($lsvn_error_t);
		SVN::Error::croak_on_error($lsvn_error_t);	
	}
}

sub works_croakInstallederrorhandler() {
  local $SVN::Error::handler = \&error_handler_ls_croak;
  $ctx -> ls($gRepoPath, 'HEAD', 1);
}

sub works_confessInstallederrorhandler() {
  local $SVN::Error::handler = \&error_handler_ls_confess;
  $ctx -> ls($gRepoPath, 'HEAD', 1);
}

sub aintwork_ignoreInstallederrorhandler() {
  local $SVN::Error::handler = \&error_handler_ls_ignore;
  $ctx -> ls($gRepoPath, 'HEAD', 1);
}

sub works_defaulterrorhandler() {
  #local $SVN::Error::handler = \&error_handler_ls;
  $ctx -> ls($gRepoPath, 'HEAD', 1);
}

sub works_simple() {
  eval{$ctx -> ls($gRepoPath, 'HEAD', 1);};
  if ($@) {
	  if ($@ =~ /Filesystem has no item: URL/) {
		  print STDERR "An error to do with no such URL - pass is something correct buster!\n";
	  } else {
		print STDERR "Error pooh: $@\n";
		exit 1;
	  }
  }
}

print STDERR "Call ls(" . $gRepoPath . ", 'HEAD', 1)\n";

#works_simple();
#works_croakInstallederrorhandler();
#works_confessInstallederrorhandler();
aintwork_ignoreInstallederrorhandler();

