#!/usr/bin/perl -w

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

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

=pod

=for Pod2Wiki

=head1 NAME

B<generate_views> - Generate static browse pages for an EPrint repository

=head1 SYNOPSIS

B<generate_views> I<repository_id> [B<options>] 

=head1 DESCRIPTION

This script creates some or all of the pages used in the /view/ section of the website. Since 3.1 these pages update themselves if they are older than a certain age, but this can cause delays for the viewer, so this script is still provided to pre-prepare them to provide a smoother experience.

Note: Since EPrints 3.1 it is not essential to run generate_views periodically, but it is still recommended.

For each view configured in $c->{browse_views}, (see: ~/archives/ARCHIVEID/cfg/cfg.d/views.pl) a static web page is created for each value of that field, and index pages to navigate to them. 

For example, if we make "year" view then this script will generate one page for each unique value of the year field. A user can then view the 1995 page and see links to all the 1995 eprints.

Advantages of this are that this puts less load on the database than user searches, assuming you pick two or three sensible fields to make views with.

On large repositories some views may take considerable time to generate. These views could be regenerated daily or weekly. Appropriate values for B<max_menu_age> and B<max_list_age> should be defined for the view in this case.

The more eprints the longer it will take to run. The rough length of time to run this is of the order of O( B<languages> * B<eprints> * B<browsable fields> ).  You can automate running this with the B<cron> system.

If the 'view' option is set, only that view is updated (the option can be repeated). This can be useful if some views need updating more often then others. The top /view/ page which links to each view is always updated.

=head1 ARGUMENTS

=over 8

=item B<repository_id> 

The ID of the eprint repository to use.

=back

=head1 OPTIONS

=over 8

=item B<--generate menus> 

Only generate the menu pages, not the pages with links to records.

=item B<--generate lists> 

Only generate the pages with lists of records, not the menu pages.

=item B<--view> I<view_id>

Generate only the view with this ID.
May be repeated B<--view> I<viewid_1> B<--view> I<viewid_2>

=item B<--lang> I<lang_id>

Generate only pages for the language with this ID. 

=item B<--help>

Print a brief help message and exit.

=item B<--man>

Print the full manual page and then exit.

=item B<--quiet>

Be vewwy vewwy quiet. This option will suppress all output unless an error occurs.

=item B<--verbose>

Explain in detail what is going on.
May be repeated for greater effect.

=item B<--version>

Output version information and exit.

=back   


=cut


use EPrints;

use File::Copy;
use strict;
use Getopt::Long;
use Pod::Usage;

my $version = 0;
my $verbose = 0;
my $quiet = 0;
my $help = 0;
my $man = 0;
my $generate_opt;
my @view_opts;
my $lang_opt;

Getopt::Long::Configure("permute");

GetOptions( 
	'help|?' => \$help,
	'man' => \$man,
	'version' => \$version,
	'verbose+' => \$verbose,
	'silent' => \$quiet,
	'quiet' => \$quiet,
	'generate=s' => \$generate_opt,
	'view=s' => \@view_opts,
	'lang=s' => \$lang_opt,
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "generate_views" ) if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) if( scalar @ARGV != 1 ); 

my $noise = 1;
$noise = 0 if( $quiet );
$noise = 1+$verbose if( $verbose );

my $do_menus = 1;
my $do_lists = 1;
if( defined $generate_opt )
{
	if( $generate_opt eq "menus" ) { $do_lists = 0; }
	elsif( $generate_opt eq "lists" ) { $do_menus = 0; }
	else 
	{
		print STDERR "--generate must be either menus or lists.\n";
		exit 1;
	}
}

my $PATH = "view";

# Set STDOUT to auto flush (without needing a \n)
$|=1;

binmode( STDOUT, ":utf8" );

my $repoid = $ARGV[0];

my $session = new EPrints::Session( 1 , $repoid , $noise );
if( !defined $session )
{
	print STDERR "Failed to load repository: $repoid\n";
	exit 1;
}

$session->cache_subjects;

my $repository = $session->get_repository;

my $views = $repository->get_conf( "browse_views" );

my $ds = $repository->get_dataset( "archive" );

if( @view_opts )
{
	foreach my $view_opt (@view_opts){
		my $ok = 0;
		foreach my $view ( @{$views} )
		{
			$ok = 1 if( $view->{id} eq $view_opt );
		}
		if( !$ok )
		{
			EPrints::abort( "Unknown view: $view_opt" );
		}
	}
}

LANGUAGE: foreach my $langid ( @{$repository->get_conf( "languages" )} )
{
	next LANGUAGE if( defined $lang_opt && $langid ne $lang_opt );

	$session->change_lang( $langid );

	my @files = ();
	my $dir =  $repository->get_conf( "htdocs_path" )."/".$langid."/view";

	VIEW: foreach my $view ( @{$views} )
	{
		next VIEW if( @view_opts && !grep( /$view->{id}/, @view_opts ) );

		$view = EPrints::Update::Views->new(
			repository => $session,
			view => $view
		);

		$view->update_view_by_path(
			on_write => sub { print "Wrote: $_[0]\n" if $noise > 1 },
			langid => $langid, 
			do_menus => $do_menus,
			do_lists => $do_lists );
	}

	if( $do_menus )
	{
		my $target = join "/", $session->config( "htdocs_path" ), $langid, "view", "index";
		# make views index page
		EPrints::Update::Views::update_browse_view_list( $session, $target, $langid );
		print "Wrote: $target\n" if( $noise > 1 );
	}


	# TODO: remove any pages we didn't create.0l
	

	print "done\n" if( $noise > 1 );
}

$session->terminate();
exit;


	

=head1 COPYRIGHT

=for COPYRIGHT BEGIN

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

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

=for COPYRIGHT END

=for LICENSE BEGIN

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/>.

=for LICENSE END

