#!/usr/bin/perl -w

use strict;
use warnings;

######################################################################
#
#
######################################################################

=pod

=for Pod2Wiki

=head1 NAME

B<console> - Provides a console-based REPL to an EPrints archive

=head1 SYNOPSIS

B<console> [I<repository_id>]

=head1 DESCRIPTION

This script loads the specified archive and starts a REPL (Read-Eval-Print Loop) on the console so that you can interact with the repository directly. The repository session is available using the C<$session> variable.

If there is only one archive then the archive name is optional.

This script requires the L<Reply> module.

=head1 ARGUMENTS

=over 8

=item B<repository_id> 

The ID of the eprint repository to use.

=back


=cut


use FindBin;
use lib "$FindBin::Bin/../perl_lib";

require File::Temp;
use File::Temp();

eval("use Reply");

if ($@) {
	print STDERR "EPrints console requires the 'Reply' package.\n";
	exit 1;
}

# Get the list of available archives.

my $archives_path = "$FindBin::Bin/../archives";

opendir my($dh), $archives_path or die "Couldn't open dir '$archives_path': $!";
my @archives = grep { $_ ne ".." && $_ ne "." && -d "$archives_path/$_" } readdir $dh;
closedir $dh;

my $archive = $ARGV[0];

if( !defined( $archive ) )
{
	if( scalar( @archives ) == 0 )
	{
		print STDERR "$0: There are no archives.\n";
		exit 1;
	}
	elsif( scalar( @archives ) == 1 )
	{
		$archive = $archives[0];
	}
	else
	{
		print STDERR "$0: There are multiple archives:\n  @archives\n";
		exit 1;
	}
}
else
{
	if( !grep { /^$archive$/ } @archives)
	{
		print STDERR "$0: Archive '$archive' was not found in $archives_path.\n";
		exit 1;
	}
}

if( !defined( $archive ))
{
	print STDERR "Usage: $0 [ARCHIVE]\n";
	exit 1;
}

my $ini = File::Temp->new( UNLINK => 0 );

open(my $ini_fh, '>', $ini->filename);
print $ini_fh "[Interrupt]\n";
print $ini_fh "[FancyPrompt]\n";
print $ini_fh "[DataDumper]\n";
print $ini_fh "[Colors]\n";
print $ini_fh "[ReadLine]\n";
print $ini_fh "[Hints]\n";
print $ini_fh "[Packages]\n";
print $ini_fh "[LexicalPersistence]\n";
print $ini_fh "[ResultCache]\n";
print $ini_fh "[Autocomplete::Packages]\n";
print $ini_fh "[Autocomplete::Lexicals]\n";
print $ini_fh "[Autocomplete::Functions]\n";
print $ini_fh "[Autocomplete::Globals]\n";
print $ini_fh "[Autocomplete::Methods]\n";
print $ini_fh "[Autocomplete::Commands]\n";
close $ini_fh;

my $reply = Reply->new(config => $ini->filename);

unlink( $ini );

$reply->_eval("use strict;\n");
$reply->_eval("use warnings;\n");
$reply->_eval("use EPrints;\n");
$reply->_eval("\$Data::Dumper::Maxdepth = 1;\n");
$reply->_eval("\$EPrints::die_on_abort = 1;\n");
$reply->_eval("my \$session = EPrints::Session->new( 1 , '$archive' );\n");

print "Archive '$archive' loaded. Use \$session to access repository.\n";

$reply->run;


=head1 COPYRIGHT

=begin COPYRIGHT

Copyright 2023 University of Southampton.
EPrints 3.4 is supplied by EPrints Services.

http://www.eprints.org/eprints-3.4/

=end COPYRIGHT

=begin LICENSE

This file is part of EPrints 3.4 L<http://www.eprints.org/>.

EPrints 3.4 and this file are released under the terms of the
GNU Lesser General Public License version 3 as published by
the Free Software Foundation unless otherwise stated.

EPrints 3.4 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with EPrints 3.4.
If not, see L<http://www.gnu.org/licenses/>.

=end LICENSE
