#!/usr/bin/perl -w
#
# A component of the Realistic Book software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2007 New Zealand Digital Library Project
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

#*****************************************************************
#Realistic Book application
#	Converting each PDF page into a SWF file. Also produce
#	the XHTML input file for the Realistic book program
#
#by Veronica Liesaputra --- 1 August 2007
#*****************************************************************/

use warnings; 
use strict;

use File::Basename;
use File::Spec::Functions;
use File::Copy;

BEGIN {
    die "REALISTIC_BOOKS_HOME not set. Have you sourced setup.bash?\n" 
	unless defined $ENV{'REALISTIC_BOOKS_HOME'};
    die "RBOS not set. Have you sourced setup.bash?\n" 
	unless defined $ENV{'RBOS'};
}


if ($#ARGV < 0)
{
    my $full_progname = $0;
    my $progname = basename($full_progname);

    print STDERR "\nUsage: $progname pdf_filename\n";
    print STDERR "or     perl -S $progname pdf_filename\n\n";
}
else
{
    my $progname = `pdf2swf --version`;
    if (defined $progname && $progname =~ m/swftools/i) {
    } else {
	die("\nPlease make sure that pdf2swf is located in the same location as this script.\nIf you are in Linux/Macintosh, please install swftools in SWFToolsPackage.\n");
    }
    my $filename = $ARGV[0];
    if (-d $filename) {
	die("$filename is a directory! Please specify the file extension. \n");
    }
    if (-f $filename) {
    } else {
	die("Cannot find $filename! \n");
    }

##    print STDERR "Ignore the message called RegOpenKeyEx failed\n";

    my($file_root, $fulldir, $file_ext) = fileparse($filename,qr/\.[^.]*/);

    my $folder_name = catfile($ENV{'REALISTIC_BOOKS_HOME'},"books",$file_root);

    #--
    # Copy modelcol content to new book folder
    # --

    mkdir($folder_name);
    my $modelcol_dir = catfile($ENV{'REALISTIC_BOOKS_HOME'},"books","modelcol");
    opendir(DIR, $modelcol_dir) 
	|| die "Cannot open directory $modelcol_dir: $!";

    my @model_files = grep { -f catfile($modelcol_dir,$_) } readdir(DIR);
    closedir DIR;

    foreach my $f ( @model_files) {
	my $src_filename = catfile($modelcol_dir,$f);
	my $dst_filename = catfile($folder_name,$f);
	copy($src_filename,$dst_filename);
    }

    my $html_input = "<HTML>\n<title>$folder_name</title>\n<Description>\n";
    $html_input = "$html_input<!-- Please do not remove Filename and PageFolder metadata -->\n";
    $html_input = "$html_input<Metadata name=\"Filename\">$folder_name</Metadata>\n";
    $html_input = "$html_input<Metadata name=\"PageFolder\">$folder_name</Metadata>\n";
    $html_input = "$html_input<Metadata name=\"StartNumbering\">1</Metadata>\n";
    $html_input = "$html_input</Description>\n";
    
    my $i = 1;
    my $page_num = $i;

    while ($i <= $page_num)
    {
	my $backFile = "Page_$i.swf";
	my $frontFile = "$folder_name";
	my $output_file = catfile($frontFile,$backFile);
	my $imgfile = `pdf2swf -p $i -s insertstop -s zoom=100 $filename -o $output_file`;
	
	if ($i == 1)
	{
	    if ($imgfile =~ m/Pages:/i)
	    {
		my $tmp = $';
		if ($tmp =~ m/(\d+)/)
		{
		    $page_num = $1;
		    print STDOUT "Total page: $page_num\n";
		}
	    }
	}

	print STDOUT "creating page $i\n";
	
	#$html_input = "$html_input<newpage totalframes=\"1\" /><!-- filenum=\"$i\" -->\n";
	$html_input = "$html_input<newpage filename=\"Page_$i\" />\n";
	$i = $i + 1;
    }

    $html_input = "$html_input</HTML>\n";
    
    my $html_file = catfile($folder_name,"pages.htm");
    open (PROD, ">$html_file") || die("Error Writing to File: $html_file $!");
    print PROD $html_input;
    close (PROD) || die("Error Closing File: $html_file $!");
    print STDERR "creating $html_file\n";

    print STDOUT "Realistic book generated in $folder_name\n";

}
