#!/usr/bin/perl -W # # lastmod.pl - A script which add the Last-Modified header to the specified HTML files # # Copyright (C) 2006,2011 Bogdan 'bogdro' Drozdowski # (bogdandr AT op.pl, bogdro AT rudy.mif.pg.gda.pl) # # Licence: # GNU General Public Licence v3 # # Last modified : 2011-12-18 # # Syntax: # ./lastmod.pl xxx.html # # If the input filename is "-", standard input will be read and the result will # be written to the standard output. # # 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 3 # 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 Foudation: # Free Software Foundation # 51 Franklin Street, Fifth Floor # Boston, MA 02110-1301 # USA use strict; use warnings; my ($outfname, $head, $lastmod); if ( @ARGV == 0 ) { print "Syntax: $0 xxx.html\n"; exit 1; } if ( $ARGV[0] !~ /\.htm/io ) { print "Syntax: $0 xxx.html\n"; exit 1; } ########################################################## # opening the files my $hin; my $hout; if ( !open ( $hin, $ARGV[0] ) ) { # $! is the error message print "$0: $ARGV[0]: $!\n"; exit 2; } if ( $ARGV[0] eq '-' ) { $outfname = '-'; } else { # take only the filename ($outfname = $ARGV[0]) =~ s/[\w\-\/\s]+\/([\w\-]+)/$1/; $outfname .= ".out"; } if ( !open ( $hout, "> $outfname" ) ) { print "$0: $outfname: $!\n"; close $hin; exit 3; } $head = 0; $lastmod = 0; my $datetime = gmtime; $datetime =~ s/(\w+)\s+(\w+)\s+(\d+)\s+(\d{1,2}:\d{1,2}:\d{1,2})\s+(\d{4})(.*)/$1, $2 $3 $5 $4 $6/; chop($datetime); ########################################################## # processing: while ( <$hin> ) { if ( !$head && /\n$_"; } print $hout $_; } ########################################################## # end close $hout; close $hin; rename $outfname, $ARGV[0];