#!/usr/bin/perl -w

use strict;

use POSIX;
use Time::Local;

use LWP::UserAgent;
use HTTP::Request::Common qw(POST GET);
use LWP::Simple;
use URI;
use Digest::MD5 qw(md5_hex);
use HTTP::Cookies;

my $ua= LWP::UserAgent->new();
my $cookies= HTTP::Cookies->new();
$cookies->set_cookie(0, "userId"=>"nrc_itsme", "/", "nrc.nl", 80, 0, 0, 9999999, 0);

#!!! set this password to whatever the nrc has assigned you
#   ( check browser cookie file )
$cookies->set_cookie(0, "password"=>"XXXXXXXX", "/", "nrc.nl", 80, 0, 0, 9999999, 0);
$ua->cookie_jar($cookies);

chdir "/home/itsme/prj/sites/foksuk/archive";

if (@ARGV) {
    processImageList(@ARGV);
    exit(0);
}
my $homepage=nrcget("http://www.nrc.nl/fs/index.html");

processhomepage($homepage);

createIndexFiles();

exit(0);

sub processImageList {
    for my $imgfile (@_) {
        my $img= nrcget("http://www.nrc.nl/images/$imgfile");

        processImage($imgfile, $imgfile, $img);
    }
}

sub processhomepage {
    my $homepage= shift;
    while ($homepage =~ m{<a href="/fs/artikel/(\d+)\.html"}g) {
        my $artikel=$1;

        my $imgpage= nrcget("http://www.nrc.nl/fs/artikel/print/$artikel.html");
        processImagePage($artikel, $imgpage);
    }

    if (my ($today)= ($homepage =~ m{<tr><td><img src="/images/([^"]*)"[^>]*></td></tr></table></div})) {

        my $todayimg= nrcget("http://www.nrc.nl/images/$today");
        processImage("today", $today, $todayimg);
    }
}

sub processImagePage {
    my ($artikel, $imgpage)= @_;

    if (my ($imgfile)= ($imgpage =~ m{class="artikelimg"[^>]+>[^<]*<tr>[^<]*<td>[^<]*<img src="/images/([^"]+)"}si)) {
        my $img= nrcget("http://www.nrc.nl/images/$imgfile");

        processImage($artikel, $imgfile, $img);
    }
}

sub processImage {
    my ($artikel, $imgfile, $img)= @_;
    my $imgtime= time; # TODO: get image time.

    my $md5= md5_hex($img);

    my @matches= glob("*-$md5-*");

    if (!@matches) {
        my $filename= strftime("%Y%m%d%H%M%S", localtime $imgtime)."-$md5-$imgfile";
        print "saving $artikel $filename\n";
        open IMG, ">", $filename or die "open: $filename : $!\n";
        print IMG $img;
        close IMG;
    }
}

sub createIndexFiles {
    my @files= glob("*.gif");
    my $nrpages= int($#files/6);

    for (my $page=0 ; $page<=$nrpages; $page++) {
        my $indexfile= "idx$page.html";

        open FH, ">", "$indexfile" or die "can't write $indexfile\n";

        print FH htmlHeader();
        print FH indexLine($page, $nrpages);
        print FH "<BR><BR>";
        print FH prevNextLinks($page, $nrpages);
        print FH "<BR><BR>";
        print FH images($page, \@files);
        print FH htmlFooter();

        close FH;
    }
    unlink "index.html";
    symlink "idx$nrpages.html", "index.html";
}

#
#-----------------------------------------------------------------------------
#
sub htmlHeader { return "<html><head><title>foksuk page</title></head><body>"; }
sub htmlFooter { return "</body></html>"; }

sub indexLine {
    my ($page, $nrpages)= @_;

    my @str= ("<DIV align=center>");

    for (my $j=0 ; $j<=$nrpages ; $j++) {
        push @str , " ", qq(&nbsp;) x 3  unless ($j==0);

        if ($j!=$page) {
            push @str, qq(<A HREF="idx$j.html">$j</A>);
        }
        else {
            push @str, "$j";
        }
    }
    push @str, "</DIV>";

    return join "", @str;
}

sub prevNextLinks {
    my ($page, $nrpages)= @_;

    return prevLink($page, $nrpages) . nextLink($page, $nrpages);
}
sub prevLink {
    my ($page, $nrpages)= @_;

    my @str= ("<DIV align=left>", "&nbsp;" x 20);
    if ($page>0) {
        push @str, qq(<A HREF="idx).($page-1).qq(.html">prev</A>);
    }
    else {
        push @str, "prev";
    }
    push @str, "</DIV>";

    return join "", @str;
}

sub nextLink {
    my ($page, $nrpages)= @_;

    my @str= ("<DIV align=right>");
    if ($page<$nrpages) {
        push @str, qq(<A HREF="idx).($page+1).qq(.html">next</A>);
    }
    else {
        push @str, "next";
    }
    push @str, "&nbsp;" x 20, "</DIV>";

    return join "", @str;
}

sub images {
    my ($page, $files)= @_;

    my @str= ();
    for (my $i=0 ; $i<6 ; $i++) {
        push @str, qq(<IMG src="$files->[$page*6+$i]">) if ($files->[$page*6+$i]);
    }

    return join "", @str;
}


sub nrcget {
    my $url= shift;
    return $ua->request(GET $url)->content;
}
