#!/usr/bin/perl -w

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

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


=pod

=for Pod2Wiki

=head1 NAME

B<generate_static> - Generate static pages of an EPrint repository using the template.

=head1 SYNOPSIS

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

=head1 DESCRIPTION

This script creates the static web site for EPrints (or, if you are running in multiple lanugages it generates the websites).

It processes every file in B<EPRINTS/archives/ARCHIVE/cfg/static/LANGID/>. For each language processes all the files in /LANGID/ and /generic/ into B<EPRINTS/archives/ARCHIVE/html/LANGID>. If that sounds confusing, don't worry, it's not that bad, just put your webpage outlines in static/en/ and your image files and the like in static/generic/ and run this script and see what happens.

Most files are copied into the target directory as is and directory structure is preserved. 

Files with a .xpage or .xhtml suffix are processed as they are copied.


=over 8

=item B<.xpage> 

This is an XML file with the following structure:

 <?xml version="1.0" standalone="no" ?>
 <!DOCTYPE page SYSTEM "entities.dtd" >
 <page>
   <title>This is the page title</title>
   <body>
     <p>Some XHTML body</p><p>Which is <b>neat</b></p>
   </body>
 </page>

The resulting file will be a .html file (foo.xpage becomes foo.html). It will take the template for this repository and insert the title and body from the appropriate places.  It will also cause the the special EPrints entities to be converted as it is copied. See the main documentation.

=item B<.xhtml> 

This is a normal XHTML file but with the following XML header:

 <?xml version="1.0" standalone="no" ?>
 <!DOCTYPE html SYSTEM "entities.dtd" >

This will cause the the HTML entities to be properly decoded. It will also be renamed to .html for example, foo.xhtml will become foo.html

=back


=head1 NOTE FOR THE NON-ENGLISH MAJORITY

If you are running EPrints in a language other than English then place the static files in a directory of your ISO language ID instead of B<en>, for example French is B<fr>. The generic directory is for language neutral stuff. Which is extra handy if you want to run the site in more than one language. Also the entities file should be renamed from -en to -whatever eg. B<entities-fr.xml>.

=head1 ARGUMENTS

=over 8

=item B<repository_id> 

The ID of the eprint repository to use.

=back

=head1 OPTIONS

=over 8

=item B<--prune>

Remove stray files from the website (eg. if something was removed from the static pages).

=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 supress 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 strict;
use Getopt::Long;
use Pod::Usage;

my $version = 0;
my $verbose = 0;
my $quiet = 0;
my $prune = 0;
my $help = 0;
my $man = 0;

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

GetOptions( 
	'help|?' => \$help,
	'man' => \$man,
	'version' => \$version,
	'prune' => \$prune,
	'verbose+' => \$verbose,
	'silent' => \$quiet,
	'quiet' => \$quiet
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "generate_static" ) if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) if( scalar @ARGV != 1 ); 

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

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

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;
}

my $strays = 0;
foreach my $langid ( @{$session->config( "languages" )} )
{
	my @static_dirs = $session->get_static_dirs( $langid );

	# scan_static_dir overwrites existing locations
	my $map = {};
	foreach my $dir ( reverse @static_dirs )
	{
		next if !defined $dir;
		next if !-d $dir;
		scan_static_dir( $dir, $map );
	}
	$session->change_lang( $langid );
	my $base_target_dir = $session->config( "htdocs_path" )."/$langid";
	my $wrote_files = {};
	foreach my $target ( keys %{$map} )
	{
		my $source = $map->{$target};
		$target = $base_target_dir.$target;
		$target =~ m/^(.*)\/([^\/]+)/;
		my( $target_dir, $target_file ) = ( $1, $2 );
	
		if( !-e $target_dir )
		{
			print "mkdir $target_dir\n" if( $noise >= 1);
			EPrints::Platform::mkdir( $target_dir );
		}

		$source =~ m/\.([^.]+)$/;

		my $suffix = $1;

		print "$source -> $target\n" if( $noise >= 2);
		if( $suffix eq "xhtml" ) { copy_xhtml( $session, $source, $target, $wrote_files ); }
		elsif( $suffix eq "xpage" ) { copy_xpage( $session, $source, $target, $wrote_files ); }
		else { copy_plain( $session, $source, $target, $wrote_files ); }
	}	

	my $fn;

	# do the magic auto.js and auto.css
	$fn = EPrints::Update::Static::update_secure_auto_js(
			$session,
			$base_target_dir,
			\@static_dirs
		);
	$wrote_files->{$fn} = 1;

	$fn = EPrints::Update::Static::update_auto_js(
			$session,
			$base_target_dir,
			\@static_dirs
		);
	$wrote_files->{$fn} = 1;

	$fn = EPrints::Update::Static::update_auto_css(
			$session,
			$base_target_dir,
			\@static_dirs
		);
	$wrote_files->{$fn} = 1;

	my $existing_files = {};
	scan_dir( $base_target_dir, $existing_files );

	foreach my $e_file ( keys %{$existing_files} )
	{
		next if defined( $wrote_files->{$e_file} );
		next if( $e_file =~ m/^$base_target_dir\/view\// );
		next if( $e_file =~ m/^$base_target_dir\/view_tmp/ );
		next if( $e_file =~ m/^$base_target_dir\/archive\// );
		if( $prune )
		{
			print "removing $e_file\n" if( $noise >= 1);
			unlink( $e_file );
		}
		else
		{
			print "Unrecognised file in website: $e_file\n" if( $noise >= 1 );
			$strays = 1;
		}
	}
}

if( $strays )
{
	print "To prune unrecognised files run with the --prune option.\n" if( $noise >= 1 );
}
$session->terminate();

exit;

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

sub scan_dir
{
	my( $dir, $map ) = @_;

	my $dh;
	my @dirs = ();
	opendir( $dh, $dir ) || EPrints::abort( "Failed to read dir: $dir" );
	while( my $file = readdir( $dh ) )
	{
		next if $file eq ".svn";
		next if $file eq "CVS";
		next if $file eq ".";
		next if $file eq "..";
		if( -d "$dir/$file" ) 
		{
			push @dirs, $file;
			next;
		}
		# file
		$map->{"$dir/$file"} = 1;
	}
	closedir( $dh );

	foreach my $subdir ( @dirs )
	{
		scan_dir( $dir."/".$subdir, $map );
	}
}

sub scan_static_dir
{
	my( $dir, $map ) = @_;

	scan_static_dir_aux( $dir, $map, "" );
}

sub scan_static_dir_aux
{
	my( $dir, $map, $prefix ) = @_;

	my $dh;
	my @dirs = ();
	opendir( $dh, $dir ) || EPrints::abort( "Failed to read dir: $dir" );
	while( my $file = readdir( $dh ) )
	{
		next if $file eq "CVS";
		# allow .htaccess, .htpasswd etc. via a hack
		next if $file =~ /^\./ && $file !~ /^\.ht/;
		if( -d "$dir/$file" ) 
		{
			push @dirs, $file;
			next;
		}
		# file
		my $source = $dir."/".$file;
		my $target = $prefix."/".$file;
		$target =~ s/\.x(html|page)$/.html/;
		$map->{$target} = $source;
	}
	closedir( $dh );

	foreach my $subdir ( @dirs )
	{
		scan_static_dir_aux( $dir."/".$subdir, $map, $prefix."/".$subdir );
	}
}

# copy_plain( $from, $to, $wrote_files )
sub copy_plain { &EPrints::Update::Static::copy_plain(@_[1..$#_]) }

# copy_xpage( $session, $from, $to, $wrote_files )
sub copy_xpage { &EPrints::Update::Static::copy_xpage }

# copy_xhtml( $session, $from, $to, $wrote_files )
sub copy_xhtml { &EPrints::Update::Static::copy_xhtml }


=head1 COPYRIGHT

=for COPYRIGHT BEGIN

Copyright 2000-2011 University of Southampton.

=for COPYRIGHT END

=for LICENSE BEGIN

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

EPrints is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

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

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

=for LICENSE END

