Index: other-projects/trunk/realistic-books/src/pdf2realbook.pl
===================================================================
--- other-projects/trunk/realistic-books/src/pdf2realbook.pl	(revision 19253)
+++ other-projects/trunk/realistic-books/src/pdf2realbook.pl	(revision 19628)
@@ -45,33 +45,47 @@
 
 
-if ($#ARGV < 0)
-{
-    my $full_progname = $0;
+sub print_usage
+{
+    my ($full_progname) = @_;
+
     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];
+
+    exit(-1);
+}
+
+sub check_pdf2swf
+{
+    my $output = `pdf2swf --version`;
+
+    if (!defined $output || ($output !~ m/swftools/i)) {
+	print STDERR "Unable to find pdf2swf: $!\n";
+	print STDERR "Have you sourced setup.bash (Linux/MacOS)?\n";
+	print STDERR "Have you run     setup.bat  (Windows)?\n";
+	exit(-1);
+    }
+}
+
+sub check_file
+{
+    my ($filename) = @_;
+
     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);
+	print STDERR "Error: $filename is a directory.\n";
+	print STDERR "       Please specify the PDF file as input.\n";
+	exit(-1);
+    }
+
+    if (!-e $filename) {
+	print STDERR "Error: Unable to find $filename\n";
+	exit(-1);
+    }
+}
+
+sub copy_model_col
+{
+    my ($folder_name) = @_;
 
     #--
@@ -92,51 +106,101 @@
 	copy($src_filename,$dst_filename);
     }
-
-    my $html_input = "<HTML>\n<title>$folder_name</title>\n<Description>\n";
+}
+
+sub pdf2swf_info
+{
+    my ($filename) = @_;
+
+    my $cmd = "pdf2swf --info $filename";
+    if ($ENV{'RBOS'} =~ /^windows$/i) {
+	$cmd .= " 2>null";
+    }
+    else {
+	$cmd .= " 2>/dev/null";
+    }
+    
+    my @output_lines = split(/\n/,`$cmd`);
+
+    my $last_line = pop(@output_lines);
+
+    # Watch out for trailing \n
+    $last_line = pop(@output_lines) if ($last_line =~ m/^$/);
+
+    my ($num_pages) = ($last_line =~ m/^page=(\d+)/);
+
+    return $num_pages;
+}
+
+    
+sub generate_html
+{
+    my ($full_folder_name,$folder_root,$filename) = @_;
+
+    my $num_pages = pdf2swf_info($filename);
+    print STDOUT "Total page: $num_pages\n";
+
+    my $html_input = "<HTML>\n<title>$folder_root</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=\"Filename\">$folder_root</Metadata>\n";
+    $html_input = "$html_input<Metadata name=\"PageFolder\">.</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)
-    {
+
+    for (my $i=1; $i<=$num_pages; $i++) {
+
 	my $backFile = "Page_$i.swf";
-	my $frontFile = "$folder_name";
+	my $frontFile = "$full_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");
+
+    return $html_input;
+}
+
+
+sub main
+{
+    my ($argc,@argv) = @_;
+
+    my $full_progname = $0;
+
+    if ($argc < 0) {
+	print_usage($full_progname);
+    }
+
+    check_pdf2swf();
+
+    my $filename = $argv[0];
+    check_file($filename);
+
+    # print STDERR "Ignore the message called RegOpenKeyEx failed\n";
+
+    my($file_root, $fulldir, $file_ext) = fileparse($filename,qr/\.[^.]*/);
+
+    my $full_folder_name = catfile($ENV{'REALISTIC_BOOKS_HOME'},"books",$file_root);
+
+    copy_model_col($full_folder_name);
+
+    my $html_input = generate_html($full_folder_name,$file_root,$filename);
+
+    # Write out HTML file
+    my $html_file = catfile($full_folder_name,"pages.htm");
     open (PROD, ">$html_file") || die("Error Writing to File: $html_file $!");
+    print STDERR "Creating $html_file\n";
+
     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";
-
-}
+
+
+    print STDOUT "Realistic book generated in $full_folder_name\n";
+
+}
+
+
+&main(scalar(@ARGV),@ARGV);
