#!/usr/bin/perl -w
# vim: set sw=4 ts=4 si et:
# Written by: Guido Socher
#
use strict;
use vars qw($opt_g $opt_h);
use Getopt::Std;
#
sub help();
#
getopts("hg:")||exit 1;
help() if ($opt_h);
#
my $netscape;
my $lang;
my $cmd;
my $pdf;
help() unless($ARGV[1]);
#
$lang=$ARGV[0];
$netscape=$ARGV[1];
unless(-w "."){
    die "ERROR:$0: can not write in current directory\n";
}
rename("htmlfiles.txt","htmlfiles.txt.bak");
$cmd="-noraise -remote \"openurl(http://linuxfocus.org/$lang/Archives/htmlfiles.txt)\"";
print "OK: running $cmd ...\n";
print `$netscape $cmd 2>&1`;
die "command failed\n" if ($?);
#
sleep(5);
#
$cmd="-noraise -remote \"saveas(htmlfiles.txt,Text)\"";
print "OK: running $cmd ...\n";
print `$netscape $cmd 2>&1`;
die "command failed\n" if ($?);

my @line;
open(FF,"htmlfiles.txt")||die "ERROR: can not read htmlfiles.txt\n";
while(<FF>){
    next unless(m!^lf!);
    if ($opt_g){
        next unless(m!$opt_g!io);
    }
    print "OK: working on $_";
    @line=split();
    $cmd="-noraise -remote \"openurl(http://linuxfocus.org/$lang/Archives/$line[0])\"";
    print "OK: running $cmd ...\n";
    print `$netscape $cmd 2>&1`;
    die "command failed\n" if ($?);
    $line[0]=~s/\.html/.ps/;
    sleep(6);
    rename($line[0],$line[0].".bak");
    $cmd="-noraise -remote \"saveas($line[0],PostScript)\"";
    print "OK: running $cmd ...\n";
    print `$netscape $cmd 2>&1`;
    die "command failed\n" if ($? || ! -r "$line[0]");
    sleep(1);
    $pdf=$line[0];
    $pdf=~s/\.ps/.pdf/;
    $cmd="ps2pdf $line[0]";
    rename($pdf,$pdf . ".bak");
    print "OK: running $cmd ...\n";
    print `$cmd 2>&1`;
    if ($? || ! -f $pdf){
        unlink($pdf);
        die "command failed\n" if ($? || ! -f $pdf);
    }
    unlink($line[0]);
}
close FF;
#
sub help(){
print "lfpdf_remote -- generate pdf files using netscape 4.x

USAGE: lfpdf_remote [-h][-g greppattern] LangDir path_to_nestscape 

OPTIONS: -h this help
         -g update only files matching a given pattern in the 
            htmlfiles.txt file in the LangDir/Archives directory
            on linuxfocus.org

Note: Terminate all open webbrowsers and then start manually
only netscape FROM THE CURRENT WORKING DIRECOTRY WHERE YOU WILL
RUN THIS SCRIPT FROM

EXAMPLE:
 /opt/netscape/netscape &
 lfpdf_remote -g 2002 Francais /opt/netscape/netscape
";
exit;
}

__END__ 

