#!/usr/bin/perl -w # Copyright © 2001-2012 Jamie Zawinski # # Permission to use, copy, modify, distribute, and sell this software and its # documentation for any purpose is hereby granted without fee, provided that # the above copyright notice appear in all copies and that both that # copyright notice and this permission notice appear in supporting # documentation. No representations are made about the suitability of this # software for any purpose. It is provided "as is" without express or # implied warranty. # # Created: 20-Jul-01. # # This updates the .htaccess file to change the redirects for the various # "latest.html" pseudo-files for calendar-like things. require 5; use diagnostics; use strict; my $progname = $0; $progname =~ s@.*/@@g; my $version = q{ $Revision: 1.22 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/; my $verbose = 0; my $base_head_1 = ""; sub calendar_latest($$$$) { my ($dotm, $month, $year, $base_head) = @_; return ( "$base_head/calendar/latest.html", sprintf ("$base_head/calendar/%04d/%02d.html", $year, $month+1)); } sub flyer_latest($$$$) { my ($dotm, $month, $year, $base_head) = @_; return ( "$base_head/flyers/latest.html", sprintf ("$base_head/flyers/%04d/%02d/", $year, $month+1)); } sub gallery_latest($$$$) { my ($dotm, $month, $year, $base_head) = @_; my $gdir = "gallery"; my @files; opendir (my $DIR, "$gdir") || error ("$gdir: $!"); @files = readdir ($DIR); closedir $DIR; my $yyyy; foreach (sort { $b cmp $a } @files) { if (m/^\d{4}$/) { $yyyy = $_; last; } } return () unless $yyyy; # opendir ($DIR, "$gdir/$yyyy") || error ("$gdir/$yyyy: $!"); # @files = readdir ($DIR); # closedir $DIR; # my $mm_dd; # foreach (sort { $b cmp $a } @files) { # # if (m/^\d{2}-\d{2}$/) { # $mm_dd = $_; # last; # } # } # return () unless $mm_dd; return ("$base_head/gallery/latest.html", # "$base_head/gallery/$yyyy/$mm_dd/" "$base_head/gallery/$yyyy/" ); } sub update_htaccess($) { my ($dir) = @_; my ($sec,$min,$hour,$dotm,$mon,$year,$wday,$yday,$isdst) = localtime; $year += 1900; my @redirects = (); my $h1 = $base_head_1; push @redirects, calendar_latest ($dotm, $mon, $year, $h1); push @redirects, flyer_latest ($dotm, $mon, $year, $h1); push @redirects, gallery_latest ($dotm, $mon, $year, $h1); my $file = "$dir/.htaccess"; open (my $in, '<', $file) || error ("$file: $!"); local $/ = undef; # read entire file my $body = <$in>; close $in; my $changed = 0; while (my $from = shift @redirects) { my $to = shift @redirects; # if ($body =~ s@^(Redirect\s+\d\d\d\s+\Q$from\E\s+)(.*)$@$1$to@img) { if ($body =~ m@^(Redirect\s+\d\d\d\s+\Q$from\E\s+)(.*)$@im) { $body =~ s@^(Redirect\s+\d\d\d\s+\Q$from\E\s+)(.*)$@$1$to@im; my $old = $2; if ($to eq $old) { print STDERR "$progname: $from -> $to (unchanged)\n" if ($verbose > 3); } else { $changed++; print STDERR "$progname: $from -> $to (was $old)\n" if ($verbose > 2); } } else { error ("didn't find \"$from\" redirector in $file"); } } if ($changed) { open (my $out, '>', $file) || error ("$file: $!"); (print $out $body) || error ("$file: $!"); close $out; print STDERR "$progname: wrote $file\n" if ($verbose); } else { print STDERR "$progname: $file unchanged\n" if ($verbose > 1); } } sub error($) { my ($err) = @_; print STDERR "$progname: $err\n"; exit 1; } sub usage() { print STDERR "usage: $progname [--verbose] directory\n"; exit 1; } sub main() { my $dir; while ($_ = $ARGV[0]) { shift @ARGV; if ($_ eq "--verbose") { $verbose++; } elsif (m/^-v+$/) { $verbose += length($_)-1; } elsif (m/^-./) { usage; } elsif (!defined($dir)) { $dir = $_; } else { usage; } } # When writing files, make permissions match the parent directory # by computing a umask from the directory's permissions. # umask (~((stat($dir))[2] & 0666) & 0666); usage unless defined($dir); update_htaccess ($dir); } main(); exit 0;