#/usr/bin/perl -w
###############################################################################
# Perl-skripti tiedoston hakemiseksi WWW:sta 
#
# Kutsun syntaksi:
#   hae.pl linkki [kohdenimi]
#
# Tiedosto haetaan vain mikäli se on uudempi kuin jo mahdollisesti levyllä
# oleva.
#
#   Esim:
#
#   perl hae.pl http://www.jyu.fi/~vesal/gko/files.txt
#     - hakee tiedoston http://www.jyu.fi/~vesal/gko/files.txt
#   
# Vesa Lappalainen 7.2.2000
#
#
###############################################################################

use strict;

###############################################################################
sub trim($) { # merkkijono => merkkijono
  my($s) = @_;
# Poistetaan merkkijonosta alkutyhjat, lopputyhjat ja rivinvaihdot (lopusta)
# Esim. "   kissa istuu\n   "  => "kissa istuu"
  $s =~ s/^\s*//;      # alkutyhjat
  $s =~ s/\s*$//;      # lopputyhjat (myös rivinvaihdot)
# s/^\s*(.*?)\s*$/\1/;
  return $s;
}

use Time::localtime;
use File::stat;

###############################################################################
sub GetWWWFileDate($) # => mtime
#------------------------------------------------------------------------------
# Hakee WWW-tiedoston päiväyksen
# Esim: GetWWWFileDate('http://www.jyu.fi/~vesal/gko/delphi/vespacadf.dfm')
###############################################################################
{
  my ($url) = @_;
  $url  = trim($url);


  require LWP;
  # Create the user agent object
  my $ua =new LWP::UserAgent;

  # Set up a request.  We will use the same request object for all URLs.
  my $request = new HTTP::Request "GET";

  # Send the request and get a response back from the server
  $request->url($url);
  my $response = $ua->request($request);

  if ($response->is_success) {
    $response->headers->last_modified;
  } else {
    0;
  }

}

###############################################################################
sub GetWWWFile($$) {  
  my ($url,$file) = map {trim($_)} @_;
#  $url  = trim($url);
#  $file = trim($file);
#------------------------------------------------------------------------------
# Hakee WWW-tiedoston ja tallettaa sen lokaalisti valituksi tiedostoksi
# Esim: GetWWWFile('http://www.jyu.fi/~vesal/gko/delphi/vespacadf.dfm',
#                  'delphi/vespacadf.dfm');
# Mahdollisesti tarvittavat alihakemistot pitää jo olla olemassa
# Otetaan vain tiedostot, jotka ovat uudempia kuin kohdekoneessa
###############################################################################
  my $filedate = 0;
  my $filestat;
  $filestat = stat($file) and $filedate = $filestat->mtime; 
# my $filedate = $filestat->mtime if $filestat = stat($file);
  require LWP;
  # Create the user agent object
  my $ua =new LWP::UserAgent;

  # Set up a request.  We will use the same request object for all URLs.
  my $request = new HTTP::Request "GET";

  # Send the request and get a response back from the server
  $request->url($url);
  my $response = $ua->request($request);

  if ($response->is_success) {
    if ( $response->headers->last_modified > $filedate ) {
      print "$url => $file\n";
      # print ctime($response->headers->last_modified) . " " . ctime($filedate) . "\n"; 
      # print $response->headers->last_modified . " $filedate"\n"; 
      open(OUT, ">$file") or die "Error opening output file $file: $!\n"; 
      binmode OUT;
      print OUT $response->content;
      close(OUT) if ($file);
      utime $response->headers->last_modified,$response->headers->last_modified,$file;
    } 
    # else { print ctime($response->headers->last_modified) . " " . ctime($filedate) . "\n"; } 
    # else { print "@{[ctime($response->headers->last_modified) ctime($filedate)\n"; } 
    # else { print "$response->headers->last_modified";
  } else {
    print STDERR "ERROR: $url\n"; 
    print STDERR $response->error_as_HTML;
  }

}

###############################################################################
# Pääohjelma
###############################################################################
my $www      = $ARGV[0];
my $destname = $ARGV[1]; 
my $filename = $www;

if ( !$filename ) { die "Haetaan tiedosto WWW:sta (vl 7.2.2000)\nKaytto: hae.pl url [kohdenimi]\n"; }

$filename =~ s!.*/!!;
if ( $destname ) { $filename = $destname; }

# print "Link: $www filename: $filename destname: $destname\n";
GetWWWFile("$www","$filename");

